47#if __cplusplus < 201703L
48#ifndef DRAIN_NULLPTR_STREAM_COMPAT
49#define DRAIN_NULLPTR_STREAM_COMPAT
50inline std::ostream &
operator<<(std::ostream & os, std::nullptr_t) {
51 return os << static_cast<const void *>(
nullptr);
106 void link(
void *p,
const std::type_info &t){
130 Type::call<drain::Type::typeSetter>(*
this, t);
157 bool typeIsSet()
const {
return type != &
typeid(void); };
167 void put(
void *p,
const T & x)
const {
169 std::stringstream sstr;
175 throw std::runtime_error(std::string(
"Caster::put(void *, const T & ), type unset"));
180 void put(
void *p, std::nullptr_t)
const;
185 void put(
const T & x)
const {
196 void put(
void *p,
const char * x)
const;
200 void put(
const char * x)
const {
207 T
get(
const void *p)
const {
208 throw std::runtime_error(std::string(
"Caster::get() unimplemented output type: ") +
typeid(T).name());
215 return get<T>(this->
ptr);
223 (*translatePtr)(c, ptrC,
ptr);
231 (*translatePtr)(c, c.
ptr, this->
ptr);
237 bool compare(
const void *
ptr,
const Caster &c,
const void *cPtr)
const {
238 return (*comparePtr)(
ptr, c, cPtr);
243 std::istream & fromStream(std::istream & istr,
const void *p)
const{
244 return (*fromStreamPtr)(istr, p);
249 std::ostream &
toOStream(std::ostream &ostr,
const void *p)
const {
273 void (* putBool)(
void *p,
const bool &x);
274 void (* putInt)(
void *p,
const int &x);
275 void (* putChar)(
void *p,
const char &x);
276 void (* putUChar)(
void *p,
const unsigned char &x);
277 void (* putUInt)(
void *p,
const unsigned int &x);
278 void (* putShort)(
void *p,
const short &x);
279 void (* putUShort)(
void *p,
const unsigned short &x);
280 void (* putLong)(
void *p,
const long &x);
281 void (* putULong)(
void *p,
const unsigned long &x);
282 void (* putFloat)(
void *p,
const float &x);
283 void (* putDouble)(
void *p,
const double &x);
286 char (* getChar)(
const void *p);
287 unsigned char (* getUChar)(
const void *p);
288 int (* getInt)(
const void *p);
289 unsigned int (* getUInt)(
const void *p);
290 short (* getShort)(
const void *p);
291 unsigned short (* getUShort)(
const void *p);
292 long (* getLong)(
const void *p);
293 unsigned long (* getULong)(
const void *p);
294 float (* getFloat)(
const void *p);
295 double (* getDouble)(
const void *p);
297 bool (* getBool)(
const void *p);
306 bool (* comparePtr)(
const void *,
const Caster &c,
const void *) =
nullptr;
310 std::ostream & (* toOStreamPtr)(std::ostream &ostr,
const void *p);
311 std::istream & (* fromStreamPtr)(std::istream &istr,
const void *p) =
nullptr;
318 template <
class T,
class F>
320 void putT(
void *p,
const T &x) {
321 *(F*)p =
static_cast<F
>(x);
340 std::stringstream sstr;
342 *(std::string*)p = sstr.str();
355 void putToCasterT(
void *p,
const T &x) {
371 template <
class T,
class F>
374 return static_cast<T
>(*(F*)p);
402 std::stringstream sstr;
403 sstr << *(
const std::string *)p;
428 *(F *)
ptr = c.
get<F>(ptrC);
445 return (*(F *)
ptr == c.
get<F>(cPtr));
453 std::ostream &
toOStreamT(std::ostream & ostr,
const void *p) {
461 std::ostream & toOStreamFloats(std::ostream & ostr,
const void *p){
464 throw std::runtime_error(
"Caster::toOStreamT<double>:: null ptr as argument");
515void Caster::link(
Caster &c){
521void Caster::updateType<void>();
524void Caster::updateType<std::string>();
528void Caster::updateType<Caster>();
534 if (!std::is_fundamental<F>::value){
535 throw std::runtime_error(std::string(__FILE__) + __FUNCTION__ +
':' +
typeid(F).name() +
": cannot convert to basic types");
544 putBool = & Caster::putT<bool,F>;
545 putInt = & Caster::putT<int,F>;
546 putChar = & Caster::putT<char,F>;
547 putUChar = & Caster::putT<unsigned char,F>;
548 putUInt = & Caster::putT<unsigned int,F>;
549 putShort = & Caster::putT<short,F>;
550 putUShort = & Caster::putT<unsigned short,F>;
551 putLong = & Caster::putT<long int,F>;
552 putULong = & Caster::putT<unsigned long,F>;
553 putFloat = & Caster::putT<float,F>;
554 putDouble = & Caster::putT<double,F>;
556 getBool = & Caster::getT<bool,F>;
557 getInt = & Caster::getT<int,F>;
558 getChar = & Caster::getT<char,F>;
559 getUChar = & Caster::getT<unsigned char,F>;
560 getUInt = & Caster::getT<unsigned int,F>;
561 getShort = & Caster::getT<short,F>;
562 getUShort = & Caster::getT<unsigned short,F>;
563 getLong = & Caster::getT<long,F>;
564 getULong = & Caster::getT<unsigned long,F>;
565 getFloat = & Caster::getT<float,F>;
566 getDouble = & Caster::getT<double,F>;
569 comparePtr = & Caster::compareT<F>;
572 fromStreamPtr = & Caster::fromStreamT<F>;
579void Caster::put<bool>(
void *p,
const bool & x)
const {
581 (this->putBool)(p,x);
586void Caster::put<char>(
void *p,
const char & x)
const {
587 (this->putChar)(p,x);
592void Caster::put<unsigned char>(
void *p,
const unsigned char & x)
const {
593 (this->putUChar)(p,x);
597void Caster::put<int>(
void *p,
const int & x)
const {
603void Caster::put<unsigned int>(
void *p,
const unsigned int & x)
const {
604 (this->putUInt)(p,x);
608void Caster::put<long>(
void *p,
const long & x)
const {
609 (this->putLong)(p,x);
613void Caster::put<unsigned long>(
void *p,
const unsigned long & x)
const {
614 (this->putULong)(p,x);
618void Caster::put<float>(
void *p,
const float& x)
const {
619 (this->putFloat)(p,x);
623void Caster::put<double>(
void *p,
const double & x)
const {
624 (this->putDouble)(p,x);
628void Caster::put<std::string>(
void *p,
const std::string & x)
const {
632 if (getType() ==
typeid(std::string))
633 *(std::string *)p = x;
634 else if (getType() ==
typeid(char)){
640 std::cerr <<
"Caster::put<std::string>() warning: single-char dst, assigned 1st of multi-char '"<< x <<
"'\n";
641 *(
char *)p = x.at(0);
657bool Caster::get<bool>(
const void *p)
const {
658 return (this->getBool)(p);
663char Caster::get<char>(
const void *p)
const {
664 return (this->getChar)(p);
668unsigned char Caster::get<unsigned char>(
const void *p)
const {
669 return (this->getUChar)(p);
673short Caster::get<short>(
const void *p)
const {
674 return (this->getShort)(p);
678unsigned short Caster::get<unsigned short>(
const void *p)
const {
679 return (this->getUShort)(p);
683int Caster::get<int>(
const void *p)
const {
684 return (this->getInt)(p);
688unsigned int Caster::get<unsigned int>(
const void *p)
const {
689 return (this->getUInt)(p);
693long Caster::get<long>(
const void *p)
const {
694 return (this->getLong)(p);
698unsigned long Caster::get<unsigned long>(
const void *p)
const {
699 return (this->getULong)(p);
703float Caster::get<float>(
const void *p)
const {
704 return (this->getFloat)(p);
708double Caster::get<double>(
const void *p)
const {
709 return (this->getDouble)(p);
715std::string Caster::get<std::string>(
const void *p)
const {
718 if (getType() ==
typeid(
void)){
719 static const std::string empty;
722 else if (getType() ==
typeid(
char)){
724 return std::string(1, *(
char *)p);
726 else if (getType() ==
typeid(
bool)){
727 return *(
const bool *)p ?
"true" :
"false";
729 else if (getType() ==
typeid(std::string)){
730 return *(
const std::string *)p;
733 std::stringstream sstr;
744void Caster::translateT<void>(
const Caster &c,
const void *ptrC,
void *ptr){
750void Caster::translateT<Caster>(
const Caster &c,
const void *ptrC,
void *ptr){
752 c2.translate(c, ptrC, c2.ptr);
759double Caster::getFromVoidT<double>(
const void *p){
767float Caster::getFromVoidT<float>(
const void *p){
777bool Caster::compareT<void>(
const void *ptr,
const Caster &c,
const void *cPtr){
786std::ostream & Caster::toOStreamT<void>(std::ostream &ostr,
const void *p){
794std::ostream & Caster::toOStreamT<bool>(std::ostream &ostr,
const void *p){
795 ostr << (*(
const bool *)p ?
"true" :
"false");
802std::ostream & Caster::toOStreamT<Caster>(std::ostream &ostr,
const void *p){
811std::ostream & Caster::toOStreamT<float>(std::ostream & ostr,
const void *p){
812 return toOStreamFloats<float>(ostr, p);
818std::ostream & Caster::toOStreamT<double>(std::ostream & ostr,
const void *p){
819 return toOStreamFloats<double>(ostr, p);
std::ostream & toOStream(std::ostream &ostr) const
Write data to output stream.
Definition Caster.h:255
static void putToStringT(void *p, std::nullptr_t)
nullptr_t overload: treated as 0 (avoids ambiguous operator<< in C++14).
Definition Caster.h:347
void updateType()
Sets pointers to the type-dependent conversion functions.
Definition Caster.h:532
void put(const T &x) const
Write to internal pointer using put(this->ptr, x).
Definition Caster.h:185
void put(const char *x) const
Write to internal pointer, calls put(this->ptr, x).
Definition Caster.h:200
const std::type_info * type
Current type.
Definition Caster.h:506
std::ostream & toOStream(std::ostream &ostr, const void *p) const
Write data to output stream.
Definition Caster.h:249
static T getFromVoidT(const void *p)
This default implementation is for integral numeric types, returns zero. Specialisation for floats re...
Definition Caster.h:385
static T getT(const void *p)
The main handler converting input to output.
Definition Caster.h:373
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
static bool compareT(const void *ptr, const Caster &c, const void *cPtr)
Checks equality through base type casting.
Definition Caster.h:437
static void putToVoidT(void *p, const T &x)
Void - does nothing, regardless of input type.
Definition Caster.h:328
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
Caster()
Default constructor. Leaves the type undefined.
Definition Caster.h:88
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:144
size_t byteSize
Size of target memory (target object) in bytes.
Definition Caster.h:509
void translate(const Caster &c) const
Convert from str Caster.
Definition Caster.h:229
void(* translatePtr)(const Caster &c, const void *ptrC, void *ptr)
Convert from str pointer and Caster.
Definition Caster.h:302
std::ostream &(* toOStreamPtr)(std::ostream &ostr, const void *p)
Write to stream.
Definition Caster.h:310
void setType()
Sets pointers to the type-dependent conversion functions.
Definition Caster.h:138
void translate(const Caster &c, const void *ptrC, void *ptr) const
Convert from str pointer and Caster.
Definition Caster.h:221
static T getFromStringT(const void *p)
The main handler converting input to output.
Definition Caster.h:400
void * ptr
Future member: enables setting Caster type.
Definition Caster.h:260
static std::istream & fromStreamT(std::istream &istr, const void *p)
Output.
Definition Caster.h:497
static std::ostream & toOStreamT(std::ostream &ostr, const void *p)
Output.
Definition Caster.h:453
void put(void *p, const T &x) const
Default conversion (for unconventional types). Uses std::stringstream for conversion.
Definition Caster.h:167
static void putToStringT(void *p, const T &x)
A handler for converting input to a std::string.
Definition Caster.h:339
Caster(const Caster &c)
Copy constructor. Copies the type.
Definition Caster.h:96
static void translateT(const Caster &c, const void *ptrC, void *ptr)
Casts from ptrC to ptr.
Definition Caster.h:426
static void putT(void *p, const T &x)
Convert input of base type to internally applied base type.
Definition Caster.h:320
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631