An object that can be automatically casted to and from a basetype, array of basetype or std::string. Designed for objects returned by CastableIterator. Saves type info (like Caster) and also a pointer to external object. The object ie. the memory resource is provided by the user. Supports arrays.
#include <cassert>
#include <iostream>
#include "drain/Log.h"
#include "drain/Type.h"
#include "drain/UniTuple.h"
#include "drain/Castable.h"
#include "drain/Reference.h"
#include "drain/Variable.h"
#include "drain/FlexibleVariable.h"
#include "drain/util/Range.h"
template <class T>
static inline
const T x = c;
c = x;
std::cout << "Native: " << TypeName<T>::str() << '(' << x << ')' << " -> Castable ";
std::cout << '\n';
const T y = c;
if (x != y){
mout.
warn(
"Re-assign test failed: '", x,
"' != '", y,
"'");
}
std::cout << '\n';
}
};
template <>
}
drain::Type::call<CastableTester>(c, c.
getType());
}
template <class S, class T>
void demo(const T & value, char outputSeparator = ','){
std::cout << "Convert: " << TypeName<T>::str() << " -> " << TypeName<S>::str() << '\n';
S data;
c.setSeparator(outputSeparator);
std::cout << "Castable: ";
testCastable(c = value);
std::cout << "Reference: ";
r.setSeparator(outputSeparator);
r.link(data);
testCastable(r = value);
std::cout << "Variable: ";
v.setSeparator(outputSeparator);
v.setType(typeid(S));
testCastable(v = value);
std::cout << "FlexVar, copied: ";
testCastable(fv = data);
std::cout << "FlexVar, linked: ";
fv.link(data);
testCastable(fv);
std::cout << "Var=Cas: ";
v.reset();
testCastable(v = c);
}
int main(int argc, char **argv){
if (argc <= 1){
std::cout << "Demonstration of assigning values to and from Castables, Variables and Referencers.\n";
std::cout << "Usage:\n " << argv[0] << " <input1> <input2> ... ]\n";
std::cout << "Example:\n " << argv[0] << " 123.45 456.789E+6 Ă…land ... ]\n\n";
}
Range<double> reijo;
for (int i=1; i<argc; ++i){
const char *s = argv[i];
std::cout << "Arg " << i << ":\t" << s << '\n';
std::string str = s;
int k = atoi(s);
double d = atof(s);
std::cout << "---------\n";
demo<std::string>(s);
demo<std::string>(k);
demo<std::string>(d);
std::cout << "---------\n";
demo<char>(s,0);
demo<char>(k,0);
demo<char>(d,0);
std::cout << "---------\n";
demo<int>(s);
demo<int>(k);
demo<int>(d);
std::cout << "---------\n";
demo<double>(s);
demo<double>(k);
demo<double>(d);
}
std::cout << "---------\n\n";
std::cout << "Storage type: unituple<2,int>\n";
Range<int> range(12,34);
Castable cr(range.tuple());
std::cout << cr << '\n';
cr.info();
std::cout << '\n';
cr = 123;
cr << 5 << 6;
cr.info();
std::cout << cr << '\n';
return 0;
}
Definition: Castable.h:76
virtual const std::type_info & getType() const
Return the storage type (base type) of the referenced variable.
Definition: Castable.h:135
virtual void info(std::ostream &ostr=std::cout) const
Print value, type and element count.
Definition: Castable.cpp:233
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
VariableT is a final class applied through typedefs Variable, Reference and FlexibleVariable.
Definition: VariableT.h:87
Definition: DataSelector.cpp:1277
DRAIN_VARIABLE Variable
Value container supporting dynamic type.
Definition: Variable.h:63
Checks if the value assigned to a drain::Castable stays same when reassigned.
Definition: Castable-example.cpp:48