indigoX
common.hpp
Go to the documentation of this file.
1 
4 #ifndef INDIGOX_UTILS_COMMON_HPP
5 #define INDIGOX_UTILS_COMMON_HPP
6 
7 #include <algorithm>
8 #include <iterator>
9 #include <memory>
10 #include <string>
11 #include <type_traits>
12 
13 // forward definitions of serilisation stuff
14 namespace cereal {
15  class access;
16  class PortableBinaryInputArchive;
17  class PortableBinaryOutputArchive;
18  class JSONInputArchive;
19  class JSONOutputArchive;
20  template <class T> class construct;
21 } // namespace cereal
22 
24 namespace indigox::utils {
25  enum class Option {
26  Yes = 1,
27  No = Yes << 1,
28  Auto = Yes << 2,
29  Default = Yes << 3,
30  All = Yes << 4,
31  Some = Yes << 5,
32  None = Yes << 6
33  };
34 
35  inline Option operator|(Option l, Option r) {
36  using under = std::underlying_type<Option>::type;
37  return static_cast<Option>(static_cast<under>(l) | static_cast<under>(r));
38  }
39 
40  inline Option operator&(Option l, Option r) {
41  using under = std::underlying_type<Option>::type;
42  return static_cast<Option>(static_cast<under>(l) & static_cast<under>(r));
43  }
44 
48  std::string ToUpper(const std::string &s);
49 
53  std::string ToLower(const std::string &s);
54 
59  std::string ToUpperFirst(const std::string &s);
60 
66  std::string GetRandomString(size_t length, size_t seed = 0);
67 
77  template <typename T, typename __Iter>
78  inline __Iter WeakContainsShared(__Iter b, __Iter e, std::shared_ptr<T> x) {
79  static_assert(
80  std::is_same<typename std::weak_ptr<T>,
81  typename std::iterator_traits<__Iter>::value_type>::value,
82  "__Iter must be iterator over std::weak_ptr<T>.");
83  if (!x) return e;
84  return std::find_if(b, e,
85  [x](std::weak_ptr<T> _x) { return _x.lock() == x; });
86  }
87 
94  // template<typename T>
95  // inline bool IsNull(std::shared_ptr<T> t) {
96  // return !(t && *t);
97  // }
98 
99 } // namespace indigox::utils
100 
101 #endif /* INDIGOX_UTILS_COMMON_HPP */
Option
Definition: common.hpp:25
Definition: common.hpp:20
Definition: common.hpp:24
std::string ToUpperFirst(const std::string &s)
Convert a string to lower case with a single leading upper case.
std::string ToUpper(const std::string &s)
Convert a string to upper case.
Option operator|(Option l, Option r)
Definition: common.hpp:35
Definition: common.hpp:14
Option operator &(Option l, Option r)
Definition: common.hpp:40
std::string ToLower(const std::string &s)
Convert a string to lower case.
std::string GetRandomString(size_t length, size_t seed=0)
Generate a random string.
__Iter WeakContainsShared(__Iter b, __Iter e, std::shared_ptr< T > x)
Check if a given shared_ptr<T> is in a weak_ptr<T> container.
Definition: common.hpp:78