// Copyright 2019 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef UTIL_STD_UTIL_H_ #define UTIL_STD_UTIL_H_ #include <stddef.h> #include <algorithm> #include <map> #include <sstream> #include <string> #include <utility> #include <vector> #include "util/stringprintf.h" namespace openscreen { template <typename T, size_t N> constexpr size_t countof(T (&array)[N]) { … } // std::basic_string::data() has no mutable overload prior to C++17 [1]. // Hence this overload is provided. // Note: str[0] is safe even for empty strings, as they are guaranteed to be // null-terminated [2]. // // [1] http://en.cppreference.com/w/cpp/string/basic_string/data // [2] http://en.cppreference.com/w/cpp/string/basic_string/operator_at template <typename CharT, typename Traits, typename Allocator> CharT* data(std::basic_string<CharT, Traits, Allocator>& str) { … } // Stringify a vector of objects that have an operator<< overload. template <typename T> std::string Join(const std::vector<T>& vec, const char* delimiter = ", ") { … } // Removes ALL whitespace in place from the string, based on the present C // locale. This includes spaces, tabs, and returns. This is useful for string // comparisons where whitespace doesn't matter, or, in the case of JSON // serialization, is dependent on build configuration and other settings. std::string& RemoveWhitespace(std::string& s); template <typename Key, typename Value> void RemoveValueFromMap(std::map<Key, Value*>* map, Value* value) { … } template <typename ForwardIteratingContainer> bool AreElementsSortedAndUnique(const ForwardIteratingContainer& c) { … } template <typename RandomAccessContainer> void SortAndDedupeElements(RandomAccessContainer* c) { … } // Append the provided elements together into a single vector. This can be // useful when creating a vector of variadic templates in the ctor. // // This is the base case for the recursion template <typename T> std::vector<T>&& Append(std::vector<T>&& so_far) { … } // This is the recursive call. Depending on the number of remaining elements, it // either calls into itself or into the above base case. template <typename T, typename TFirst, typename... TOthers> std::vector<T>&& Append(std::vector<T>&& so_far, TFirst&& new_element, TOthers&&... new_elements) { … } // Creates an empty vector with |size| elements reserved. Intended to be used as // GetEmptyVectorOfSize<T>(sizeof...(variadic_input)) template <typename T> std::vector<T> GetVectorWithCapacity(size_t size) { … } // Returns true if an element equal to |element| is found in |container|. // C.begin() must return an iterator to the beginning of C and C.end() must // return an iterator to the end. template <typename C, typename E> bool Contains(const C& container, const E& element) { … } // Returns true if any element in |container| returns true for |predicate|. // C.begin() must return an iterator to the beginning of C and C.end() must // return an iterator to the end. template <typename C, typename P> bool ContainsIf(const C& container, P predicate) { … } } // namespace openscreen #endif // UTIL_STD_UTIL_H_