Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/howto/ConfigValue


Ignore:
Timestamp:
Oct 11, 2008, 2:35:05 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/howto/ConfigValue

    v1 v1  
     1= HowTo: ConfigValue =
     2[[TracNav(TracNav/TOC_Development)]]
     3
     4== Description ==
     5Config-values are member variables of a class that can be configured in the [wiki:howto/ConfigFile config-file]. The configured value will be assigned to every instance of this class.
     6
     7To use config-values in your class, you have to follow those steps:
     8 1. Inherit from [wiki:OrxonoxClass] or a derivative
     9 1. Then you have to add {{{void setConfigValues()}}} to the class
     10 1. Include [wiki:ConfigValueIncludes core/ConfigValueIncludes.h] in the source file
     11 1. Add '''SetConfigValue('''''variable, defaultvalue''''')''' macro-calls into {{{setConfigValues()}}}
     12 1. Call {{{setConfigValues()}}} from the constructor of your class
     13
     14== Example ==
     15
     16*.h file:
     17{{{
     18class MyClass : public SomeObject     (1)
     19{
     20    public:
     21        MyClass();
     22        void setConfigValues();       (2)
     23
     24        ...
     25
     26    private:
     27        float value_;
     28};
     29}}}
     30
     31*.cc file:
     32{{{
     33#include "core/CoreIncludes.h"
     34#include "core/ConfigValueIncludes.h" (3)
     35
     36// Constructor:
     37MyClass::MyClass()
     38{
     39    RegisterObject(MyClass);
     40
     41    this->setConfigValues();          (5)
     42}
     43
     44void MyClass::setConfigValues()
     45{
     46    SetConfigValue(value_, 123.4);    (4)
     47}
     48}}}
     49
     50== Inheritance ==
     51There are three possible ways to deal with inheritance and config-values:
     52=== Use a config-value in derived classes ===
     53If a config-value is defined for a base-class, all derived classes will be configured by this value.
     54
     55'''Example''':
     56
     57{{{BaseClass}}} implements a config value:
     58{{{
     59class BaseClass : public OrxonoxClass
     60{
     61    public:
     62        ...
     63        void setConfigValues();
     64
     65    protected:
     66        float value_;
     67};
     68
     69void BaseClass::setConfigValues()
     70{
     71    SetConfigValue(value_, 123.4);
     72}
     73}}}
     74
     75{{{DerivedClass}}} inherits from {{{BaseClass}}} without implementing {{{setConfigValues}}} again:
     76{{{
     77class DerivedClass : public BaseClass
     78{
     79};
     80}}}
     81
     82In the config-file this will look like this:
     83{{{
     84[BaseClass]
     85value_=123.4
     86}}}
     87Then {{{BaseClass::value_}}} and {{{DerivedClass::value_}}} are both set by the config-value of {{{BaseClass}}} (and therefore 123.4).
     88
     89=== Implementing a config-value only in a derived class ===
     90It's possible to define a config-value for a variable which was inherited from a base class.
     91
     92'''Example''':
     93
     94{{{BaseClass}}} declares a variable but no config-value:
     95{{{
     96class BaseClass : public OrxonoxClass
     97{
     98    ...
     99
     100    protected:
     101        float baseValue_;
     102};
     103}}}
     104
     105{{{DerivedClass}}} derives from {{{BaseClass}}} and implements the config-value:
     106{{{
     107class DerivedClass : public BaseClass
     108{
     109    public:
     110        ...
     111        void setConfigValues();
     112};
     113
     114void DerivedClass::setConfigValues()
     115{
     116    SetConfigValue(baseValue_, 10.1);
     117}
     118}}}
     119
     120In the config-file this will look like this:
     121{{{
     122[DerivedClass]
     123value_=10.1
     124}}}
     125
     126Then {{{DerivedClass::baseValue_}}} is set by the config-value (and therefore 10.1) but {{{BaseClass::baseValue_}}} isn't configurable.
     127
     128=== Overwriting config-values in a derived class ===
     129If both, a base class and a derived class, implement the same value, instances of the base class use the configured value from the config-value-section of the base class while instances of the derived class (and further derived classes) use the overwritten value.
     130
     131'''Example''':
     132
     133{{{BaseClass}}} implements a config value:
     134{{{
     135class BaseClass : public OrxonoxClass
     136{
     137    public:
     138        ...
     139        void setConfigValues();
     140
     141    protected:
     142        float value_;
     143};
     144
     145void BaseClass::setConfigValues()
     146{
     147    SetConfigValue(value_, 123.4);
     148}
     149}}}
     150
     151{{{DerivedClass}}} derives from {{{BaseClass}}} and implements the config-value again:
     152{{{
     153class DerivedClass : public BaseClass
     154{
     155    public:
     156        ...
     157        void setConfigValues();
     158};
     159
     160void DerivedClass::setConfigValues()
     161{
     162    SetConfigValue(value_, 10.1);
     163}
     164}}}
     165
     166In the config-file this will look like this:
     167{{{
     168[BaseClass]
     169value_=123.4
     170
     171[DerivedClass]
     172value_=10.1
     173}}}
     174
     175Then {{{BaseClass::value_}}} is set by the config-value of {{{BaseClass}}} (and therefore 123.4) but {{{DerivedClass::value_}}} uses the config-value of {{{DerivedClass}}} (and therefore 10.1).