Browse Source

Use ToString for printing and dumping ObjectSimple classes.

ObjPtr
Michael Uleysky 9 years ago
parent
commit
c3c6f4643d
  1. 29
      include/common.h

29
include/common.h

@ -19,6 +19,19 @@ EXPORT std::ostream& COUT(debug_level dl);
typedef std::set<std::string> UsedType; typedef std::set<std::string> UsedType;
// We use different format for conversion of double to string
template<class T>
inline std::string ToString(T n) { return std::to_string(n); }
template<>
inline std::string ToString<double>(double n)
{
char buffer[32];
int i;
i=snprintf(buffer,32,"%.*G",DBL_DIG,n);
return std::string(buffer,i);
}
template<>
inline std::string ToString<std::string>(std::string s) {return s;}
// Base class for all objects // Base class for all objects
class EXPORT ObjectBase class EXPORT ObjectBase
@ -159,7 +172,7 @@ public:
bool Print() const override bool Print() const override
{ {
COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl; COUT(NORMAL)<<std::endl<<"Object type: "<<Type()<<std::endl;
COUT(NORMAL)<<"Value: "<<val<<std::endl; COUT(NORMAL)<<"Value: "<<ToString(val)<<std::endl;
return true; return true;
} }
std::string Type() const override {return type;} std::string Type() const override {return type;}
@ -171,7 +184,7 @@ public:
if(std::type_index(typeid(T))==std::type_index(typeid(std::string))) if(std::type_index(typeid(T))==std::type_index(typeid(std::string)))
s<<"\""<<val<<"\""; s<<"\""<<val<<"\"";
else else
s<<val; s<<ToString(val);
return s.str(); return s.str();
} }
@ -414,16 +427,4 @@ inline WordList Split(const char* str, const std::string& delims) {return Split(
inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);} inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);}
inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);} inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);}
// We use different format for conversion of double to string
template<class T>
inline std::string ToString(T n) { return std::to_string(n); }
template<>
inline std::string ToString<double>(double n)
{
char buffer[32];
int i;
i=snprintf(buffer,32,"%.*G",DBL_DIG,n);
return std::string(buffer,i);
}
#endif #endif

Loading…
Cancel
Save