32 #ifndef DRAIN_CASTABLE2
33 #define DRAIN_CASTABLE2 "drain::Castable"
118 bool typeIsSet()
const {
119 return caster.typeIsSet();
142 return (caster.typeIsSet()) && (caster.
ptr !=
nullptr);
173 return (caster.
getType() ==
typeid(std::string));
244 static const std::string s(
"char-array");
248 return Type::call<drain::simpleName>(
getType());
275 throw std::runtime_error(
"Castable & operator=(const T *x): unsupported");
306 void assign(
const std::string &c){
312 void assign(
const char *c){
318 void assign(
const Castable &c){
324 void assign(
const T *p){
328 throw std::runtime_error(
"Castable & assign(const T *p): unsupported");
343 else if (typeIsSet()){
355 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
425 appendToElementArray(x);
465 T get(
size_t i)
const {
467 throw std::runtime_error(
"Castable::get() index overflow");
477 operator std::string()
const {
500 std::stringstream sstr;
506 return caster.
get<T>();
509 template <
class T,
size_t N>
517 bool operator==(
const Castable &c)
const {
521 if (!this->typeIsSet()){
522 return !c.typeIsSet();
529 if (this->
isString() || c.isString()){
530 return (this->toStr() == c.toStr());
539 if (! caster.compare(
getPtr(i), c.caster, c.getPtr(i))){
555 return (toStr() == s);
568 return getType() ==
typeid(void);
588 return (caster.
get<T>() == x);
592 template <
class T,
size_t N>
596 return (this->toStr() == x.toStr(
','));
599 for (
size_t i=0; i<N; ++i){
600 if (x[i] != this->get<T>(i)){
620 return ! this->operator==(x);
624 return ! this->operator==(x);
632 return (caster.
get<T>() < x);
638 return (caster.
get<T>() > x);
645 return (caster.
get<T>() <= x);
651 return (caster.
get<T>() >= x);
706 std::ostream & toStream(std::ostream & ostr = std::cout,
char separator=
'\0')
const;
708 std::istream & fromStream(std::istream & istr);
711 std::string toStr()
const;
714 void typeInfo(std::ostream & ostr)
const;
719 void info(std::ostream & ostr = std::cout)
const;
744 container.insert(container.end(), caster.
get<
typename T::value_type>(
getPtr(i)) );
754 void toMap(T & map,
char separator = 0,
char equalSign =
'=')
const {
760 typedef std::list<std::string> entryList;
764 for (entryList::const_iterator it = entries.begin(); it!=entries.end(); ++it){
766 typename T::key_type key;
767 typename T::mapped_type data;
775 typename T::key_type key;
776 typename T::mapped_type data;
777 std::stringstream sstr;
780 std::stringstream sstr2;
782 sstr2 << caster.
get<
typename T::mapped_type>(
getPtr(i));
802 const char *
getPtr(
size_t i = 0)
const {
817 bool requestType(
const std::type_info & t){
836 void updateSize(
size_t elems){
884 if ((!std::is_arithmetic<F>::value) && (
typeid(F)!=
typeid(std::string))){
885 throw std::runtime_error(std::string(__FUNCTION__) +
": unsupported type: " +
typeid(F).name());
896 void setPtr(
void *p,
const std::type_info &t,
size_t count=1){
921 template <
class F,
size_t N>
923 setPtr(tuple.begin(),
typeid(F), N);
978 std::stringstream sstr;
988 std::string s = sstr.str();
997 assignToCharArray(s);
1000 throw std::runtime_error(std::string(__FUNCTION__) +
": type is not a string: " +
typeid(T).name());
1006 void assignToString(
const T & x){
1029 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string");
1033 void assignToCharArray(
const std::string & s);
1036 void assignToCharArray(
const char *s){
1037 assignToCharArray(std::string(s));
1046 std::stringstream sstr;
1048 assignToCharArray(sstr.str());
1053 void appendToElementArray(
const char *s){
1054 appendToElementArray(std::string(s));
1058 void appendToElementArray(
const T & s){
1062 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
1068 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string (not implemented yet)");
1075 throw std::runtime_error(std::string(__FUNCTION__) +
": still empty after resize request(+1)");
1101 template <
class T,
size_t N>
1103 void assign(
const UniTuple<T,N> & tuple){
1122 std::stringstream sstr;
1130 assignToString(sstr.str());
1132 else if (caster.typeIsSet()){
1135 for (
typename T::const_iterator it = v.begin(); it != v.end(); ++it){
1136 appendToElementArray(*it);
1141 typename T::const_iterator it = v.begin();
1142 typename T::const_iterator itLast = it;
1159 throw std::runtime_error(std::string(__FILE__) +
": type unset, cannot assign");
1229 std::ostream & operator<<(std::ostream &ostr,
const Castable &c){
1230 return c.toStream(ostr);
Definition: Castable.h:76
void setPtr(UniTuple< F, N > &tuple)
Stores the pair as an array of two elements.
Definition: Castable.h:922
void setPtr(void *p, const std::type_info &t, size_t count=1)
Sets the data pointer and its explicit type.
Definition: Castable.h:896
void append(const std::set< T > &s)
Appends elements of a set.
Definition: Castable.h:458
Castable & setSeparator(char c=',')
The character used between array elements in output stream.
Definition: Castable.h:659
bool isCharArrayString() const
Returns true, if type is C char array and outputSepator is the null char.
Definition: Castable.h:166
virtual bool isLinking() const
Return true, if the referenced variable is external. Inherited classes - like Variable - may have int...
Definition: Castable.h:148
virtual const std::type_info & getType() const
Return the storage type (base type) of the referenced variable.
Definition: Castable.h:135
void append(const std::list< T > &l)
Appends elements of std::list.
Definition: Castable.h:441
Castable & operator=(const Castable &c)
Copies the value referred to by Castable. Notice that if type is unset (void), no operation is perfor...
Definition: Castable.h:284
bool operator==(const nullptr_t ptr) const
Experimental. Notice that if type is set, false is returned also for an empty array.
Definition: Castable.h:567
void copyFormat(const Castable &c)
Copies array layout and formatting: separators, element count, fillArray flag.
Definition: Castable.h:808
void assignString(const std::string &s)
Input type specific assign operations.
Definition: Castable.cpp:365
bool operator<(const T &x) const
Compares a value to inner data.
Definition: Castable.h:631
Castable(const Castable &c)
Copy constructor: copies the layout and the pointer to the target.
Definition: Castable.h:89
bool operator<=(const T &x) const
Compares a value to inner data.
Definition: Castable.h:644
Castable & setFill(bool fill=true)
Set or unset filling (padding) an array if input set is smaller.
Definition: Castable.h:219
void typeInfo(std::ostream &ostr) const
Writes a string of type indentifier char and size in bytes, for example [C@8] for unsigned char and [...
Definition: Castable.cpp:220
virtual size_t getElementCount() const
Returns the length of the array, the number of elements in this entry.
Definition: Castable.h:190
virtual void info(std::ostream &ostr=std::cout) const
Print value, type and element count.
Definition: Castable.cpp:233
virtual bool suggestType(const std::type_info &t)
Request to change in type. For Castable, simply returns true if the current type was requested.
Definition: Castable.h:826
void assign(const std::vector< T > &v)
Copies elements of a vector.
Definition: Castable.h:384
void append(const char *s)
Appends the string or appends the array by one element.
Definition: Castable.h:433
bool isValid() const
Return true, if the pointer is set.
Definition: Castable.h:141
char getOutputSeparator()
Definition: Castable.h:701
bool empty() const
Returns true, if string is empty or array size is zero. (In future, semantics may change: 0 for scala...
Definition: Castable.cpp:36
void assign(const std::list< T > &l)
Copies elements of a list.
Definition: Castable.h:375
virtual bool isVariable() const
Returns true, if the class contains an own data array.
Definition: Castable.h:160
void setPtr(F &p)
Stores the pointer and its storage type F.
Definition: Castable.h:883
size_t elementCount
Pointer to the data variable.
Definition: Castable.h:1209
bool isStlString() const
Returns true, if type is std::string .
Definition: Castable.h:172
std::string getTypeName() const
Returns the name of the current storage type.
Definition: Castable.h:242
void assignToCharArray(const T &s)
Input anything to char array string.
Definition: Castable.h:1045
void assignContainer(const T &v, bool append=false)
Assigns a STL Sequence, element by element.
Definition: Castable.h:1109
char getInputSeparator()
Definition: Castable.h:691
void appendToString(const T &x)
Append to std::string or char array.
Definition: Castable.h:976
void assign(const T &x)
Copies an arbitrary base type or std::string value.
Definition: Castable.h:336
Castable & assignCastable(const Castable &c)
Copy data from Castable. Perhaps copy size and type, too.
Definition: Castable.cpp:256
size_t getElementSize() const
Return the size of the current storage type, in bytes.
Definition: Castable.h:201
void assign(const std::set< T > &s)
Copies elements of a set.
Definition: Castable.h:393
bool operator==(const char *s) const
Compare as a character string.
Definition: Castable.h:549
char * getPtr(size_t i=0)
Returns pointer to the array of chars without validity check.
Definition: Castable.h:797
bool fillArray
If array, assigning a scalar will fill up the current array.
Definition: Castable.h:831
void toSequence(T &container, char separator=0) const
Converts data to a STL Sequence, for example std::set, std::list or std::vector .
Definition: Castable.h:732
bool operator>(const T &x) const
Compares a value with inner data.
Definition: Castable.h:637
char outputSeparator
Element separator usein in writing and reading character strings (streams).
Definition: Castable.h:1215
bool isString() const
Returns true, if type is C char array or std::string .
Definition: Castable.h:178
void setPtr(void *p)
Stores the pointer and its storage type F. Assumes elementCount=1.
Definition: Castable.h:875
const char * getCharArray() const
Returns pointer to the array of chars, checks validity first.
Definition: Castable.cpp:93
void append(const std::vector< T > &v)
Appends elements of a vector.
Definition: Castable.h:450
virtual void setType(const std::type_info &t)
Sets the storage type. If a target value is available, use setPtr() directly.
Definition: Castable.h:856
char inputSeparator
Element separator usein in reading a char sequence to an (numeric) array.
Definition: Castable.h:1212
Castable & operator=(const char *c)
Definition: Castable.h:267
void clear()
Clears strings, or for scalars and arrays, sets all the values to zero. Does not change type.
Definition: Castable.cpp:81
bool operator==(const T &x) const
Compares a value to internal data.
Definition: Castable.h:580
void setType()
Sets the storage type. If a target value is available, use setPtr() directly.
Definition: Castable.h:849
void append(const T &x)
Appends the string or appends the array by one element.
Definition: Castable.h:419
void reset()
Definition: Castable.h:232
void relink(Castable &c)
Copies the link and element count.
Definition: Castable.cpp:69
void castElement(size_t i, const Caster &c, void *p) const
Let Caster c convert my element #i to target *p.
Definition: Castable.h:960
void setOutputSeparator(char c=',')
Definition: Castable.h:681
Castable(F &p)
Constructor for an object pointing to a variable.
Definition: Castable.h:96
bool operator>=(const T &x) const
Compares a value with inner data.
Definition: Castable.h:650
virtual size_t getSize() const
Returns the size in bytes of the target: (elementCount * elementSize)
Definition: Castable.h:211
bool operator!=(const T &x) const
Compares a value to inner data.
Definition: Castable.h:619
virtual bool requestSize(size_t elementCount)
Request to change the array size. For Castable (and Reference) does nothing and returns false.
Definition: Castable.h:867
void setInputSeparator(char c=',')
Definition: Castable.h:671
void assign(std::initializer_list< T > l)
Copies elements of a list.
Definition: Castable.h:366
const std::type_info & getType() const
Returns type_info of the current type.
Definition: Caster.h:144
T get(const void *p) const
Default implementation throws an error. See specialized implementation below.
Definition: Caster.h:194
void unsetType()
calls void setType<void>().
Definition: Caster.cpp:134
size_t getElementSize() const
Returns the size of the base type (size of an element, not of element array).
Definition: Caster.h:151
void setType(const std::type_info &t)
Calls setType<T>() for which typeid(T) = t.
Definition: Caster.h:119
void translate(const Caster &c, const void *ptrC, void *ptr) const
Convert from str pointer and Caster.
Definition: Caster.h:208
void * ptr
Future member: enables setting Caster type.
Definition: Caster.h:247
void put(void *p, const T &x) const
Default conversion (for unconventional types). Uses std::stringstream for conversion.
Definition: Caster.h:157
static const SprinterLayout plainLayout
Display plain values, concatenating them with comma (except for strings).
Definition: Sprinter.h:209
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition: TupleBase.h:244
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
T & operator*=(T &x, const Castable &c)
Arithmetics: addition.
Definition: Castable.h:1260
T & operator+=(T &x, const Castable &c)
Arithmetics: addition.
Definition: Castable.h:1244
T & operator/=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition: Castable.h:1268
T & operator-=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition: Castable.h:1252
Definition: Sprinter.h:137
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition: Type.h:558