StreamBase C++ API  10.6.4.2
FieldValue.hpp
1 // Copyright (c) 2004-2023 TIBCO Software Inc. All rights reserved.
2 
3 #ifndef STREAMBASE_FIELDVALUE_HPP
4 #define STREAMBASE_FIELDVALUE_HPP
5 
6 #include "StreamBase.hpp"
7 #include "CompleteDataType.hpp"
8 #include "Timestamp.hpp"
9 
10 SB_NAMESPACE_BEGIN;
11 
12 class Tuple;
13 class Function;
14 
15 /** A value a Field may take on. Also possibly a list element.
16  * Has a type and some information of said type.
17  *
18  * FieldValues are immutable. Calling an inappropriate getter will cause an error.
19  *
20  * <code>
21  * FieldValue fv(5); // this creates a FieldValue of DataType::INT<br>
22  * cout << fv.getInt() << "\n";<br>
23  * cout << fv.getBool() << "\n";<br>
24  * </code>
25  *
26  * The first <code>cout</code> line will succeed; the second will throw an exception.
27  */
28 class FieldValue {
29 public:
30 #ifndef DOXYGEN_INTERNAL_ONLY
31  /// Create a null FieldValue of DataType::NONE
32  FieldValue();
33 #endif
34 
35  /// Destructor
36  ~FieldValue();
37 
38 #ifndef DOXYGEN_INTERNAL_ONLY
39  /// Create a null FieldValue of the given CompleteDataType
40  FieldValue(const CompleteDataType& cdt);
41 #endif
42 
43  /// DataType::BOOL constructor
44  FieldValue(bool value);
45 
46  /// DataType::INT constructor
47  FieldValue(int value);
48 
49  /// DataType::DOUBLE constructor
50  FieldValue(double value);
51 
52  /// DataType::LONG constructor
53  FieldValue(long long value);
54 
55  /// DataType::TIMESTAMP constructor
56  FieldValue(const Timestamp& value);
57 
58  /// DataType::STRING constructor
59  FieldValue(const char* value);
60 
61  /// DataType::STRING constructor
62  FieldValue(const std::string& value);
63 
64  /// DataType::STRING, DataType::BLOB, and DataType::CAPTURE constructor
65  FieldValue(const DataType& type, const std::string& value);
66 
67  /// DataType::TUPLE constructor
68  FieldValue(const Tuple& value);
69 
70  /// DataType::FUNCTION constructor
71  FieldValue(const Function& value);
72 
73  /// DataType::LIST constructor
74  FieldValue(const std::vector<FieldValue>& value);
75 
76  /// Create an empty list of the specified element type
77  FieldValue(const DataType& type, const CompleteDataType& elemType);
78 
79  /// Copy constructor
80  FieldValue(const FieldValue& fv);
81 
82  /// Assignment operator
83  const FieldValue& operator=(const FieldValue& fv);
84 
85  bool operator==(const FieldValue& t) const { return compare(t) == 0; }
86  bool operator!=(const FieldValue& t) const { return compare(t) != 0; }
87  bool operator<(const FieldValue& t) const { return compare(t) < 0; }
88  bool operator>(const FieldValue& t) const { return compare(t) > 0; }
89  bool operator<=(const FieldValue& t) const { return compare(t) <= 0; }
90  bool operator>=(const FieldValue& t) const { return compare(t) >= 0; }
91 
92  /**
93  * Compare the current FieldValue against the provided FieldValue.
94  *
95  * Null FieldValues are less than non-null FieldValues.
96  * Provides a total ordering over the set of possible FieldValues, so
97  * does not check types. Ordering of FieldValues of heterogeneous types
98  * is not guaranteed but is consistent.
99  *
100  * @return -1 if the current FieldValue is less than the provided FieldValue,
101  * 0 if the current is equal to the provided FieldValue,
102  * and 1 if the current is greater than the provided FieldValue.
103  */
104  int compare(const FieldValue& fv) const;
105 
106  /// Get the CompleteDataType of this FieldValue
107  const CompleteDataType& getCompleteType() const;
108 
109  /// Get the primitive DataType of this FieldValue
110  const DataType& getType() const;
111 
112 #ifndef DOXYGEN_INTERNAL_ONLY
113  size_t getSize() const;
114 #endif
115 
116  /**
117  * Returns true if this FieldValue and the provided FieldValue are of the same type
118  *
119  * "Of the same type" is defined as their CompleteDataType members being equivalent.
120  */
121  bool sameType(const FieldValue& fv) const;
122 
123  /// Return a string representation of this FieldValue.
124  std::string as_string() const;
125 
126 #ifndef DOXYGEN_INTERNAL_ONLY
127  std::string toDelimitedString(const std::string& delimiter,
128  const std::string& null_string,
129  bool include_names,
130  bool quote_all_strings) const;
131 
132  std::string toCSVString(char delim, const std::string& null_string, char quote) const;
133 #endif
134 
135  /// Return true if this FieldValue represents a null value.
136  bool isNull() const;
137 
138  /// Return the value of a boolean FieldValue.
139  /// Will throw an exception if this FieldValue is not of type DataType::BOOL.
140  bool getBool() const;
141 
142  /// Return the value of an integer FieldValue.
143  /// Will throw an exception if this FieldValue is not of type DataType::INT.
144  int getInt() const;
145 
146  /// Return the value of a double FieldValue.
147  /// Will throw an exception if this FieldValue is not of type DataType::DOUBLE.
148  double getDouble() const;
149 
150  /// Return the value of a long FieldValue.
151  /// Will throw an exception if this FieldValue is not of type DataType::LONG.
152  long long getLong() const;
153 
154  /// Return the value of a Timestamp FieldValue as a Timestamp.
155  /// Will throw an exception if this FieldValue is not of type DataType::TIMESTAMP.
156  Timestamp getTimestamp() const;
157 
158  /// Return the value of a string FieldValue.
159  /// Will throw an exception if this FieldValue is not of type DataType::STRING.
160  const std::string& getString() const;
161 
162  /// Return the value of a blob FieldValue as a string.
163  /// Will throw an exception if this FieldValue is not of type DataType::BLOB.
164  const std::string& getBlob() const;
165 
166  /// Return the value of a Tuple FieldValue.
167  /// Will throw an exception if this FieldValue is not of type DataType::TUPLE.
168  const Tuple& getTuple() const;
169 
170  /// Return the value of a Capture FieldValue, as a string
171  /// Will throw an exception if this FieldValue is not of type DataType::CAPTURE.
172  const std::string& getCapture() const;
173 
174  /// Return the value of a Function FieldValue
175  /// Will throw an exception if this FieldValue is not of type DataType::FUNCTION.
176  const Function& getFunction() const;
177 
178  /** Return the value of a Tuple FieldValue as a mutable Tuple.
179  * Will throw an exception if this FieldValue is not of type DataType::TUPLE.
180  *
181  * The caller takes on itself the responsibility of managing all concurrency.
182  */
183  Tuple& getTuple();
184 
185  /// Return the value of a List FieldValue.
186  /// Will throw an exception if this FieldValue is not of type DataType::LIST.
187  const std::vector<FieldValue>& getList() const;
188 
189 private:
190  /// Set this FieldValue to represent a null value of its current type.
191  void setNull(bool value = true);
192 
193  /// Set this FieldValue to be of type DataType::BOOL with the given boolean value.
194  void setBool(bool value);
195 
196  /// Set this FieldValue to be of type DataType::INT with the given integer value.
197  void setInt(int value);
198 
199  /// Set this FieldValue to be of type DataType::DOUBLE with the given double value.
200  void setDouble(double value);
201 
202  /// Set this FieldValue to be of type DataType::LONG with the given long value.
203  void setLong(long long value);
204 
205  /// Return the value of a Timestamp FieldValue as a long long.
206  /// Will error if this FieldValue is not of type DataType::TIMESTAMP.
207  long long getTimestampLong() const;
208  /// Set this FieldValue to be of type DataType::TIMESTAMP with the given Timestamp value.
209  void setTimestamp(const Timestamp& value);
210  /// Set this FieldValue to be of type DataType::TIMESTAMP with the given long long value.
211  void setTimestampLong(long long value);
212 
213  /// Set this FieldValue to be of type DataType::STRING with the given string value.
214  void setString(const std::string& value);
215 
216  /// Set this FieldValue to be of type DataType::BLOB with the given string as its blob value.
217  void setBlob(const std::string& value);
218 
219  void setVarLength(const CompleteDataType& cdt, const std::string& value);
220 
221  const std::string& getVarLength() const;
222 
223  /// Set this FieldValue to be of type DataType::TUPLE with the given Tuple value.
224  void setTuple(const Tuple& value);
225 
226  /// Set this FieldValue to be of the type DataType::FUNCTION with the given function value.
227  void setFunction(const Function& value);
228 
229  /// Set this FieldValue to be of type DataType::LIST, with element type elemType,
230  /// with the given vector as its list value. (Only way to make 0-element lists.)
231  void setList(const CompleteDataType& elemType, const std::vector<FieldValue>& value);
232 
233  /// Set this FieldValue to be of type DataType::LIST with the given vector as its list value.
234  /// Infers the element type from the type of the first FieldValue in the vector.
235  void setList(const std::vector<FieldValue>& value);
236 
237 
238  /** Member variables **/
239 
240  bool _isNull;
241  //BOOL, INT, LONG, DOUBLE, TIMESTAMP
242  union FieldUnion {
243  bool b;
244  int i;
245  long long l;
246  double d;
247  };
248  FieldUnion u;
249  //StreamBase data types not represented by C's built-in types go outside the union
250 
251  //default-constructed std::string's are cheap
252  //so we don't have to worry about the cost
253  //STRING, BLOB
254  std::string varlen;
255 
256  //default-constructed vectors are also cheap
257  //LIST
258  std::vector<FieldValue> elems;
259 
260  //pointers are very cheap
261  //TUPLE (null otherwise)
262  Tuple *nested_tuple;
263  //FUNCTION (null otherwise)
264  Function* function_value;
265 
266  //the complete (ie. fully-qualified) StreamBase type of a LIST's elements;
267  //default-constructed otherwise
268  CompleteDataType _cdt;
269 
270  std::string _captureName;
271 
272  void verifyCheck(const DataType& type) const;
273  void unsetNull(const CompleteDataType& cdt);
274 
275  friend class sb_internal::SchemaUtil;
276  friend class Tuple;
277  friend struct TuplePrivateImpl;
278 
279 };
280 
281 SB_NAMESPACE_END;
282 
283 #endif /* STREAMBASE_FIELDVALUE_HPP */