#pragma once /////////////////////////////////////////////////////////////////////// // CodeUtilities.h - small, generally useful, helper classes // // ver 1.0 // // Language: C++, Visual Studio 2017 // // Application: Most Projects, CSE687 - Object Oriented Design // // Author: Jim Fawcett, Syracuse University, CST 4-187 // // jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package provides classes: * - Converter converts T to and from strings * - Box converts primitive type to instance of a class * - PersistFactory adds toXml() method to T * * Build Process: * -------------- * Required Files: * CodeUtilities.h * * Maintenance History: * -------------------- * ver 1.0 : 12 Jan 2018 * - first release * - refactored from earlier Utilities.h * * Notes: * ------ * - Designed to provide all functionality in header file. * - Implementation file only needed for test and demo. * * Planned Additions and Changes: * ------------------------------ * - none yet */ #include #include namespace Utilities { ///////////////////////////////////////////////////////////////////// // Converter class // - supports converting unspecified types to and from strings // - a type is convertible if it provides insertion and extraction // operators template class Converter { public: static std::string toString(const T& t); static T toValue(const std::string& src); }; //----< convert t to a string >-------------------------------------- template std::string Converter::toString(const T& t) { std::ostringstream out; out << t; return out.str(); } //----< convert a string to an instance of T >----------------------- /* * - the string must have been generated by Converter::toString(const T& t) * - T::operator>> must be the inverse of T::operator<< */ template T Converter::toValue(const std::string& src) { std::istringstream in(src); T t; in >> t; return t; } /////////////////////////////////////////////////////////////////////// // Box class // - wraps primitive type in class // - preserves primitive syntax template class Box { public: Box() : primitive_(T()) {} Box(const T& t) : primitive_(t) {} operator T&() { return primitive_; } T& operator=(const T& t) { primitive_ = t; return primitive_; } private: T primitive_; }; /////////////////////////////////////////////////////////////////////// // ToXml interface // - defines language for creating XML elements struct ToXml { virtual std::string toXml(const std::string& tag) = 0; virtual ~ToXml() {}; }; /////////////////////////////////////////////////////////////////////// // PersistFactory class // - wraps an instance of user-defined type // - preserves semantics of user-defined type // - adds toXml("tag") method template class PersistFactory : public T, ToXml { public: PersistFactory() = default; PersistFactory(const T& t) { T::operator=(t); } std::string toXml(const std::string& tag) { std::ostringstream out; out << "<" << tag << ">" << *this << ""; return out.str(); } }; }