genericproperty.h 3.88 KB
Newer Older
schultezub's avatar
 
schultezub committed
1
2
3
4
#ifndef GENERICPROPERTY_H__
#define GENERICPROPERTY_H__

#include "tgt/logmanager.h"
schultezub's avatar
schultezub committed
5
#include "core/properties/abstractproperty.h"
schultezub's avatar
 
schultezub committed
6
7
8
9

namespace TUMVis {

    /**
schultezub's avatar
schultezub committed
10
11
12
     * Generic class for value-based properties.
     *
     * \tparam  T   Base type of the property's value.
schultezub's avatar
 
schultezub committed
13
14
15
     * \todo    Add PropertyWidgets
     */
    template<typename T>
schultezub's avatar
schultezub committed
16
    class GenericProperty : public AbstractProperty {
schultezub's avatar
 
schultezub committed
17
18
    public:
        /**
schultezub's avatar
schultezub committed
19
20
21
22
23
         * Creates a new GenericProperty.
         * \param name      Property name
         * \param title     Property title (e.g. used for GUI)
         * \param value     Initial value of the property
         * \param il        Invalidation level that this property triggers
schultezub's avatar
 
schultezub committed
24
         */
schultezub's avatar
schultezub committed
25
26
27
28
29
        GenericProperty(
            const std::string& name,
            const std::string& title,
            const T& value,
            InvalidationLevel il = InvalidationLevel::INVALID_RESULT);
schultezub's avatar
 
schultezub committed
30
31
32
33
34
35
36

        /**
         * Virtual Destructor
         **/
        virtual ~GenericProperty();


37
38
39
40
41
42
43
44
        /**
         * Adds the given property \a prop to the set of shared properties.
         * All shared properties will be changed when this property changes.
         * \note        Make sure not to build circular sharing or you will encounter endless loops!
         * \param prop  Property to add, must be of the same type as this property.
         */
        virtual void addSharedProperty(AbstractProperty* prop);

schultezub's avatar
schultezub committed
45
46
47
48
49
        /**
         * Returns the current value of this property.
         * \return _value
         */
        const T getValue() const;
schultezub's avatar
 
schultezub committed
50

schultezub's avatar
schultezub committed
51
52
53
54
55
        /**
         * Sets the property value to \a value and notifies all observers.
         * \param value     New value for this property.
         */
        void setValue(const T& value);
schultezub's avatar
 
schultezub committed
56
57


schultezub's avatar
schultezub committed
58
59
    protected:
        T _value;           ///< value of the property
60

schultezub's avatar
schultezub committed
61
62
        static const std::string loggerCat_;
    };
schultezub's avatar
 
schultezub committed
63

schultezub's avatar
schultezub committed
64
// - template implementation ----------------------------------------------------------------------
schultezub's avatar
 
schultezub committed
65

schultezub's avatar
schultezub committed
66
67
68
69
70
71
    template<typename T>
    TUMVis::GenericProperty<T>::GenericProperty(const std::string& name, const std::string& title, const T& value, InvalidationLevel il /*= InvalidationLevel::INVALID_RESULT*/) 
        : AbstractProperty(name, title, il)
        , _value(value)
    {
    }
schultezub's avatar
 
schultezub committed
72

schultezub's avatar
schultezub committed
73
74
    template<typename T>
    TUMVis::GenericProperty<T>::~GenericProperty() {
schultezub's avatar
 
schultezub committed
75

schultezub's avatar
schultezub committed
76
    }
schultezub's avatar
 
schultezub committed
77

78
79
80
81
82
83
84
85
86
87
88
    template<typename T>
    void TUMVis::GenericProperty<T>::addSharedProperty(AbstractProperty* prop) {
        // make type check first, then call base method.
        tgtAssert(prop != 0, "Shared property must not be 0!");
        if (dynamic_cast< GenericProperty<T>* >(prop) == 0) {
            tgtAssert(false, "Shared property must be of the same type as this property!");
            return;
        }
        AbstractProperty::addSharedProperty(prop);
    }

schultezub's avatar
schultezub committed
89
90
91
92
    template<typename T>
    const T TUMVis::GenericProperty<T>::getValue() const {
        return _value;
    }
schultezub's avatar
 
schultezub committed
93

schultezub's avatar
schultezub committed
94
95
96
    template<typename T>
    void TUMVis::GenericProperty<T>::setValue(const T& value) {
        _value = value;
97
98
99
100
101
102
103
104
        // TODO:    think about the correct/reasonable order of observer notification
        //          thread-safety might play a role thereby...
        for (std::set<AbstractProperty*>::iterator it = _sharedProperties.begin(); it != _sharedProperties.end(); ++it) {
            // We ensure all shared properties to be of type GenericProperty<T> in the addSharedProperty overload.
            // Hence, static_cast ist safe.
            GenericProperty<T>* child = static_cast< GenericProperty<T>* >(*it);
            child->setValue(value);
        }
schultezub's avatar
schultezub committed
105
106
        notifyObservers(PropertyObserverArgs(this, _invalidationLevel));
    }
schultezub's avatar
 
schultezub committed
107

schultezub's avatar
schultezub committed
108
109
    template<typename T>
    const std::string TUMVis::GenericProperty<T>::loggerCat_ = "TUMVis.core.datastructures.GenericProperty";
schultezub's avatar
 
schultezub committed
110
111
112
}

#endif // GENERICPROPERTY_H__