3#ifndef STREAMBASE_TUPLE_H
4#define STREAMBASE_TUPLE_H
6#include "StreamBase.hpp"
8#include "Timestamp.hpp"
11#include "Exceptions.hpp"
13#include <NMSTL/platform.hpp>
14#include <streambase/impl/Memory.hpp>
15#include "TuplePrivate.hpp"
16#include "TupleExceptions.hpp"
17#include "FieldValue.hpp"
20#pragma warning( push )
21#pragma warning(disable:4127)
24SB_INTERNAL_FWD(TupleUtil);
25SB_INTERNAL_FWD(Errors);
34T Tuple_do_get(
const Tuple& tuple,
const Field &field);
36void Tuple_do_set(Tuple& tuple,
const Field &field, T value);
62 bool operator==(
const Tuple& rhs)
const;
70 if(!getSchema()) {
return; }
71 for(
size_t i = 0; i < getNumFields(); ++i) {
83 const Schema& s = getSchema();
109 template <
typename T> T
get(
const Field &f)
const {
110 return Tuple_do_get<T>(*
this, f);
112 template <
typename T> T get(
size_t i)
const {
113 return get<T>(getSchema().getField(i));
115 template <
typename T> T get(
const std::string &field_name)
const {
116 return get<T>(getSchema().getField(field_name));
129 template <
typename T>
void set(
const Field &field, T value) {
130 return Tuple_do_set<T>(*
this, field, value);
132 template <
typename T>
void set(
int fieldNum, T value) {
133 set(getSchema().getField(fieldNum), value);
135 template <
typename T>
void set(
const std::string &field_name, T value) {
136 set(getSchema().getField(field_name), value);
149 bool isNull(
size_t i)
const {
150 return isNull(getSchema().getField(i));
152 bool isNull(
const std::string& n)
const {
153 return isNull(getSchema().getField(n));
165 void setNull(
size_t i) { setNull(getSchema().getField(i)); }
166 void setNull(
const std::string& n) { setNull(getSchema().getField(n)); }
179 bool getBool(
size_t i)
const {
180 return getBool(getSchema().getField(i));
182 bool getBool(
const std::string& n)
const {
183 return getBool(getSchema().getField(n));
195 void setBool(
size_t i,
bool v) {
196 setBool(getSchema().getField(i), v);
198 void setBool(
const std::string& n,
bool v) {
199 setBool(getSchema().getField(n), v);
213 int getInt(
size_t i)
const {
214 return getInt(getSchema().getField(i));
216 int getInt(
const std::string& n)
const {
217 return getInt(getSchema().getField(n));
229 void setInt(
size_t i,
int v) {
230 setInt(getSchema().getField(i), v);
232 void setInt(
const std::string& n,
int v) {
233 setInt(getSchema().getField(n), v);
247 long long getLong(
size_t i)
const {
248 return getLong(getSchema().getField(i));
250 long long getLong(
const std::string& n)
const {
251 return getLong(getSchema().getField(n));
263 void setLong(
size_t i,
long long v) {
264 setLong(getSchema().getField(i), v);
266 void setLong(
const std::string& n,
long long v) {
267 setLong(getSchema().getField(n), v);
281 double getDouble(
size_t i)
const {
282 return getDouble(getSchema().getField(i));
284 double getDouble(
const std::string& n)
const {
285 return getDouble(getSchema().getField(n));
297 void setDouble(
size_t i,
double v) {
298 setDouble(getSchema().getField(i), v);
300 void setDouble(
const std::string& n,
double v) {
301 setDouble(getSchema().getField(n), v);
316 return getTimestamp(getSchema().getField(i));
318 Timestamp getTimestamp(
const std::string& n)
const {
319 return getTimestamp(getSchema().getField(n));
331 void setTimestamp(
size_t i,
const Timestamp& v) {
332 setTimestamp(getSchema().getField(i), v);
334 void setTimestamp(
const std::string& n,
const Timestamp& v) {
335 setTimestamp(getSchema().getField(n), v);
350 const std::string& getBlobBuffer(
size_t i)
const {
351 return getBlobBuffer(getSchema().getField(i));
353 const std::string& getBlobBuffer(
const std::string& n)
const {
354 return getBlobBuffer(getSchema().getField(n));
366 void setBlobBuffer(
size_t i,
const std::string& v) {
367 setBlobBuffer(getSchema().getField(i), v);
369 void setBlobBuffer(
const std::string& n,
const std::string& v) {
370 setBlobBuffer(getSchema().getField(n), v);
384 const std::vector<FieldValue>& getList(
size_t i)
const {
385 return getList(getSchema().getField(i));
387 const std::vector<FieldValue>& getList(
const std::string& n)
const {
388 return getList(getSchema().getField(n));
400 void setList(
size_t i,
const std::vector<FieldValue>& v) {
401 setList(getSchema().getField(i), v);
403 void setList(
const std::string& n,
const std::vector<FieldValue>& v) {
404 setList(getSchema().getField(n), v);
417 const std::string& getString(
size_t i)
const {
418 return getString(getSchema().getField(i));
420 const std::string& getString(
const std::string& n)
const {
421 return getString(getSchema().getField(n));
434 const std::string &value,
Flags flags = NONE);
435 void setString(
size_t field,
436 const char* data,
size_t len,
Flags flags = NONE) {
437 setString(field, std::string(data, len), flags);
439 void setString(
const std::string& field,
440 const char* data,
size_t len, Flags flags = NONE) {
441 setString(field, std::string(data, len), flags);
443 void setString(
const Field& field,
444 const char* data,
size_t len, Flags flags = NONE) {
445 setString(field, std::string(data, len), flags);
447 void setString(
size_t field_index,
448 const std::string &value, Flags flags = NONE) {
449 setString(getSchema().getField(field_index), value, flags);
451 void setString(
const std::string& field_name,
452 const std::string& value, Flags flags = NONE) {
453 setString(getSchema().getField(field_name), value, flags);
467 const Tuple& getTuple(
size_t i)
const {
468 return getTuple(getSchema().getField(i));
470 const Tuple& getTuple(
const std::string& n)
const {
471 return getTuple(getSchema().getField(n));
485 const Function& getFunction(
size_t i)
const {
486 return getFunction(getSchema().getField(i));
488 const Function& getFunction(
const std::string& n)
const {
489 return getFunction(getSchema().getField(n));
494 Tuple& getTupleMutable(
const Field &f);
506 void setTuple(
size_t i,
const Tuple& v) {
507 setTuple(getSchema().getField(i), v);
509 void setTuple(
const std::string& n,
const Tuple& v) {
510 setTuple(getSchema().getField(n), v);
523 void setFunction(
size_t i,
const Function& v) {
524 setFunction(getSchema().getField(i), v);
526 void setFunction(
const std::string& n,
const Function& v) {
527 setFunction(getSchema().getField(n), v);
562 bool hasHeader()
const {
return getSchema().hasHeader(); }
582 const std::string& null_string = getNullString())
const;
596 const std::string& null_string = getNullString(),
597 bool include_names =
false,
bool quote_all_strings =
false)
const;
599#ifndef DOXYGEN_INTERNAL_ONLY
602 void writeCSVString(std::ostream& out,
char delim,
char quote,
603 const std::string& null_string = getNullString())
const;
608 static std::string NULL_STRING =
"null";
612#ifndef DOXYGEN_INTERNAL_ONLY
625 size_t copyIntoBuffer(
void* buf,
size_t len,
bool byteswap)
const;
636 size_t setFromPackedBuffer(
637 const void* buf,
size_t buf_len,
bool byteswap,
const Schema& s);
639 static Tuple createFromPackedBuffer(
size_t* size_used,
640 const void* buf,
size_t buf_len,
bool byteswap,
const Schema& s);
644 std::shared_ptr<TuplePrivateImpl> _;
649 void ensureAllocated()
const;
652 const Tuple& resolveField(
const Field& f)
const;
656#define DO_ACCESSOR(type, getter) \
657template <> inline type Tuple_do_get<type>(const Tuple& t, const Field& f) { return t.get ## getter(f); } \
658template <> inline void Tuple_do_set<type>(Tuple& t, const Field& f, type v) { return t.set ## getter(f, v); }
660DO_ACCESSOR(
bool, Bool)
662DO_ACCESSOR(
long long, Long)
663DO_ACCESSOR(
double, Double)
664DO_ACCESSOR(std::string, String)
665DO_ACCESSOR(Timestamp, Timestamp)
671#pragma warning( pop )
676inline std::ostream& operator << (std::ostream& os,
const sb::Tuple& tuple) {
A value a Field may take on.
Definition: FieldValue.hpp:28
Information (name, type, and optionally size) about a field in a Schema.
Definition: Field.hpp:40
A type of tuple, containing zero or more fields (each encapsulated as a Schema::Field object).
Definition: Schema.hpp:62
size_t getNumFields() const
Returns the number of fields in the schema.
Class for representing moments in time and intervals at millisecond granularity.
Definition: Timestamp.hpp:39
Tuples are value types that can be copied and modified separately thus.
Definition: Tuple.hpp:47
std::string toDelimitedString(const std::string &delimiter, const std::string &null_string=getNullString(), bool include_names=false, bool quote_all_strings=false) const
Return a string value representing this tuple, separated by the given delimiter.
const FieldValue & getFieldValue(const Schema::Field &f) const
Takes a field with a path and returns the field at the target of that path.
std::string as_string_external(char delimiter=',', const std::string &null_string=getNullString()) const
Return a human-readable string value representing this tuple.
void setBool(const Field &f, bool v)
Set the value of a Boolean field.
int getInt(const Field &f) const
Return the value of an int field.
FieldValue & getFieldValue(const std::string &field_name)
Takes the name of a field in this tuple's schema, returns the corresponding field value.
size_t getNumFields() const
Definition: Tuple.hpp:82
void setTimestamp(const Field &f, const Timestamp &v)
Set the value of a Timestamp field.
const std::string & getString(const Field &f) const
Return the value of a string field.
void setInt(const Field &f, int v)
Set the value of an integer field.
T get(const Field &f) const
Return the value of the given field of type T.
Definition: Tuple.hpp:109
void setNull(const Field &f)
Set the null setting for field f.
const std::vector< FieldValue > & getList(const Schema::Field &f) const
Return the value of a LIST field.
FieldValue & getFieldValue(const Field &f)
Takes a field with a path and returns the field at the target of that path.
Flags
Flags that can be passed to setString().
Definition: Tuple.hpp:93
void setId(unsigned int tid)
set the id
void setFunction(const Field &f, const Function &v)
Set the value of a function field.
void setFieldValue(size_t field_num, const FieldValue &fv)
Takes the index of a field in this tuple's schema and sets the corresponding field value.
Tuple(const Schema &s)
Construct a tuple of the specified schema.
void verify() const
verify that the tuple is valid Throws StreamBaseException on invalid tuple
const FieldValue & getFieldValue(size_t field_num) const
Takes the index of a field in this tuple's schema, returns the corresponding field value.
void setFieldValue(const std::string &field_name, const FieldValue &fv)
Takes the name of a field in this tuple's schema and sets the corresponding field value.
Tuple(const Tuple &t)
Copy constructor.
void setLong(const Field &f, long long v)
Set the value of a long field.
const Tuple & getTuple(const Field &f) const
Return the value of a nested tuple field.
bool hasHeader() const
Definition: Tuple.hpp:562
void setString(const Field &field, const std::string &value, Flags flags=NONE)
Set the value of a string field.
long long getLong(const Field &f) const
Return the value of a long field.
void setDouble(const Field &f, double v)
Set the value of a double field.
bool getBool(const Field &f) const
Return the value of a Boolean field.
void setBlobBuffer(const Field &f, const std::string &v)
Set the value of a BLOB field.
static const std::string & getNullString()
Return the default string used to represent null, i.e., null.
Definition: Tuple.hpp:607
void setTuple(const Field &f, const Tuple &v)
Set the value of a nested tuple field.
const Schema & getSchema() const
bool isNull(const Field &f) const
Test the null setting for field f.
FieldValue & getFieldValue(size_t field_num)
Takes the index of a field in this tuple's schema, returns the corresponding field value.
const Function & getFunction(const Field &f) const
Return the value of a function field.
void setList(const Schema::Field &f, const std::vector< FieldValue > &v)
Set the value of a LIST field.
const std::string & getBlobBuffer(const Field &f) const
Return the value of a BLOB field.
double getDouble(const Field &f) const
Return the value of a double field.
Timestamp getTimestamp(const Field &f) const
Return the value of a Timestamp field.
void set(const Field &field, T value)
Set a field to a specific value.
Definition: Tuple.hpp:129
const FieldValue & getFieldValue(const std::string &field_name) const
Takes the name of a field in this tuple's schema, returns the corresponding field value.
std::string as_string() const
Return a human-readable string value representing this tuple in its entirety (including all header va...
void setFieldValue(const Field &f, const FieldValue &fv)
Takes a field with a path and sets the value of the field at the target of that path to that of the p...
void setSchema(const Schema &)
set the schema
unsigned int getId() const
void clear()
null all fields in the tuple
Definition: Tuple.hpp:69