Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern class PropertyManager *propertyManager;
- class PropertyManager
- {
- public:
- using PropertyValue = std::variant<bool, int, QString, QColor>;
- enum Type { Int, Bool, String, Color };
- template<typename T>
- constexpr static Type getType()
- {
- if constexpr (std::is_same_v<int, T>)
- return Int;
- if constexpr (std::is_same_v<bool, T>)
- return Bool;
- if constexpr (std::is_same_v<QString, T>)
- return String;
- if constexpr (std::is_same_v<QColor, T>)
- return Color;
- }
- static void initInstance() { propertyManager = new PropertyManager(); }
- static void freeInstance() { delete propertyManager; }
- void set(void *instance, const QString &groupName, const QString &prop, PropertyValue value)
- {
- m_properties[groupName][prop]->set(instance, value);
- }
- void set(void *instance, const QString &groupName, const QString &prop, const char *value)
- {
- m_properties[groupName][prop]->set(instance, QString(value));
- }
- void set(void *instance, const QString &groupName, const QString &prop, int value)
- {
- m_properties[groupName][prop]->set(instance, (value));
- }
- void set(void *instance, const QString &groupName, const QString &prop, bool value)
- {
- m_properties[groupName][prop]->set(instance, (value));
- }
- PropertyValue get(void *instance, const QString &groupName, const QString &prop)
- {
- return m_properties[groupName][prop]->get(instance);
- }
- void copyProperties(const QString &destGroup, const QString &srcGroup)
- {
- auto &s = m_properties[srcGroup];
- m_properties[destGroup].insert(s.begin(), s.end());
- }
- template<typename T, typename PropertyType, typename Getter, typename Setter>
- void add(const QString &groupName, const QString &name, Getter getter, Setter setter)
- {
- m_properties[groupName][name] = std::shared_ptr<IPayload>(static_cast<IPayload *>(
- new Payload<T, PropertyType, Getter, Setter>(getType<PropertyType>(), getter, setter)));
- }
- template<typename T, typename PropertyType, typename Getter, typename Setter>
- void addState(const QString &groupName, const QString &name, Getter getter, Setter setter)
- {
- m_stateProperties[groupName].insert(name);
- m_properties[groupName][name] = std::shared_ptr<IPayload>(static_cast<IPayload *>(
- new PayloadState<ObjectState::Normal, T, PropertyType, Getter, Setter>(
- getType<PropertyType>(), getter, setter)));
- m_properties[groupName][name + "Selected"] = std::shared_ptr<IPayload>(
- static_cast<IPayload *>(
- new PayloadState<ObjectState::Selected, T, PropertyType, Getter, Setter>(
- getType<PropertyType>(), getter, setter)));
- m_properties[groupName][name + "Clicked"] = std::shared_ptr<IPayload>(
- static_cast<IPayload *>(
- new PayloadState<ObjectState::Clicked, T, PropertyType, Getter, Setter>(
- getType<PropertyType>(), getter, setter)));
- m_properties[groupName][name + "Disabled"] = std::shared_ptr<IPayload>(
- static_cast<IPayload *>(
- new PayloadState<ObjectState::Disabled, T, PropertyType, Getter, Setter>(
- getType<PropertyType>(), getter, setter)));
- }
- private:
- struct IPayload
- {
- const Type type;
- IPayload(Type t) : type{t} {}
- virtual PropertyValue get(void *instance) = 0;
- virtual void set(void *instance, const PropertyValue &v) = 0;
- virtual ~IPayload() {}
- };
- template<typename T, typename PropertyType, typename Getter, typename Setter>
- struct Payload : public IPayload
- {
- Getter g;
- Setter s;
- Payload(Type t, Getter getter, Setter setter) : IPayload(t), g{getter}, s{setter} {}
- PropertyValue get(void *instance) override
- {
- return std::invoke(g, reinterpret_cast<T *>(instance));
- }
- void set(void *instance, const PropertyValue &value) override
- {
- std::invoke(s, reinterpret_cast<T *>(instance), std::get<PropertyType>(value));
- }
- };
- template<ObjectState s, typename T, typename PropertyType, typename Getter, typename Setter>
- struct PayloadState : public IPayload
- {
- Getter g;
- Setter s;
- PayloadState(Type t, Getter getter, Setter setter) : IPayload{t}, g{getter}, s{setter} {}
- PropertyValue get(void *instance) override
- {
- return std::invoke(g, reinterpret_cast<T *>(instance));
- }
- void set(void *instance, const PropertyValue &value) override
- {
- std::invoke(s, reinterpret_cast<T *>(instance), std::get<PropertyType>(value));
- }
- };
- std::unordered_map<QString, std::unordered_map<QString, std::shared_ptr<IPayload>>> m_properties;
- QHash<QString, QSet<QString>> m_stateProperties;
- };
Add Comment
Please, Sign In to add comment