Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. class ICommandVar {
  2. public:
  3. virtual ~ICommandVar() = default;
  4. virtual std::string GetName() = 0;
  5. };
  6.  
  7. template <class T>
  8. class CommandVar : virtual public ICommandVar {
  9. public:
  10. CommandVar<T>(const char* name, T* defaultValue);
  11. std::string GetName() override;
  12.  
  13. protected:
  14. std::string name_;
  15. T defaultValue_;
  16. T* currentValue_;
  17. };
  18.  
  19. template <class T>
  20. CommandVar<T>::CommandVar(const char* name, T* defaultValue) : name_(name), defaultValue_(*defaultValue),currentValue_(defaultValue) {
  21. config::AddCommandVar(this);
  22. }
  23.  
  24. namespace config {
  25. std::map<std::string, ICommandVar*>* CmdVars;
  26.  
  27. void AddCommandVar(ICommandVar* cv) {
  28. if (!CmdVars) CmdVars = new std::map<std::string, ICommandVar*>();
  29. CmdVars->insert(std::pair<std::string, ICommandVar*>(cv->GetName(), cv));
  30. }
  31. void ParseLaunchArguments(int argc, char** argv) {
  32. for (auto& it : *CmdVars) {
  33. auto cmdVar = it.second;
  34. //cmdVar is a nullPtr here, cmdVar->GetName() fails
  35. }
  36. }
  37.  
  38.  
  39. template <typename T>
  40. T* declare_cmdvar(const char* name, T* defaultValue) {
  41. new CommandVar<T>(name, defaultValue);
  42. return defaultValue;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement