This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: KIKIJIKI on Nov 17th, 2012  |  syntax: C++  |  size: 1.55 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <string>
  2. #include <map>
  3. #include <memory>
  4. #include <iostream>
  5.  
  6. struct Parameter{};
  7.  
  8. template<typename T>
  9. struct TypedParameter : public Parameter
  10. {
  11. public:
  12.         TypedParameter (const T& data): data(data){};
  13.         T data;
  14. };
  15.  
  16. struct ParameterList
  17. {
  18.         std::map<std::wstring, std::shared_ptr<Parameter>> list;
  19.  
  20.     //ParameterList(){}
  21.  
  22.     ParameterList(const ParameterList& other):list(other.list){}
  23.  
  24.     template<typename... Values>
  25.     ParameterList(std::pair<std::wstring, Values> const &... keyvalues)
  26.     {
  27.         addPair<Values>(keyvalues...);
  28.     }
  29.  
  30.         template<class T> void addParameter(const std::wstring& name, const T& param)
  31.         {
  32.                 list[name] = std::shared_ptr<Parameter>(new TypedParameter<T>(param));
  33.         }
  34.  
  35.     template<class T> void addPair(std::pair<std::wstring, T> param)
  36.     {
  37.         addParameter<T>(param.first, param.second);
  38.     }
  39.  
  40.         template<class T> T& getParameter(const std::wstring& name) const
  41.         {
  42.                 return static_cast<TypedParameter<T>*>(list.at(name).get())->data;
  43.         }
  44. };
  45.  
  46. class Test
  47. {
  48. private:
  49.     ParameterList _param;
  50. protected:
  51. public:
  52.     Test(ParameterList p): _param(p){}
  53.  
  54.     void Present()
  55.     {
  56.         std::wcout << L"My name is " << _param.getParameter<std::wstring>(L"name");
  57.         std::wcout << L" and I'm " << _param.getParameter<int>(L"age") << L" years old." << std::endl;
  58.     }
  59. };
  60.  
  61. int main()
  62. {
  63.     Test t1(
  64.         std::make_pair(L"name", L"Vorel"),
  65.         std::make_pair(L"age", 19870),
  66.         std::make_pair(L"job", L"dragon"));
  67.  
  68.     t1.Present();
  69.  
  70.     getchar();
  71. }
clone this paste RAW Paste Data