32#ifndef DRAIN_CASTABLE2
33#define DRAIN_CASTABLE2 "drain::Castable"
118 bool typeIsSet()
const {
119 return caster.typeIsSet();
147 return (caster.typeIsSet()) && (caster.
ptr !=
nullptr);
178 return (caster.
getType() ==
typeid(std::string));
256 static const std::string s(
"char-array");
260 return Type::call<drain::simpleName>(
getType());
285 throw std::runtime_error(
"Castable & operator=(const T *x): unsupported");
304 void assign(
const std::string &c){
310 void assign(
const char *c){
316 void assign(
const Castable &c){
322 void assign(
const T *p){
326 throw std::runtime_error(
"Castable & assign(const T *p): unsupported");
341 else if (typeIsSet()){
353 std::cerr << __FILE__ <<
':' << __FUNCTION__ <<
": arg:" << x <<
'\n';
354 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
417 appendToElementArray(x);
453 template <
class T,
class ...TT>
454 void append(
const T &arg,
const TT& ...args){
462 T get(
size_t i)
const {
464 std::cerr << __FILE__ <<
" index=" << i <<
", contents: '";
465 this->toStream(std::cerr);
467 throw std::runtime_error(
"Castable::get() index overflow");
477 throw std::runtime_error(
"Castable::front() called on empty variable");
486 throw std::runtime_error(
"Castable::back() called on empty variable");
498 throw std::runtime_error(
"Castable::front() called on empty variable");
501 throw std::runtime_error(
"Castable::pop_back() called on a referencing variable");
523 operator std::string()
const {
546 std::stringstream sstr;
552 return caster.
get<T>();
555 template <
class T,
size_t N>
563 bool operator==(
const Castable &c)
const {
567 if (!this->typeIsSet()){
568 return !c.typeIsSet();
575 if (this->
isString() || c.isString()){
576 return (this->toStr() == c.toStr());
585 if (! caster.compare(
getPtr(i), c.caster, c.getPtr(i))){
601 return (toStr() == s);
614 return getType() ==
typeid(void);
634 return (caster.
get<T>() == x);
638 template <
class T,
size_t N>
642 return (this->toStr() == x.toStr(
','));
645 for (
size_t i=0; i<N; ++i){
646 if (x[i] != this->get<T>(i)){
666 return ! this->operator==(x);
670 return ! this->operator==(x);
678 return (caster.
get<T>() < x);
684 return (caster.
get<T>() > x);
691 return (caster.
get<T>() <= x);
697 return (caster.
get<T>() >= x);
752 std::ostream & toStream(std::ostream & ostr = std::cout,
char separator=
'\0')
const;
754 std::istream & fromStream(std::istream & istr);
757 std::string toStr()
const;
760 void typeInfo(std::ostream & ostr)
const;
765 void info(std::ostream & ostr = std::cout)
const;
790 container.insert(container.end(), caster.
get<
typename T::value_type>(
getPtr(i)) );
800 void toMap(T & map,
char separator = 0,
char equalSign =
'=')
const {
806 typedef std::list<std::string> entryList;
810 for (entryList::const_iterator it = entries.begin(); it!=entries.end(); ++it){
812 typename T::key_type key;
813 typename T::mapped_type data;
821 typename T::key_type key;
822 typename T::mapped_type data;
823 std::stringstream sstr;
826 std::stringstream sstr2;
828 sstr2 << caster.
get<
typename T::mapped_type>(
getPtr(i));
848 const char *
getPtr(
size_t i = 0)
const {
863 bool requestType(
const std::type_info & t){
882 void updateSize(
size_t elems){
930 if ((!std::is_arithmetic<F>::value) && (
typeid(F)!=
typeid(std::string))){
931 throw std::runtime_error(std::string(__FUNCTION__) +
": unsupported type: " +
typeid(F).name());
942 void setPtr(
void *p,
const std::type_info &t,
size_t count=1){
967 template <
class F,
size_t N>
969 setPtr(tuple.begin(),
typeid(F), N);
994 void setPtr(Castable &c){
995 caster.link0((Caster &)c);
996 // caster.ptr = c.caster.ptr;
997 // caster.setType(c.getType());
998 elementCount = c.elementCount; // TODO
999 outputSeparator = c.outputSeparator; // ?
1024 std::stringstream sstr;
1034 std::string s = sstr.str();
1043 assignToCharArray(s);
1046 throw std::runtime_error(std::string(__FUNCTION__) +
": type is not a string: " +
typeid(T).name());
1052 void assignToString(
const T & x){
1075 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string");
1079 void assignToCharArray(
const std::string & s);
1082 void assignToCharArray(
const char *s){
1083 assignToCharArray(std::string(s));
1092 std::stringstream sstr;
1094 assignToCharArray(sstr.str());
1099 void appendToElementArray(
const char *s){
1100 appendToElementArray(std::string(s));
1104 void appendToElementArray(
const T & s){
1108 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
1114 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string (not implemented yet)");
1121 throw std::runtime_error(std::string(__FUNCTION__) +
": still empty after resize request(+1)");
1147 template <
class T,
size_t N>
1149 void assign(
const UniTuple<T,N> & tuple){
1168 std::stringstream sstr;
1176 assignToString(sstr.str());
1178 else if (caster.typeIsSet()){
1181 for (
typename T::const_iterator it = v.begin(); it != v.end(); ++it){
1182 appendToElementArray(*it);
1187 typename T::const_iterator it = v.begin();
1188 typename T::const_iterator itLast = it;
1205 throw std::runtime_error(std::string(__FILE__) +
": type unset, cannot assign");
1275std::ostream & operator<<(std::ostream &ostr,
const Castable &c){
1276 return c.toStream(ostr);
void pop_back()
Tries to remove the last element.
Definition Castable.h:496
void setPtr(UniTuple< F, N > &tuple)
Stores the pair as an array of two elements.
Definition Castable.h:968
void setPtr(void *p, const std::type_info &t, size_t count=1)
Sets the data pointer and its explicit type.
Definition Castable.h:942
void append(const std::set< T > &s)
Appends elements of a set.
Definition Castable.h:444
bool isCharArrayString() const
Returns true, if type is C char array and outputSepator is the null char.
Definition Castable.h:171
virtual bool isLinking() const
Return true, if the referenced variable is external. Inherited classes - like Variable - may have int...
Definition Castable.h:153
Castable & setFill(bool fill=true)
Set or unset filling (padding) an array if input set is smaller.
Definition Castable.h:231
void append(const std::list< T > &l)
Appends elements of std::list.
Definition Castable.h:430
size_t size() const
Alias, to comply with STL containers.
Definition Castable.h:223
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:613
void copyFormat(const Castable &c)
Copies array layout and formatting: separators, element count, fillArray flag.
Definition Castable.h:854
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:677
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:690
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:196
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:872
void assign(const std::vector< T > &v)
Copies elements of a vector.
Definition Castable.h:378
void append(const char *s)
Appends the string or appends the array by one element.
Definition Castable.h:423
bool isValid() const
Return true, if the pointer is set.
Definition Castable.h:146
char getOutputSeparator()
Definition Castable.h:747
virtual size_t getSize() const final
Returns the size in bytes of the target: (elementCount * elementSize)
Definition Castable.h:217
Castable & operator=(const char *c)
Definition Castable.h:277
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:371
virtual bool isVariable() const
Returns true, if the class contains an own data array.
Definition Castable.h:165
void setPtr(F &p)
Stores the pointer and its storage type F.
Definition Castable.h:929
char * getPtr(size_t i=0)
Returns pointer to the array of chars without validity check.
Definition Castable.h:843
size_t elementCount
Pointer to the data variable.
Definition Castable.h:1255
bool isStlString() const
Returns true, if type is std::string .
Definition Castable.h:177
std::string getTypeName() const
Returns the name of the current storage type.
Definition Castable.h:254
void assignToCharArray(const T &s)
Input anything to char array string.
Definition Castable.h:1091
void assignContainer(const T &v, bool append=false)
Assigns a STL Sequence, element by element.
Definition Castable.h:1155
char getInputSeparator()
Definition Castable.h:737
virtual const std::type_info & getType() const
Return the storage type (base type) of the referenced variable.
Definition Castable.h:135
void appendToString(const T &x)
Append to std::string or char array.
Definition Castable.h:1022
void assign(const T &x)
Copies an arbitrary base type or std::string value.
Definition Castable.h:334
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:207
void assign(const std::set< T > &s)
Copies elements of a set.
Definition Castable.h:386
bool operator==(const char *s) const
Compare as a character string.
Definition Castable.h:595
bool fillArray
If array, assigning a scalar will fill up the current array.
Definition Castable.h:877
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:294
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:778
bool operator>(const T &x) const
Compares a value with inner data.
Definition Castable.h:683
char outputSeparator
Element separator usein in writing and reading character strings (streams).
Definition Castable.h:1261
bool isString() const
Returns true, if type is C char array or std::string .
Definition Castable.h:183
void setPtr(void *p)
Stores the pointer and its storage type F. Assumes elementCount=1.
Definition Castable.h:921
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:437
virtual void setType(const std::type_info &t)
Sets the storage type. If a target value is available, use setPtr() directly.
Definition Castable.h:902
char inputSeparator
Element separator usein in reading a char sequence to an (numeric) array.
Definition Castable.h:1258
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:626
void setType()
Sets the storage type. If a target value is available, use setPtr() directly.
Definition Castable.h:895
void append(const T &x)
Appends the string or appends the array by one element.
Definition Castable.h:410
void reset()
Definition Castable.h:244
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:1006
void setOutputSeparator(char c=',')
Definition Castable.h:727
Castable & setSeparator(char c=',')
The character used between array elements in output stream.
Definition Castable.h:705
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:696
bool operator!=(const T &x) const
Compares a value to inner data.
Definition Castable.h:665
virtual bool requestSize(size_t elementCount)
Request to change the array size. For Castable (and Reference) does nothing and returns false.
Definition Castable.h:913
void setInputSeparator(char c=',')
Definition Castable.h:717
void assign(std::initializer_list< T > l)
Copies elements of a list.
Definition Castable.h:364
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
const std::type_info & getType() const
Returns type_info of the current type.
Definition Caster.h:144
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 std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:420
static const SprinterLayout plainLayout
Display plain values, concatenating them with comma (except for strings).
Definition Sprinter.h:209
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition TupleBase.h:287
Tuple of N elements of type T.
Definition UniTuple.h:65
Definition DataSelector.cpp:1277
T & operator-=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition Castable.h:1298
T & operator*=(T &x, const Castable &c)
Arithmetics: addition.
Definition Castable.h:1306
T & operator/=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition Castable.h:1314
T & operator+=(T &x, const Castable &c)
Arithmetics: addition.
Definition Castable.h:1290
Definition Sprinter.h:137
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition Type.h:558