Paste Search Dynamic
Recent pastes
ValuesToPrint
  1. #include <experimental/optional>
  2. #include <iostream>
  3. #include <memory>
  4. #include <string>
  5.  
  6. namespace ValuesToPrint {
  7.     template <typename T>
  8.     std::experimental::optional<T> g_value = std::experimental::nullopt;
  9.  
  10.     template <typename T>
  11.     T* g_pValue = nullptr;
  12. }
  13.  
  14. struct PrintAdapter {
  15.     PrintAdapter(int value) {
  16.         using namespace ValuesToPrint;
  17.         g_value<int> = value;
  18.     }
  19.     PrintAdapter(int* value) {
  20.         using namespace ValuesToPrint;
  21.         g_pValue<int> = value;
  22.     }
  23.     PrintAdapter(float* value) {
  24.         using namespace ValuesToPrint;
  25.         g_pValue<float> = value;
  26.     }
  27.     PrintAdapter(const std::string& value) {
  28.         using namespace ValuesToPrint;
  29.         g_value<std::string> = value;
  30.     }
  31.     PrintAdapter(double value) {
  32.         using namespace ValuesToPrint;
  33.         g_value<double> = value;
  34.     }
  35.  
  36.     PrintAdapter(const PrintAdapter& other) = delete;
  37.     PrintAdapter& operator=(const PrintAdapter& other) = delete;
  38.     PrintAdapter(PrintAdapter&& other) = default;
  39.     PrintAdapter& operator=(PrintAdapter&& other) = delete;
  40. };
  41.  
  42. std::ostream& operator<<(std::ostream& stream, const PrintAdapter&) {
  43.     using namespace ValuesToPrint;
  44.     if (g_value<double>) {
  45.         return stream << g_value<double>.value() << std::endl;
  46.     } else if (g_value<std::string>) {
  47.         return stream << g_value<std::string>.value() << std::endl;
  48.     } else if (g_pValue<float>) {
  49.         return stream << "[" << *g_pValue<float> << "]" << std::endl;
  50.     } else if (g_pValue<int>) {
  51.         return stream << "[" << *g_pValue<int> << "]" << std::endl;
  52.     } else if (g_value<int>) {
  53.         return stream << g_value<int>.value() << std::endl;
  54.     }
  55.     return stream;
  56. }
  57.  
  58. class Printer {
  59.     std::unique_ptr<PrintAdapter> m_adapter;
  60.  
  61. public:
  62.     void operator=(PrintAdapter&& adapter) {
  63.         if (m_adapter == nullptr) {
  64.             m_adapter = std::make_unique<PrintAdapter>(std::forward<PrintAdapter>(adapter));
  65.         }
  66.     }
  67.  
  68.     void print() {
  69.         if (m_adapter) {
  70.             std::cout << *m_adapter;
  71.         }
  72.     }
  73. };
  74.  
  75. int main() {
  76.     Printer printer;
  77.  
  78.     printer = 42;
  79.     printer.print(); // should print "42" to standard output
  80.  
  81.     int* value = new int(10);
  82.     printer = value;
  83.     printer.print(); // should print "[10]"
  84.  
  85.     *value = 20; // the value pointer did not change, changed a value by it
  86.     printer.print(); // should print "[20]"
  87.  
  88.     float* fvalue = new float(9.81);
  89.     printer = fvalue;
  90.     printer.print(); // should print "[9.81]"
  91.  
  92.     *fvalue = 0.2+0.3;
  93.     printer.print(); // should print "[0.5]"
  94.  
  95.     printer = std::string("Hello world");
  96.     printer.print(); // should print "Hello world"
  97.  
  98.     printer = 2.718281;
  99.     printer.print(); // should print "2.718281"
  100.  
  101.     delete fvalue;
  102.     delete value;
  103.     return 0;
  104. }
Parsed in 0.013 seconds