1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#ifndef DATABUFFER_H
#define DATABUFFER_H
#include <ssiutil/ssidef.h>
#include <ssiutil/Tokenizer.h>
#include <ssiutil/ReferenceCountedPtr.h>
#include <memory>
#include <vector>
#include <stack>
#include <cstdlib>
/**
* Class to represent a data value. It supports simple values and structured
* aggregate values (arrays and struct/tuples).
*/
class DataValue;
typedef ReferenceCountedPtr<DataValue> DataValuePtr;
class DataValue : public virtual ReferenceCountedObject, /*
*/public ReferenceCountedObjectImpl {
public:
/**
* Smart pointer type for this object
*/
typedef DataValuePtr Ptr;
private:
/**
* Type of this data value
*/
enum Type {
TYPE_NULL,
TYPE_BOOLEAN,
TYPE_INTEGER,
TYPE_REAL,
TYPE_STRING,
/**
* Aggregates include tuples with named elements and arrays
*/
TYPE_AGGREGATE
} type;
/**
* Actual value
*/
union {
bool booleanValue;
int integerValue;
double realValue;
std::string *stringValue;
ReferenceTrackerVector<Ptr> *aggregateValue;
} value;
/**
* Name for this value
*/
std::string name;
/**
* Parse a table value
*/
static void parseTableValue(DataValue::Ptr dataValue, Tokenizer &t)
throw(ParseException);
public:
/**
* Constructor
*/
DataValue();
/**
* Constructor
* @param n name for this data value
*/
DataValue(const std::string &n);
/**
* Constructor
* @param dv data value to make a copy of
*/
DataValue(const DataValue &dv);
/**
* Constructor
* @param n name for this data value
* @param dv data value to make a copy of
*/
DataValue(const std::string &n, const DataValue &dv);
/**
* Destructor
*/
~DataValue();
/**
* Set the value of this value to be a copy of the indicated value
* @param dv data value to make a copy of
*/
void setValue(const DataValue &dv);
/**
* @return true if value is null or a scalar
*/
bool isScalar() const;
/**
* @return true if value is an aggregate
*/
bool isAggregate() const;
/**
* @return the cardinality of an aggregate value
*/
int cardinality() const;
/**
* @return the name of this value. Only makes sense if this is a member of
* a tuple.
*/
std::string getName() const;
/**
* Search for a particular indexed sub-value from an array or tuple
* @param index of sub-value
* @param create create the sub-value if it does not exist
* @return extracted value
*/
DataValue::Ptr searchValue(int index, bool create);
/**
* Search for a particular named sub-value from a tuple
* @param name of sub-value
* @param create create the sub-value if it does not exist
* @return extracted value
*/
DataValue::Ptr searchValue(const std::string &name, bool create);
/**
* Search for a particular indexed sub-value from an array or tuple
* @param index of sub-value
* @return extracted value
*/
DataValue::Ptr searchValue(int index) const;
/**
* Search for a particular named sub-value from a tuple
* @param name of sub-value
* @return extracted value
*/
DataValue::Ptr searchValue(const std::string &name) const;
/**
* Extract a particular sub-value from an array or tuple
* @param index index of sub-value
* @return extracted value
*/
DataValue::Ptr getValue(int index) const;
/**
* Retrieve a particular named sub-value
* @param name name of subvalue
* @return retrieved subvalue
*/
DataValue::Ptr getValue(const std::string &name) const;
//
// Setting and getting simple values
//
/**
* @return true if value is null
*/
bool isNullValue() const;
/**
* Set this value to null
*/
void setNullValue();
/**
* Set a sub-value to null
* @param index index of sub-value in array or tuple
*/
void setNullValue(int index);
/**
* Set a sub-value to null
* @param name name of sub-value in tuple
*/
void setNullValue(const std::string &name);
/**
* @return this value as a boolean
*/
bool getBooleanValue() const;
/**
* Set this value to a boolean value
*/
void setBooleanValue(bool value);
/**
* Set a sub-value to a boolean value
* @param index index of sub-value in array or tuple
*/
void setBooleanValue(int index, bool value);
/**
* Set a sub-value to a boolean value
* @param name name of sub-value in tuple
*/
void setBooleanValue(const std::string &name, bool value);
/**
* @return this value as a integer
*/
int getIntegerValue() const;
/**
* Set this value to an integer value
*/
void setIntegerValue(int value);
/**
* Set a sub-value to an integer value
* @param index index of sub-value in array or tuple
*/
void setIntegerValue(int index, int value);
/**
* Set a sub-value to an integer value
* @param name name of sub-value in tuple
*/
void setIntegerValue(const std::string &name, int value);
/**
* @return this value as an real
*/
double getRealValue() const;
/**
* Set this value to a real value
*/
void setRealValue(double value);
/**
* Set a sub-value to a real value
* @param index index of sub-value in array or tuple
*/
void setRealValue(int index, double value);
/**
* Set a sub-value to a real value
* @param name name of sub-value in tuple
*/
void setRealValue(const std::string &name, double value);
/**
* @return this value as a string
* @param ident indentation level for a subsequent lines in a
* multi-line result for aggregates
*/
std::string getStringValue(int indent = 0, bool compress_tables = false, bool macro_values = false) const;
#endif
|