32#ifndef DRAIN_CASTABLE2
33#define DRAIN_CASTABLE2 "drain::Castable"
47#include "StringTools.h"
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");
347 else if (typeIsSet()){
359 std::cerr << __FILE__ <<
':' << __FUNCTION__ <<
": arg type:" <<
typeid(T).name() <<
'\n';
360 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
423 appendToElementArray(x);
459 template <
class T,
class ...TT>
460 void append(
const T &arg,
const TT& ...args){
468 T get(
size_t i)
const {
470 std::cerr << __FILE__ <<
" index=" << i <<
", contents: '";
471 this->toStream(std::cerr);
473 throw std::runtime_error(
"Castable::get() index overflow");
483 throw std::runtime_error(
"Castable::front() called on empty variable");
492 throw std::runtime_error(
"Castable::back() called on empty variable");
504 throw std::runtime_error(
"Castable::front() called on empty variable");
507 throw std::runtime_error(
"Castable::pop_back() called on a referencing variable");
529 operator std::string()
const {
552 std::stringstream sstr;
558 return caster.
get<T>();
561 template <
class T,
size_t N>
569 bool operator==(
const Castable &c)
const {
573 if (!this->typeIsSet()){
574 return !c.typeIsSet();
581 if (this->
isString() || c.isString()){
582 return (this->toStr() == c.toStr());
591 if (! caster.compare(
getPtr(i), c.caster, c.getPtr(i))){
607 return (toStr() == s);
620 return getType() ==
typeid(void);
640 return (caster.
get<T>() == x);
644 template <
class T,
size_t N>
648 return (this->toStr() == x.toStr(
','));
651 for (
size_t i=0; i<N; ++i){
652 if (x[i] != this->get<T>(i)){
672 return ! this->operator==(x);
676 return ! this->operator==(x);
684 return (caster.
get<T>() < x);
690 return (caster.
get<T>() > x);
697 return (caster.
get<T>() <= x);
703 return (caster.
get<T>() >= x);
758 std::ostream & toStream(std::ostream & ostr = std::cout,
char separator=
'\0')
const;
760 std::istream & fromStream(std::istream & istr);
763 std::string toStr()
const;
766 void typeInfo(std::ostream & ostr)
const;
771 void info(std::ostream & ostr = std::cout)
const;
796 container.insert(container.end(), caster.
get<
typename T::value_type>(
getPtr(i)) );
806 void toMap(T & map,
char separator = 0,
char equalSign =
'=')
const {
812 typedef std::list<std::string> entryList;
816 for (entryList::const_iterator it = entries.begin(); it!=entries.end(); ++it){
818 typename T::key_type key;
819 typename T::mapped_type data;
827 typename T::key_type key;
828 typename T::mapped_type data;
829 std::stringstream sstr;
832 std::stringstream sstr2;
834 sstr2 << caster.
get<
typename T::mapped_type>(
getPtr(i));
854 const char *
getPtr(
size_t i = 0)
const {
869 bool requestType(
const std::type_info & t){
888 void updateSize(
size_t elems){
936 if ((!std::is_arithmetic<F>::value) && (
typeid(F)!=
typeid(std::string))){
937 throw std::runtime_error(std::string(__FUNCTION__) +
": unsupported type: " +
typeid(F).name());
948 void setPtr(
void *p,
const std::type_info &t,
size_t count=1){
973 template <
class F,
size_t N>
975 setPtr(tuple.begin(),
typeid(F), N);
1003 void setPtr(Castable &c){
1004 caster.link0((Caster &)c);
1005 // caster.ptr = c.caster.ptr;
1006 // caster.setType(c.getType());
1007 elementCount = c.elementCount; // TODO
1008 outputSeparator = c.outputSeparator; // ?
1033 std::stringstream sstr;
1043 std::string s = sstr.str();
1052 assignToCharArray(s);
1055 throw std::runtime_error(std::string(__FUNCTION__) +
": type is not a string: " +
typeid(T).name());
1061 void assignToString(
const T & x){
1084 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string");
1088 void assignToCharArray(
const std::string & s);
1091 void assignToCharArray(
const char *s){
1092 assignToCharArray(std::string(s));
1101 std::stringstream sstr;
1103 assignToCharArray(sstr.str());
1108 void appendToElementArray(
const char *s){
1109 appendToElementArray(std::string(s));
1113 void appendToElementArray(
const T & s){
1117 throw std::runtime_error(std::string(__FUNCTION__) +
": type is unset");
1123 throw std::runtime_error(std::string(__FUNCTION__) +
": type is std::string (not implemented yet)");
1130 throw std::runtime_error(std::string(__FUNCTION__) +
": still empty after resize request(+1)");
1156 template <
class T,
size_t N>
1158 void assign(
const UniTuple<T,N> & tuple){
1177 std::stringstream sstr;
1185 assignToString(sstr.str());
1187 else if (caster.typeIsSet()){
1190 for (
typename T::const_iterator it = v.begin(); it != v.end(); ++it){
1191 appendToElementArray(*it);
1196 typename T::const_iterator it = v.begin();
1197 typename T::const_iterator itLast = it;
1214 throw std::runtime_error(std::string(__FILE__) +
": type unset, cannot assign");
1284std::ostream & operator<<(std::ostream &ostr,
const Castable &c){
1285 return c.toStream(ostr);
1329DRAIN_TYPENAME(Castable);
void pop_back()
Tries to remove the last element.
Definition Castable.h:502
void setPtr(UniTuple< F, N > &tuple)
Stores the pair as an array of two elements.
Definition Castable.h:974
void setPtr(void *p, const std::type_info &t, size_t count=1)
Sets the data pointer and its explicit type.
Definition Castable.h:948
void append(const std::set< T > &s)
Appends elements of a set.
Definition Castable.h:450
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:436
size_t size() const
Alias, to comply with STL containers.
Definition Castable.h:223
void copyFormat(const Castable &c)
Copies array layout and formatting: separators, element count, fillArray flag.
Definition Castable.h:860
void assignString(const std::string &s)
Input type specific assign operations.
Definition Castable.cpp:376
bool operator==(const std::nullptr_t ptr) const
Experimental. Notice that if type is set, false is returned also for an empty array.
Definition Castable.h:619
bool operator<(const T &x) const
Compares a value to inner data.
Definition Castable.h:683
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:696
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:231
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:244
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:878
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:429
bool isValid() const
Return true, if the pointer is set.
Definition Castable.h:146
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
char getOutputSeparator() const
Definition Castable.h:753
void assign(const std::list< T > &l)
Copies elements of a list.
Definition Castable.h:377
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:935
char * getPtr(size_t i=0)
Returns pointer to the array of chars without validity check.
Definition Castable.h:849
size_t elementCount
Pointer to the data variable.
Definition Castable.h:1264
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:1100
void assignContainer(const T &v, bool append=false)
Assigns a STL Sequence, element by element.
Definition Castable.h:1164
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:1031
void assign(const T &x)
Copies an arbitrary base type or std::string value.
Definition Castable.h:340
Castable & assignCastable(const Castable &c)
Copy data from Castable. Perhaps copy size and type, too.
Definition Castable.cpp:267
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:392
bool operator==(const char *s) const
Compare as a character string.
Definition Castable.h:601
char getInputSeparator() const
Definition Castable.h:743
bool fillArray
If array, assigning a scalar will fill up the current array.
Definition Castable.h:883
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:784
bool operator>(const T &x) const
Compares a value with inner data.
Definition Castable.h:689
char outputSeparator
Element separator usein in writing and reading character strings (streams).
Definition Castable.h:1270
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:927
const char * getCharArray() const
Returns pointer to the array of chars, checks validity first.
Definition Castable.cpp:104
void append(const std::vector< T > &v)
Appends elements of a vector.
Definition Castable.h:443
virtual void setType(const std::type_info &t)
Sets the storage type. If a target value is available, use setPtr() directly.
Definition Castable.h:908
char inputSeparator
Element separator usein in reading a char sequence to an (numeric) array.
Definition Castable.h:1267
void clear()
Clears strings, or for scalars and arrays, sets all the values to zero. Does not change type.
Definition Castable.cpp:92
bool operator==(const T &x) const
Compares a value to internal data.
Definition Castable.h:632
void setType()
Sets the storage type. If a target value is available, use setPtr() directly.
Definition Castable.h:901
void append(const T &x)
Appends the string or appends the array by one element.
Definition Castable.h:416
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:1015
void setOutputSeparator(char c=',')
Definition Castable.h:733
Castable & setSeparator(char c=',')
The character used between array elements in output stream.
Definition Castable.h:711
void assign(std::nullptr_t)
Assigning nullptr resets the castable (unsets type and clears content).
Definition Castable.h:331
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:702
bool operator!=(const T &x) const
Compares a value to inner data.
Definition Castable.h:671
virtual bool requestSize(size_t elementCount)
Request to change the array size. For Castable (and Reference) does nothing and returns false.
Definition Castable.h:919
void setInputSeparator(char c=',')
Definition Castable.h:723
void assign(std::initializer_list< T > l)
Copies elements of a list.
Definition Castable.h:370
T get(const void *p) const
Default implementation throws an error. See specialized implementation below.
Definition Caster.h:207
void unsetType()
calls void setType(void)().
Definition Caster.cpp:153
const std::type_info & getType() const
Returns type_info of the current type.
Definition Caster.h:154
size_t getElementSize() const
Returns the size of the base type (size of an element, not of element array).
Definition Caster.h:161
void setType(const std::type_info &t)
Calls setType<T>() for which typeid(T) = t.
Definition Caster.h:129
void translate(const Caster &c, const void *ptrC, void *ptr) const
Convert from str pointer and Caster.
Definition Caster.h:221
void * ptr
Future member: enables setting Caster type.
Definition Caster.h:260
void put(void *p, const T &x) const
Default conversion (for unconventional types). Uses std::stringstream for conversion.
Definition Caster.h:167
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
static const SprinterLayout plainLayout
Display plain values, concatenating them with comma (except for strings).
Definition Sprinter.h:212
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
T & operator-=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition Castable.h:1307
T & operator*=(T &x, const Castable &c)
Arithmetics: addition.
Definition Castable.h:1315
T & operator/=(T &x, const Castable &c)
Arithmetics: subtraction.
Definition Castable.h:1323
T & operator+=(T &x, const Castable &c)
Arithmetics: addition.
Definition Castable.h:1299
Definition Sprinter.h:136