///////////////////////////////////////////////////////////////////// // TemplatesIntro.cpp - Demonstrate Template Syntax // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2017 // ///////////////////////////////////////////////////////////////////// #include "TemplatesIntro.h" #include #include ///////////////////////////////////////////////////////////////////// // Widget class - shows user defined types can be template arguments class Widget { public: Widget(const std::string& str = "") : state(str) {} std::string& value() { return state; } private: std::string state; }; std::ostream& operator<<(std::ostream& out, Widget& widget) { out << widget.value(); return out; } std::istream& operator >> (std::istream& in, Widget& widget) { std::string temp; while (in.good()) // extract all the words from widget's string state { in >> temp; widget.value() += temp + " "; } return in; } ///////////////////////////////////////////////////////////////////// // lambdas that provide mildly useful local processing std::function putLine = []() { std::wcout << "\n"; }; std::function putLines = [](size_t n) { for (size_t i = 0; i < n; ++i) putLine(); }; std::function titleCore = [](const std::string& msg, char underline='-') { std::cout << "\n " << msg.c_str(); std::wcout << "\n " << std::string(msg.size() + 2, underline).c_str(); }; std::function Title = [](const std::string& msg) { titleCore(msg, '='); }; std::function title = [](const std::string& msg) { titleCore(msg, '-'); }; ///////////////////////////////////////////////////////////////////// // Demo code int main() { Title("Demonstrating Templates"); putLine(); title("TemplateDemo and Convert>"); TemplateDemo tdi(42); std::cout << "\n type is: " << Convert>::id(tdi); std::cout << "\n value is: " << tdi.value(); std::string TDIstring = Convert>::toString(tdi); std::cout << "\n toString: " << TDIstring; std::cout << "\n creating TemplateDemo object using fromString:"; TemplateDemo newTdi = Convert>::fromString(TDIstring); std::cout << "\n type is: " << Convert>::id(newTdi); std::cout << "\n value is: " << newTdi.value(); putLine(); title("TemplateDemo and Convert>"); TemplateDemo tdd(3.1415927); std::cout << "\n type is: " << Convert>::id(tdd); std::cout << "\n value is: " << tdd.value(); std::string TDDstring = Convert>::toString(tdd); std::cout << "\n toString: " << TDDstring; std::cout << "\n creating TemplateDemo object using fromString:"; TemplateDemo newTdd = Convert>::fromString(TDDstring); std::cout << "\n type is: " << Convert>::id(newTdd); std::cout << "\n value is: " << newTdd.value(); putLine(); title("TemplateDemo and Convert>"); Widget widget("I am a widget"); TemplateDemo tdw(widget); std::cout << "\n type is: " << Convert>::id(widget); std::cout << "\n value is: " << tdw.value(); std::string TDWstring = Convert>::toString(tdw); std::cout << "\n toString: " << TDWstring; std::cout << "\n creating TemplateDemo object using fromString:"; TemplateDemo newTdw = Convert>::fromString(TDWstring); std::cout << "\n type is: " << Convert>::id(newTdw); std::cout << "\n value is: " << newTdw.value(); putLines(2); }