libUTL++
Pair.h
1 #pragma once
2 
4 
5 #include <libutl/String.h>
6 
8 
9 UTL_NS_BEGIN;
10 
23 
25 class Pair : public Object, protected FlagsMI
26 {
28 
29 public:
37  Pair(const Object& first,
38  const Object& second,
39  bool firstOwner = true,
40  bool secondOwner = true);
41 
49  Pair(const Object* first,
50  const Object* second,
51  bool firstOwner = true,
52  bool secondOwner = true);
53 
55  void
57  {
58  setFirst(nullptr);
59  setSecond(nullptr);
60  }
61 
62  virtual int compare(const Object& rhs) const;
63 
64  virtual void copy(const Object& rhs);
65 
66  virtual void steal(Object& rhs);
67 
68  virtual void vclone(const Object& rhs);
69 
71  virtual const Object& getKey() const;
72 
73  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
74 
75  virtual String toString() const;
76 
77  virtual size_t innerAllocatedSize() const;
78 
80  Object*
81  first() const
82  {
83  return _first;
84  }
85 
87  Object*
88  second() const
89  {
90  return _second;
91  }
92 
94  Object*
95  getFirst() const
96  {
97  return _first;
98  }
99 
101  Object*
102  getSecond() const
103  {
104  return _second;
105  }
106 
108  void setFirst(const Object* first);
109 
111  void setSecond(const Object* second);
112 
114  void
115  setFirst(const Object& first)
116  {
117  if (isFirstOwner())
118  setFirst(first.clone());
119  else
120  setFirst(&first);
121  }
122 
124  void
125  setSecond(const Object& second)
126  {
127  if (isSecondOwner())
128  setSecond(second.clone());
129  else
130  setSecond(&second);
131  }
132 
134  bool
135  isFirstOwner() const
136  {
137  return getFlag(flg_firstOwner);
138  }
139 
141  bool
143  {
144  return getFlag(flg_secondOwner);
145  }
146 
148  void
149  setFirstOwner(bool firstOwner)
150  {
151  setFlag(flg_firstOwner, firstOwner);
152  }
153 
155  void
156  setSecondOwner(bool secondOwner)
157  {
158  setFlag(flg_secondOwner, secondOwner);
159  }
160 
161 private:
162  enum flg_t
163  {
164  flg_firstOwner,
165  flg_secondOwner
166  };
167 
168 private:
169  void
170  init()
171  {
172  _first = _second = nullptr;
173  setFirstOwner(true);
174  setSecondOwner(true);
175  }
176  void
177  deInit()
178  {
179  clear();
180  }
181 
182 private:
183  Object* _first;
184  Object* _second;
185  byte_t _flags;
186 };
187 
189 
190 UTL_NS_END;
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
bool isSecondOwner() const
Return the owner flag for the second object.
Definition: Pair.h:142
void deInit()
De-initialize UTL++.
default representation (via getSerializeMode())
Definition: util.h:75
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
void clear()
Disassociate both objects.
Definition: Pair.h:56
void setFirstOwner(bool firstOwner)
Set the owner flag for the first object.
Definition: Pair.h:149
unsigned char byte_t
Unsigned character.
Definition: types.h:31
Character string.
Definition: String.h:31
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
void setSecond(const Object &second)
Set the second object (make a copy if isSecondOwner()).
Definition: Pair.h:125
Object * second() const
Return the second object.
Definition: Pair.h:88
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
Object * getFirst() const
Return the first object.
Definition: Pair.h:95
Object * getSecond() const
Return the second object.
Definition: Pair.h:102
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Stream I/O abstraction.
Definition: Stream.h:68
void setFirst(const Object &first)
Set the first object (make a copy if isFirstOwner()).
Definition: Pair.h:115
void setSecondOwner(bool secondOwner)
Set the owner flag for the second object.
Definition: Pair.h:156
Object * first() const
Return the first object.
Definition: Pair.h:81
Simple container for two objects.
Definition: Pair.h:25
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.
bool isFirstOwner() const
Return the owner flag for the first object.
Definition: Pair.h:135