42 #include "TypeUtils.h"
96 void link(
void *p,
const std::type_info &t){
120 Type::call<drain::typesetter>(*
this, t);
147 bool typeIsSet()
const {
return type != &
typeid(void); };
157 void put(
void *p,
const T & x)
const {
159 std::stringstream sstr;
165 throw std::runtime_error(std::string(
"Caster::put(void *, const T & ), type unset"));
172 void put(
const T & x)
const {
183 void put(
void *p,
const char * x)
const;
187 void put(
const char * x)
const {
194 T
get(
const void *p)
const {
195 throw std::runtime_error(std::string(
"Caster::get() unimplemented output type: ") +
typeid(T).name());
202 return get<T>(this->
ptr);
210 (*translatePtr)(c, ptrC,
ptr);
218 (*translatePtr)(c, c.
ptr, this->
ptr);
224 bool compare(
const void *
ptr,
const Caster &c,
const void *cPtr)
const {
225 return (*comparePtr)(
ptr, c, cPtr);
230 std::istream & fromStream(std::istream & istr,
const void *p)
const{
231 return (*fromStreamPtr)(istr, p);
236 std::ostream &
toOStream(std::ostream &ostr,
const void *p)
const {
260 void (* putBool)(
void *p,
const bool &x);
261 void (* putInt)(
void *p,
const int &x);
262 void (* putChar)(
void *p,
const char &x);
263 void (* putUChar)(
void *p,
const unsigned char &x);
264 void (* putUInt)(
void *p,
const unsigned int &x);
265 void (* putShort)(
void *p,
const short &x);
266 void (* putUShort)(
void *p,
const unsigned short &x);
267 void (* putLong)(
void *p,
const long &x);
268 void (* putULong)(
void *p,
const unsigned long &x);
269 void (* putFloat)(
void *p,
const float &x);
270 void (* putDouble)(
void *p,
const double &x);
273 char (* getChar)(
const void *p);
274 unsigned char (* getUChar)(
const void *p);
275 int (* getInt)(
const void *p);
276 unsigned int (* getUInt)(
const void *p);
277 short (* getShort)(
const void *p);
278 unsigned short (* getUShort)(
const void *p);
279 long (* getLong)(
const void *p);
280 unsigned long (* getULong)(
const void *p);
281 float (* getFloat)(
const void *p);
282 double (* getDouble)(
const void *p);
284 bool (* getBool)(
const void *p);
293 bool (* comparePtr)(
const void *,
const Caster &c,
const void *) =
nullptr;
297 std::ostream & (* toOStreamPtr)(std::ostream &ostr,
const void *p);
298 std::istream & (* fromStreamPtr)(std::istream &istr,
const void *p) =
nullptr;
305 template <
class T,
class F>
307 void putT(
void *p,
const T &x) {
308 *(F*)p =
static_cast<F
>(x);
327 std::stringstream sstr;
329 *(std::string*)p = sstr.str();
336 void putToCasterT(
void *p,
const T &x) {
352 template <
class T,
class F>
355 return static_cast<T
>(*(F*)p);
383 std::stringstream sstr;
384 sstr << *(
const std::string *)p;
409 *(F *)
ptr = c.
get<F>(ptrC);
426 return (*(F *)
ptr == c.
get<F>(cPtr));
434 std::ostream &
toOStreamT(std::ostream & ostr,
const void *p) {
442 std::ostream & toOStreamFloats(std::ostream & ostr,
const void *p){
445 throw std::runtime_error(
"Caster::toOStreamT<double>:: null ptr as argument");
496 void Caster::link(
Caster &c){
502 void Caster::updateType<void>();
505 void Caster::updateType<std::string>();
509 void Caster::updateType<Caster>();
515 if (!std::is_fundamental<F>::value){
516 throw std::runtime_error(std::string(__FILE__) + __FUNCTION__ +
':' +
typeid(F).name() +
": cannot convert to basic types");
525 putBool = & Caster::putT<bool,F>;
526 putInt = & Caster::putT<int,F>;
527 putChar = & Caster::putT<char,F>;
528 putUChar = & Caster::putT<unsigned char,F>;
529 putUInt = & Caster::putT<unsigned int,F>;
530 putShort = & Caster::putT<short,F>;
531 putUShort = & Caster::putT<unsigned short,F>;
532 putLong = & Caster::putT<long int,F>;
533 putULong = & Caster::putT<unsigned long,F>;
534 putFloat = & Caster::putT<float,F>;
535 putDouble = & Caster::putT<double,F>;
537 getBool = & Caster::getT<bool,F>;
538 getInt = & Caster::getT<int,F>;
539 getChar = & Caster::getT<char,F>;
540 getUChar = & Caster::getT<unsigned char,F>;
541 getUInt = & Caster::getT<unsigned int,F>;
542 getShort = & Caster::getT<short,F>;
543 getUShort = & Caster::getT<unsigned short,F>;
544 getLong = & Caster::getT<long,F>;
545 getULong = & Caster::getT<unsigned long,F>;
546 getFloat = & Caster::getT<float,F>;
547 getDouble = & Caster::getT<double,F>;
550 comparePtr = & Caster::compareT<F>;
553 fromStreamPtr = & Caster::fromStreamT<F>;
560 void Caster::put<bool>(
void *p,
const bool & x)
const {
562 (this->putBool)(p,x);
567 void Caster::put<char>(
void *p,
const char & x)
const {
568 (this->putChar)(p,x);
573 void Caster::put<unsigned char>(
void *p,
const unsigned char & x)
const {
574 (this->putUChar)(p,x);
578 void Caster::put<int>(
void *p,
const int & x)
const {
584 void Caster::put<unsigned int>(
void *p,
const unsigned int & x)
const {
585 (this->putUInt)(p,x);
589 void Caster::put<long>(
void *p,
const long & x)
const {
590 (this->putLong)(p,x);
594 void Caster::put<unsigned long>(
void *p,
const unsigned long & x)
const {
595 (this->putULong)(p,x);
599 void Caster::put<float>(
void *p,
const float& x)
const {
600 (this->putFloat)(p,x);
604 void Caster::put<double>(
void *p,
const double & x)
const {
605 (this->putDouble)(p,x);
609 void Caster::put<std::string>(
void *p,
const std::string & x)
const {
613 if (getType() ==
typeid(std::string))
614 *(std::string *)p = x;
615 else if (getType() ==
typeid(char)){
621 std::cerr <<
"Caster::put<std::string>() warning: single-char dst, assigned 1st of multi-char '"<< x <<
"'\n";
622 *(
char *)p = x.at(0);
638 bool Caster::get<bool>(
const void *p)
const {
639 return (this->getBool)(p);
644 char Caster::get<char>(
const void *p)
const {
645 return (this->getChar)(p);
649 unsigned char Caster::get<unsigned char>(
const void *p)
const {
650 return (this->getUChar)(p);
654 short Caster::get<short>(
const void *p)
const {
655 return (this->getShort)(p);
659 unsigned short Caster::get<unsigned short>(
const void *p)
const {
660 return (this->getUShort)(p);
664 int Caster::get<int>(
const void *p)
const {
665 return (this->getInt)(p);
669 unsigned int Caster::get<unsigned int>(
const void *p)
const {
670 return (this->getUInt)(p);
674 long Caster::get<long>(
const void *p)
const {
675 return (this->getLong)(p);
679 unsigned long Caster::get<unsigned long>(
const void *p)
const {
680 return (this->getULong)(p);
684 float Caster::get<float>(
const void *p)
const {
685 return (this->getFloat)(p);
689 double Caster::get<double>(
const void *p)
const {
690 return (this->getDouble)(p);
696 std::string Caster::get<std::string>(
const void *p)
const {
699 if (getType() ==
typeid(
void)){
700 static const std::string empty;
703 else if (getType() ==
typeid(
char)){
705 return std::string(1, *(
char *)p);
707 else if (getType() ==
typeid(
bool)){
708 return *(
const bool *)p ?
"true" :
"false";
710 else if (getType() ==
typeid(std::string)){
711 return *(
const std::string *)p;
714 std::stringstream sstr;
725 void Caster::translateT<void>(
const Caster &c,
const void *ptrC,
void *ptr){
731 void Caster::translateT<Caster>(
const Caster &c,
const void *ptrC,
void *ptr){
733 c2.translate(c, ptrC, c2.ptr);
740 double Caster::getFromVoidT<double>(
const void *p){
748 float Caster::getFromVoidT<float>(
const void *p){
758 bool Caster::compareT<void>(
const void *ptr,
const Caster &c,
const void *cPtr){
767 std::ostream & Caster::toOStreamT<void>(std::ostream &ostr,
const void *p){
775 std::ostream & Caster::toOStreamT<bool>(std::ostream &ostr,
const void *p){
776 ostr << (*(
const bool *)p ?
"true" :
"false");
783 std::ostream & Caster::toOStreamT<Caster>(std::ostream &ostr,
const void *p){
792 std::ostream & Caster::toOStreamT<float>(std::ostream & ostr,
const void *p){
793 return toOStreamFloats<float>(ostr, p);
799 std::ostream & Caster::toOStreamT<double>(std::ostream & ostr,
const void *p){
800 return toOStreamFloats<double>(ostr, p);
void updateType()
Sets pointers to the type-dependent conversion functions.
Definition: Caster.h:513
void put(const T &x) const
Write to internal pointer using put(this->ptr, x).
Definition: Caster.h:172
void put(const char *x) const
Write to internal pointer, calls put(this->ptr, x).
Definition: Caster.h:187
const std::type_info * type
Current type.
Definition: Caster.h:487
const std::type_info & getType() const
Returns type_info of the current type.
Definition: Caster.h:144
static T getFromVoidT(const void *p)
This default implementation is for integral numeric types, returns zero. Specialisation for floats re...
Definition: Caster.h:366
static std::ostream & toOStreamT(std::ostream &ostr, const void *p)
Output.
Definition: Caster.h:434
static std::istream & fromStreamT(std::istream &istr, const void *p)
Output.
Definition: Caster.h:478
static T getT(const void *p)
The main handler converting input to output.
Definition: Caster.h:354
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
std::ostream &(* toOStreamPtr)(std::ostream &ostr, const void *p)
Write to stream.
Definition: Caster.h:297
static bool compareT(const void *ptr, const Caster &c, const void *cPtr)
Checks equality through base type casting.
Definition: Caster.h:418
static void putToVoidT(void *p, const T &x)
Void - does nothing, regardless of input type.
Definition: Caster.h:315
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
Caster()
Default constructor. Leaves the type undefined.
Definition: Caster.h:78
std::ostream & toOStream(std::ostream &ostr, const void *p) const
Write data to output stream.
Definition: Caster.h:236
virtual bool requestType(const std::type_info &t)
Utility for derived classes, which may be able to adapt memory array type as well.
Definition: Caster.h:134
size_t byteSize
Size of target memory (target object) in bytes.
Definition: Caster.h:490
void translate(const Caster &c) const
Convert from str Caster.
Definition: Caster.h:216
void(* translatePtr)(const Caster &c, const void *ptrC, void *ptr)
Convert from str pointer and Caster.
Definition: Caster.h:289
void setType()
Sets pointers to the type-dependent conversion functions.
Definition: Caster.h:128
void translate(const Caster &c, const void *ptrC, void *ptr) const
Convert from str pointer and Caster.
Definition: Caster.h:208
static T getFromStringT(const void *p)
The main handler converting input to output.
Definition: Caster.h:381
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 void putToStringT(void *p, const T &x)
A handler for converting input to a std::string.
Definition: Caster.h:326
std::ostream & toOStream(std::ostream &ostr) const
Write data to output stream.
Definition: Caster.h:242
Caster(const Caster &c)
Copy constructor. Copies the type.
Definition: Caster.h:86
static void translateT(const Caster &c, const void *ptrC, void *ptr)
Casts from ptrC to ptr.
Definition: Caster.h:407
static void putT(void *p, const T &x)
Convert input of base type to internally applied base type.
Definition: Caster.h:307
Definition: DataSelector.cpp:1277