Castable-example.cpp

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.

If input is string:

See also
ReferenceMap
Variable
/*
Copyright 2001 - 2017 Markus Peura, Finnish Meteorological Institute (First.Last@fmi.fi)
This file is part of Rack for C++.
Rack is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
Rack is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
You should have received a copy of the GNU General Public License
along with Rack. If not, see <http://www.gnu.org/licenses/>.
*/
/*
REQUIRE: drain/{Caster,Castable,FlexibleVariable,Log,Reference,RegExp,Sprinter,String,TextStyle,Type,TypeUtils,Variable,VariableBase}.cpp
*/
#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"
using namespace drain;
template <class T>
static inline
void callback(drain::Castable & c){
//std::cout << "CastableTester::" << __FUNCTION__ << ':';
const T x = c;
c = x;
std::cout << "Native: " << TypeName<T>::str() << '(' << x << ')' << " -> Castable ";
c.info(std::cout);
//std::cout << " <- " << TypeName<T>::str() << '\n';
std::cout << '\n';
const T y = c;
if (x != y){
drain::Logger mout(__FILE__, __FUNCTION__);
mout.warn("Re-assign test failed: '", x, "' != '", y, "'");
}
//assert(("Re-assign test failed", x == y));
std::cout << '\n';
}
};
template <>
void CastableTester::callback<void>(drain::Castable & c){
}
void testCastable(drain::Castable & c){
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;
drain::Castable c(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 << "Storage type: string\n";
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 << "Storage type: int\n";
std::cout << "---------\n";
demo<int>(s);
demo<int>(k);
demo<int>(d);
//std::cout << "Storage type: double\n";
std::cout << "---------\n";
demo<double>(s);
demo<double>(k);
demo<double>(d);
}
std::cout << "---------\n\n";
/*
std::cout << "Storage type: vector<double>\n";
std::vector<double> v(3); v[0]=1; v[1]=2; v[2]=3;
Castable c(v);
c.info(std::cout); //, '\n');
c = std::vector<unsigned int>(2, 1234);
c.info(std::cout); //, '\n');
Referencer r;
r.link(v);
r = std::vector<double>(4, 678);
r.info(std::cout); //, '\n');
c = "mika,0.2,712.122,192";
c.info(std::cout); //, '\n');
r.link(c);
*/
std::cout << "Storage type: unituple<2,int>\n";
//std::pair<int,int> range(12,34);
//std::pair<int,int> range(12,34);
Range<int> range(12,34);
Castable cr(range.tuple());
//Castable cr(range);
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