fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <memory>
  4. #include <string>
  5. #include <typeindex>
  6. #include <typeinfo>
  7.  
  8. using namespace std;
  9.  
  10. class DataInOut {
  11. public:
  12. shared_ptr<void> data = nullptr;
  13. type_index type = typeid(nullptr);
  14. bool initialized = false;
  15. bool optional = false;
  16.  
  17. // template <class Archive>
  18. virtual bool dummy_funct(int &ar, const char* charName){
  19. cout<< "serialize_or_throw from DataInOut address is an empty function." << endl;
  20. return true;
  21. }
  22.  
  23. virtual shared_ptr<DataInOut> clone() const { return make_shared<DataInOut>(*this); }
  24.  
  25. virtual ~DataInOut() = default; // Destructor
  26. };
  27.  
  28. template <typename T>
  29. class DataInOutType : public DataInOut {
  30. public:
  31. // template <class Archive>
  32. bool dummy_funct(int &ar, const char* charName){
  33. cout << "serialize_or_throw from DataInOutTYPE is an FULL function." << endl;
  34. return true;
  35. }
  36.  
  37. shared_ptr<DataInOut> clone() const override { return make_shared<DataInOutType<T>>(*this); }
  38. };
  39.  
  40. class mapClass {
  41. private:
  42. map<string, shared_ptr<DataInOut> > _m;
  43.  
  44. public:
  45.  
  46. template<typename T>
  47. void set(const string &key, shared_ptr<T> var, bool optional = false) {
  48. cout << "set entry with " << var.get() << endl;
  49. auto dataIO_ptr = make_shared<DataInOutType<T>>();
  50. dataIO_ptr->type = typeid(T);
  51. dataIO_ptr->data = var;
  52. dataIO_ptr->optional = optional;
  53. dataIO_ptr->initialized = true;
  54. _m[key] = dataIO_ptr;
  55. int toto = 1;
  56. // dataIO_ptr->dummy_funct(toto, key.c_str());
  57. // cout << "set EXIT" << endl;
  58. }
  59.  
  60. void call_dummy(const string &key){
  61. int dummyArchive = 1;
  62. _m.at(key)->dummy_funct(dummyArchive, key.c_str());
  63. }
  64. };
  65.  
  66. int main() {
  67. cout << "Hello World!" << endl;
  68.  
  69. mapClass mapTest;
  70. auto length = make_shared<double>(1.0);
  71.  
  72. mapTest.set("length", length);
  73. cout << "mapTest is out of set" << endl;
  74. mapTest.call_dummy("length");
  75.  
  76. cout << "ByeBye!" << endl;
  77. return 0;
  78. }
Success #stdin #stdout 0.01s 5484KB
stdin
Standard input is empty
stdout
Hello World!
set entry with 0x55dfafe6ee90
mapTest is out of set
serialize_or_throw from DataInOutTYPE is an FULL function.
ByeBye!