omw  0.2.1-beta
utility.h
1 /*
2 author Oliver Blaser
3 date 06.04.2022
4 copyright MIT - Copyright (c) 2023 Oliver Blaser
5 */
6 
7 #ifndef IG_OMW_UTILITY_H
8 #define IG_OMW_UTILITY_H
9 
10 #include <cstddef>
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 
15 namespace omw
16 {
21  void shiftLeftAssign(int8_t& value, unsigned int n);
22  void shiftLeftAssign(int16_t& value, unsigned int n);
23  void shiftLeftAssign(int32_t& value, unsigned int n);
24  void shiftLeftAssign(int64_t& value, unsigned int n);
25  void shiftLeftAssign(uint8_t& value, unsigned int n);
26  void shiftLeftAssign(uint16_t& value, unsigned int n);
27  void shiftLeftAssign(uint32_t& value, unsigned int n);
28  void shiftLeftAssign(uint64_t& value, unsigned int n);
29  void shiftRightAssign(int8_t& value, unsigned int n);
30  void shiftRightAssign(int16_t& value, unsigned int n);
31  void shiftRightAssign(int32_t& value, unsigned int n);
32  void shiftRightAssign(int64_t& value, unsigned int n);
33  void shiftRightAssign(uint8_t& value, unsigned int n);
34  void shiftRightAssign(uint16_t& value, unsigned int n);
35  void shiftRightAssign(uint32_t& value, unsigned int n);
36  void shiftRightAssign(uint64_t& value, unsigned int n);
37  int8_t shiftLeft(int8_t value, unsigned int n);
38  int16_t shiftLeft(int16_t value, unsigned int n);
39  int32_t shiftLeft(int32_t value, unsigned int n);
40  int64_t shiftLeft(int64_t value, unsigned int n);
41  uint8_t shiftLeft(uint8_t value, unsigned int n);
42  uint16_t shiftLeft(uint16_t value, unsigned int n);
43  uint32_t shiftLeft(uint32_t value, unsigned int n);
44  uint64_t shiftLeft(uint64_t value, unsigned int n);
45  int8_t shiftRight(int8_t value, unsigned int n);
46  int16_t shiftRight(int16_t value, unsigned int n);
47  int32_t shiftRight(int32_t value, unsigned int n);
48  int64_t shiftRight(int64_t value, unsigned int n);
49  uint8_t shiftRight(uint8_t value, unsigned int n);
50  uint16_t shiftRight(uint16_t value, unsigned int n);
51  uint32_t shiftRight(uint32_t value, unsigned int n);
52  uint64_t shiftRight(uint64_t value, unsigned int n);
53 
54  // grp_utility_langSupport
64  {
65  public:
66  Base_Nullable() : m_isNull(true) {}
67  explicit Base_Nullable(bool isNull) : m_isNull(isNull) {}
68  virtual ~Base_Nullable() {}
69 
70  bool isNull() const { return m_isNull; }
71 
72  virtual void makeNull() { m_isNull = true; }
73 
74  protected:
75  void setIsNull(bool isNull) { m_isNull = isNull; }
76 
77  private:
78  bool m_isNull;
79  };
80 
81  template <typename T>
82  class Nullable : public Base_Nullable
83  {
84  public:
85  using value_type = T;
86  using reference = value_type&;
87  using const_reference = const value_type&;
88 
89  public:
90  Nullable() : Base_Nullable(), m_value() {}
91  Nullable(const_reference value) : Base_Nullable(false), m_value(value) {}
92  virtual ~Nullable() {}
93 
94  const_reference get(const_reference fallback) const { return (isNull() ? fallback : m_value); }
95  void set(const_reference value) { setIsNull(false); m_value = value; }
96 
97  virtual void free() { m_value = value_type(); }
98  virtual void makeNull() { free(); Base_Nullable::makeNull(); }
99 
100  explicit operator value_type() const { return m_value; }
101 
102  protected:
103  value_type m_value;
104  };
105 
106  inline bool isNull(const omw::Base_Nullable& value) { return value.isNull(); }
107 
108 
109 
110  inline void toggle(bool& value) { value = !value; }
111  inline void toggle(int& value) { value = (value ? 0 : 1); }
112 
113  template <class Type>
114  bool vectorContains(const std::vector<Type>& v, const Type& item)
115  {
116  bool r = false;
117  for (size_t i = 0; (i < v.size()) && !r; ++i)
118  if (v[i] == item) r = true;
119  return r;
120  }
121 
122  // grp_utility_gpUtil
124 }
125 
126 #endif // IG_OMW_UTILITY_H
omw::Base_Nullable
Definition: utility.h:64
omw::Nullable
Definition: utility.h:83
omw
Main namespace.