#ifndef TOOLS_H #define TOOLS_H #include #include #include #include #include #include namespace fw { // read utilities for istream inline uint8 getu8(std::istream& in) { uint8 tmp; in.read((char*)&tmp, 1); return tmp; } inline uint16 getu16(std::istream& in) { uint16 tmp; in.read((char*)&tmp, 2); return tmp; } inline uint32 getu32(std::istream& in) { uint32 tmp; in.read((char*)&tmp, 4); return tmp; } /// Fill an ostream by concatenating args /// Usage: /// fw::fill_ostream(stream, a1, a2, ..., aN); inline void fill_ostream(std::ostringstream&) { } template void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) { stream << first; fill_ostream(stream, rest...); } /// Makes a std::string by concatenating args /// Usage: /// std::string str = fw::mkstr(a1, a2, ..., aN); template std::string mkstr(const T&... args) { std::ostringstream buf; fill_ostream(buf, args...); return buf.str(); } // used by dumper struct dump_util { ~dump_util() { std::cout << std::endl; } template dump_util& operator<<(const T& v) { std::cout << v << " "; return *this; } }; /// Utility for dumping variables /// Usage: /// fw::dump << v1, v2, ..., vN; struct dumper { dumper() { } template dump_util operator<<(const T& v) const { dump_util d; d << v; return d; } }; /// Utility for printing messages into stdout /// Usage: /// fw::print(v1, v2, ..., vN); template void print(const T&... args) { std::ostringstream buf; fill_ostream(buf, args...); std::cout << buf.str(); } /// Same as fw::print but adds a new line at the end template void println(const T&... args) { print(args...); std::cout << std::endl; } /// Demangle names for GNU g++ compiler inline std::string demangle_name(const char* name) { size_t len; int status; std::string ret; char* demangled = abi::__cxa_demangle(name, 0, &len, &status); if(demangled) { ret = demangled; free(demangled); } return ret; } /// Returns the name of a type /// e.g. fw::demangle_type() returns a string containing 'Foo*' template std::string demangle_type() { return demangle_name(typeid(T).name()); } /// Cast a type to another type /// @return whether the conversion was successful or not template bool cast(const T& in, R& out) { std::stringstream ss; ss << in; ss >> out; return !!ss && ss.eof(); } // cast a type to string template bool cast(const T& in, std::string& out) { std::stringstream ss; ss << in; out = ss.str(); return true; } // cast string to string template<> inline bool cast(const std::string& in, std::string& out) { out = in; return true; } // special cast from string to boolean template<> inline bool cast(const std::string& in, bool& b) { static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}}; bool ret = false; for(int i=0;i<4;++i) { if(in == validNames[0][i]) { b = true; ret = true; break; } else if(in == validNames[1][i]) { b = false; ret = true; break; } } return ret; } // special cast from boolean to string template<> inline bool cast(const bool& in, std::string& out) { out = (in ? "true" : "false"); return true; } // used by safe_cast class bad_cast : public std::bad_cast { public: virtual ~bad_cast() throw() { } template void setWhat() { m_what = mkstr("failed to cast value of type '", demangle_type(), "' to type '", demangle_type(), "'"); } virtual const char* what() { return m_what.c_str(); } private: std::string m_what; }; /// Cast a type to another type, any error throws a fw::bad_cast_exception /// Usage: /// R r = fw::safe_cast(t); template R safe_cast(const T& t) { R r; if(!cast(t, r)) { bad_cast e; e.setWhat(); throw e; } return r; } /// Cast a type to another type, cast errors are ignored /// Usage: /// R r = fw::unsafe_cast(t); template R unsafe_cast(const T& t, R def = R()) { try { return safe_cast(t); } catch(bad_cast& e) { println("CAST ERROR: ", e.what()); return def; } } template std::string tostring(const T& t) { return unsafe_cast(t); } template T fromstring(const std::string& str, T def = T()) { return unsafe_cast(str, def); } inline std::string dec2hex(unsigned int num) { std::string str; std::ostringstream o; o << std::hex << num; str = o.str(); return str; } inline unsigned int hex2dec(const std::string& str) { unsigned int num; std::istringstream i(str); i >> std::hex >> num; return num; } // an empty string to use anywhere needed const static std::string empty_string; } // shortcut for fw::dump const static fw::dumper dump; #endif