Guest User

Untitled

a guest
Mar 14th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. extern class PropertyManager *propertyManager;
  2. class PropertyManager
  3. {
  4. public:
  5.     using PropertyValue = std::variant<bool, int, QString, QColor>;
  6.     enum Type { Int, Bool, String, Color };
  7.  
  8.     template<typename T>
  9.     constexpr static Type getType()
  10.     {
  11.         if constexpr (std::is_same_v<int, T>)
  12.             return Int;
  13.         if constexpr (std::is_same_v<bool, T>)
  14.             return Bool;
  15.         if constexpr (std::is_same_v<QString, T>)
  16.             return String;
  17.         if constexpr (std::is_same_v<QColor, T>)
  18.             return Color;
  19.     }
  20.  
  21.     static void initInstance() { propertyManager = new PropertyManager(); }
  22.     static void freeInstance() { delete propertyManager; }
  23.  
  24.     void set(void *instance, const QString &groupName, const QString &prop, PropertyValue value)
  25.     {
  26.         m_properties[groupName][prop]->set(instance, value);
  27.     }
  28.     void set(void *instance, const QString &groupName, const QString &prop, const char *value)
  29.     {
  30.         m_properties[groupName][prop]->set(instance, QString(value));
  31.     }
  32.     void set(void *instance, const QString &groupName, const QString &prop, int value)
  33.     {
  34.         m_properties[groupName][prop]->set(instance, (value));
  35.     }
  36.  
  37.     void set(void *instance, const QString &groupName, const QString &prop, bool value)
  38.     {
  39.         m_properties[groupName][prop]->set(instance, (value));
  40.     }
  41.  
  42.     PropertyValue get(void *instance, const QString &groupName, const QString &prop)
  43.     {
  44.         return m_properties[groupName][prop]->get(instance);
  45.     }
  46.  
  47.     void copyProperties(const QString &destGroup, const QString &srcGroup)
  48.     {
  49.         auto &s = m_properties[srcGroup];
  50.         m_properties[destGroup].insert(s.begin(), s.end());
  51.     }
  52.  
  53.     template<typename T, typename PropertyType, typename Getter, typename Setter>
  54.     void add(const QString &groupName, const QString &name, Getter getter, Setter setter)
  55.     {
  56.         m_properties[groupName][name] = std::shared_ptr<IPayload>(static_cast<IPayload *>(
  57.             new Payload<T, PropertyType, Getter, Setter>(getType<PropertyType>(), getter, setter)));
  58.     }
  59.  
  60.     template<typename T, typename PropertyType, typename Getter, typename Setter>
  61.     void addState(const QString &groupName, const QString &name, Getter getter, Setter setter)
  62.     {
  63.         m_stateProperties[groupName].insert(name);
  64.  
  65.         m_properties[groupName][name] = std::shared_ptr<IPayload>(static_cast<IPayload *>(
  66.             new PayloadState<ObjectState::Normal, T, PropertyType, Getter, Setter>(
  67.                 getType<PropertyType>(), getter, setter)));
  68.  
  69.         m_properties[groupName][name + "Selected"] = std::shared_ptr<IPayload>(
  70.             static_cast<IPayload *>(
  71.                 new PayloadState<ObjectState::Selected, T, PropertyType, Getter, Setter>(
  72.                     getType<PropertyType>(), getter, setter)));
  73.  
  74.         m_properties[groupName][name + "Clicked"] = std::shared_ptr<IPayload>(
  75.             static_cast<IPayload *>(
  76.                 new PayloadState<ObjectState::Clicked, T, PropertyType, Getter, Setter>(
  77.                     getType<PropertyType>(), getter, setter)));
  78.  
  79.         m_properties[groupName][name + "Disabled"] = std::shared_ptr<IPayload>(
  80.             static_cast<IPayload *>(
  81.                 new PayloadState<ObjectState::Disabled, T, PropertyType, Getter, Setter>(
  82.                     getType<PropertyType>(), getter, setter)));
  83.     }
  84.  
  85. private:
  86.     struct IPayload
  87.     {
  88.         const Type type;
  89.         IPayload(Type t) : type{t} {}
  90.         virtual PropertyValue get(void *instance) = 0;
  91.         virtual void set(void *instance, const PropertyValue &v) = 0;
  92.         virtual ~IPayload() {}
  93.     };
  94.  
  95.     template<typename T, typename PropertyType, typename Getter, typename Setter>
  96.     struct Payload : public IPayload
  97.     {
  98.         Getter g;
  99.         Setter s;
  100.         Payload(Type t, Getter getter, Setter setter) : IPayload(t), g{getter}, s{setter} {}
  101.         PropertyValue get(void *instance) override
  102.         {
  103.             return std::invoke(g, reinterpret_cast<T *>(instance));
  104.         }
  105.         void set(void *instance, const PropertyValue &value) override
  106.         {
  107.             std::invoke(s, reinterpret_cast<T *>(instance), std::get<PropertyType>(value));
  108.         }
  109.     };
  110.  
  111.     template<ObjectState s, typename T, typename PropertyType, typename Getter, typename Setter>
  112.     struct PayloadState : public IPayload
  113.     {
  114.         Getter g;
  115.         Setter s;
  116.         PayloadState(Type t, Getter getter, Setter setter) : IPayload{t}, g{getter}, s{setter} {}
  117.         PropertyValue get(void *instance) override
  118.         {
  119.             return std::invoke(g, reinterpret_cast<T *>(instance));
  120.         }
  121.         void set(void *instance, const PropertyValue &value) override
  122.         {
  123.             std::invoke(s, reinterpret_cast<T *>(instance), std::get<PropertyType>(value));
  124.         }
  125.     };
  126.  
  127.     std::unordered_map<QString, std::unordered_map<QString, std::shared_ptr<IPayload>>> m_properties;
  128.     QHash<QString, QSet<QString>> m_stateProperties;
  129. };
Add Comment
Please, Sign In to add comment