Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 12, 2008, 10:53:51 PM (16 years ago)
Author:
landauf
Message:
  • added feature to add a callback function to configvalues. they get called if the value changes. an examples is in Core.cc.
  • changed the SetConfigValue macro and the Identifier::updateConfigValues() function to work properly with inherited classes in both possible cases: 1) they overwrite the config-value or 2) they don't. an example is ParticleProjectile that defines it's own speed_ configvalue.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core3/src/core/ConfigValueContainer.h

    r1505 r1596  
    5555namespace orxonox
    5656{
     57    class ConfigValueCallbackBase
     58    {
     59        public:
     60            virtual void call(void* object) = 0;
     61            inline virtual ~ConfigValueCallbackBase() {}
     62    };
     63
     64    template <class T>
     65    class ConfigValueCallback: public ConfigValueCallbackBase
     66    {
     67        public:
     68            inline ConfigValueCallback(void (T::*function) (void)) : function_(function) {}
     69            inline virtual ~ConfigValueCallback() {}
     70            inline virtual void call(void* object)
     71                { (((T*)object)->*this->function_)(); }
     72
     73        private:
     74            void (T::*function_) (void);
     75    };
     76
     77
    5778    //! The ConfigValuecontainer contains all needed informations about a configurable variable.
    5879    /**
     
    7697            ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue);
    7798            ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue);
    78 
    79             /** @brief Returns the configured value. @param value This is only needed to determine the right type. @return The value */
    80             template <typename T>
    81             inline ConfigValueContainer& getValue(T* value)
    82                 { this->value_.getValue(value); return *this; }
    83             template <typename T>
    84             inline ConfigValueContainer& getValue(std::vector<T>* value)
    85             {
    86                 value->clear();
    87                 for (unsigned int i = 0; i < this->valueVector_.size(); i++)
    88                     value->push_back(this->valueVector_[i]);
     99            ~ConfigValueContainer();
     100
     101            /** @brief Returns the configured value. @param value A pointer to the variable to store the value. @return The ConfigValueContainer */
     102            template <typename T, class C>
     103            ConfigValueContainer& getValue(T* value, C* object)
     104            {
     105                if (this->callback_ && object)
     106                {
     107                    T temp = *value;
     108                    this->value_.getValue(value);
     109                    if ((*value) != temp)
     110                        this->callback_->call(object);
     111                }
     112                else
     113                {
     114                    this->value_.getValue(value);
     115                }
     116                return *this;
     117            }
     118
     119            /** @brief Returns the configured vector. @param value A pointer to the vector to store the values. @return The ConfigValueContainer */
     120            template <typename T, class C>
     121            ConfigValueContainer& getValue(std::vector<T>* value, C* object)
     122            {
     123                if (this->callback_ && object)
     124                {
     125                    std::vector<T> temp = *value;
     126
     127                    value->clear();
     128                    for (unsigned int i = 0; i < this->valueVector_.size(); ++i)
     129                        value->push_back(this->valueVector_[i]);
     130
     131                    if (value->size() != temp.size())
     132                    {
     133                        this->callback_->call(object);
     134                    }
     135                    else
     136                    {
     137                        for (unsigned int i = 0; i < value->size(); ++i)
     138                        {
     139                            if ((*value)[i] != temp[i])
     140                            {
     141                                this->callback_->call(object);
     142                                break;
     143                            }
     144                        }
     145                    }
     146                }
     147                else
     148                {
     149                    value->clear();
     150                    for (unsigned int i = 0; i < this->valueVector_.size(); ++i)
     151                        value->push_back(this->valueVector_[i]);
     152                }
    89153                return *this;
    90154            }
     
    104168                { return this->valueVector_.size(); }
    105169
    106             void description(const std::string& description);
     170            ConfigValueContainer& description(const std::string& description);
    107171            const std::string& getDescription() const;
     172
     173            template <class T>
     174            inline ConfigValueContainer& callback(void (T::*function) (void))
     175            {
     176                if (!this->callback_)
     177                    this->callback_ = new ConfigValueCallback<T>(function);
     178                return (*this);
     179            }
    108180
    109181            bool set(const MultiTypeMath& input);
     
    142214            bool                       bAddedDescription_;          //!< True if a description was added
    143215            LanguageEntryLabel         description_;                //!< The description
     216            ConfigValueCallbackBase*   callback_;                   //!< A callback function to call after getValue if the value changed
    144217    };
    145218}
Note: See TracChangeset for help on using the changeset viewer.