#pragma once ///////////////////////////////////////////////////////////////////// // Convert.h - Implement serialization to and from strings // // ver 1.1 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2017 // ///////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package defines a single class, Convert with methods: * - std::string toString(const T&t t) converts t to a string * - T fromString(const std::string& str) converts str to an instance of T * - std::string id(const T& t) returns the typeid().Name of t * * Required Files: * --------------- * Convert.h, Convert.cpp, Utilities.h, Utilities.cpp * * Maintanence History: * -------------------- * ver 1.1 : 05 Feb 2017 * - added comments * ver 1.0 : 28 Jan 2017 * - first release */ #include #include ///////////////////////////////////////////////////////////////////// // Convert class // - (de)serializes objects of type T (from)to strings // - generates a type identifier using Run-Time Type Identification // template class Convert { public: virtual ~Convert() {} static std::string toString(const T& t); static T fromString(const std::string& str); static std::string id(const T& t); }; /*----< serialize t to a std::string >-----------------------------*/ /* * Assumes that T defines an insertion operator. */ template std::string Convert::toString(const T& t) { std::ostringstream out; out << *const_cast(&t); return out.str(); } /*----< create an instance of T from a std::string >---------------*/ /* * Assumes that T defines an extraction operator. * To succeed str must be created from Convert::toString(const T& t). */ template T Convert::fromString(const std::string& str) { std::istringstream in(str); T value; in >> value; // istringstream extraction provides the conversion return value; } /*----< create an id string from an instance of T using RTTI >----*/ template std::string Convert::id(const T& t) { return typeid(t).name(); }