StreamBase C++ API  7.7.8.2
DataType.hpp
1 // Copyright (c) 2004-2019 TIBCO Software Inc. All rights reserved.
2 
3 #ifndef STREAMBASE_DATA_TYPE_H
4 #define STREAMBASE_DATA_TYPE_H
5 
6 #include "StreamBase.hpp"
7 #include <string>
8 
9 #include "Exceptions.hpp"
10 
11 SB_NAMESPACE_BEGIN;
12 
13 /// A type of data in a tuple. Valid values are
14 ///
15 /// - DataType::NONE (sentinel "null" value)
16 /// - DataType::INT
17 /// - DataType::DOUBLE
18 /// - DataType::STRING
19 /// - DataType::TIMESTAMP
20 /// - DataType::BOOL
21 /// - DataType::LONG
22 /// - DataType::BLOB
23 /// - DataType::TUPLE
24 /// - DataType::LIST
25 /// - DataType::CAPTURE
26 /// - DataType::FUNCTION
27 ///
28 
29 class DataType {
30  public:
31 #ifndef DOXYGEN_INTERNAL_ONLY
32  enum Rep {
33  NONE = 0,
34  INT = 7,
35  DOUBLE = 8,
36  STRING = 10,
37  TIMESTAMP = 11,
38  BOOL = 12,
39  LONG = 13,
40  BLOB = 14,
41  TUPLE = 15,
42  LIST = 16,
43  CAPTURE = 17,
44  FUNCTION = 18
45  // No, we don't know why 1-6 and 9 are skipped. These are all left over
46  // from Aurora, so we assume historical reasons. Just keep incrementing.
47  };
48 #endif
49 
50  /// Construct a DataType equal to DataType::NONE.
51  DataType() : _type(NONE) {}
52 
53 #ifndef DOXYGEN_SKIP
54  /// Implicit constructor
55  DataType(Rep type) : _type(type) {}
56 
57  operator int() const { return _type; }
58 #endif
59 
60  /// Return a string representation of the data type.
61  std::string as_string() const {
62  char buf[24];
63 
64  sprintf(buf, "unknown[%d]", _type);
65  switch(_type) {
66  case NONE: return "none";
67  case INT: return "int";
68  case DOUBLE: return "double";
69  case STRING: return "string";
70  case TIMESTAMP: return "timestamp";
71  case LONG: return "long";
72  case BOOL: return "bool";
73  case BLOB: return "blob";
74  case TUPLE: return "tuple";
75  case LIST: return "list";
76  case CAPTURE: return "capture";
77  case FUNCTION: return "function";
78  default: return buf;
79  // can't use to_string, since this header is used by external clients also
80  }
81  }
82 
83  size_t getSize() const {
84  switch(_type) {
85  case LONG:
86  case TIMESTAMP:
87  case DOUBLE:
88  return 8;
89  case INT:
90  case BOOL:
91  return 4;
92  case BLOB:
93  case STRING:
94  case TUPLE:
95  case LIST:
96  case CAPTURE:
97  case FUNCTION:
98  return (size_t)-1;
99  case NONE:
100  default:
101  SB_Throw(IllegalArgumentException(SB_NEW_MESSAGE_CODE,
102  "Unrecognized or invalid data type: " + as_string()));
103  }
104  return 0; // To silence compiler warnings
105  }
106 
107  /// Return false if the type is variable-length (e.g., STRING or BLOB)
108  static bool isFixedSize(DataType type) { return type.getSize() != (size_t)-1; }
109 
110  private:
111  Rep _type;
112 };
113 
114 inline bool operator == (const DataType& a, DataType::Rep b) { return a == DataType(b); }
115 inline bool operator == (DataType::Rep b, const DataType& a) { return a == DataType(b); }
116 
117 
118 SB_NAMESPACE_END;
119 #endif