PixelLightAPI  .
VarStorage.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: VarStorage.h                                   *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_VAR_STORAGE_H__
00024 #define __PLCORE_VAR_STORAGE_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/PLCore.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Forward declarations                                  ]
00042 //[-------------------------------------------------------]
00043 class Object;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Helper classes                                        ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Storage type: Store a direct value of the specific type
00052 */
00053 class StorageDirectValue
00054 {
00055     public:
00056         // Save storage type
00057         typedef StorageDirectValue StorageType;
00058         typedef StorageDirectValue BaseStorageType;
00059 };  
00060 
00061 /**
00062 *  @brief
00063 *    Storage type: Use getter and setter methods to store a value
00064 */
00065 class StorageGetSet
00066 {
00067     public:
00068         // Save storage type
00069         typedef StorageGetSet StorageType;
00070         typedef StorageGetSet BaseStorageType;
00071 };
00072 
00073 /**
00074 *  @brief
00075 *    Storage type: Use an already defined attribute to store a value
00076 */
00077 class StorageModifyAttr
00078 {
00079     public:
00080         // Save storage type
00081         typedef StorageModifyAttr StorageType;
00082         typedef StorageModifyAttr BaseStorageType;
00083 };
00084 
00085 /**
00086 *  @brief
00087 *    Check Storage type to determine, whether the default value shall be set or not
00088 */
00089 template <typename STORAGE>
00090 class CheckStorage {
00091     public:
00092         // Set default value?
00093         enum {
00094             InitDefault = 1
00095         };
00096 };
00097 
00098 template <>
00099 class CheckStorage<StorageGetSet> {
00100     public:
00101         // Set default value?
00102         enum {
00103             InitDefault = 0
00104         };
00105 };
00106 
00107 
00108 //[-------------------------------------------------------]
00109 //[ Classes                                               ]
00110 //[-------------------------------------------------------]
00111 /**
00112 *  @brief
00113 *    Storage type of a variable
00114 *
00115 *  @remarks
00116 *    Base template declaration
00117 */
00118 template <typename T, typename STORAGE, typename STORAGETYPE>
00119 class VarStorage {
00120 };
00121 
00122 
00123 /**
00124 *  @brief
00125 *    Storage type of a variable
00126 *
00127 *  @remarks
00128 *    Implementation for direct value storage
00129 */
00130 template <typename T, typename STORAGE>
00131 class VarStorage<T, STORAGE, StorageDirectValue> {
00132 
00133 
00134     //[-------------------------------------------------------]
00135     //[ Public data types                                     ]
00136     //[-------------------------------------------------------]
00137     public:
00138         // Storage type
00139         typedef typename Type<T>::_Type _Type;
00140 
00141 
00142     //[-------------------------------------------------------]
00143     //[ Public functions                                      ]
00144     //[-------------------------------------------------------]
00145     public:
00146         /**
00147         *  @brief
00148         *    Constructor
00149         *
00150         *  @param[in] DefaultValue
00151         *    Default value
00152         */
00153         VarStorage(const _Type &DefaultValue) :
00154             m_DefaultValue(DefaultValue),
00155             m_Value(DefaultValue)
00156         {
00157         }
00158 
00159         /**
00160         *  @brief
00161         *    Constructor
00162         *
00163         *  @param[in] DefaultValue
00164         *    Default value
00165         *  @param[in] pObject
00166         *    Pointer to object holding the attribute
00167         */
00168         VarStorage(const _Type &DefaultValue, Object *pObject) :
00169             m_DefaultValue(DefaultValue),
00170             m_Value(DefaultValue)
00171         {
00172         }
00173 
00174         /**
00175         *  @brief
00176         *    Get default value
00177         *
00178         *  @return
00179         *    Default value
00180         */
00181         inline _Type GetDefault() const
00182         {
00183             return m_DefaultValue;
00184         }
00185 
00186 
00187     //[-------------------------------------------------------]
00188     //[ Protected functions                                   ]
00189     //[-------------------------------------------------------]
00190     protected:
00191         /**
00192         *  @brief
00193         *    Get stored value
00194         *
00195         *  @return
00196         *    Value
00197         */
00198         inline _Type StorageGet() const
00199         {
00200             return m_Value;
00201         }
00202 
00203         /**
00204         *  @brief
00205         *    Set stored value
00206         *
00207         *  @param[in] Value
00208         *    Value
00209         */
00210         inline void StorageSet(const _Type &Value)
00211         {
00212             m_Value = Value;
00213         }
00214 
00215 
00216     //[-------------------------------------------------------]
00217     //[ Private data                                          ]
00218     //[-------------------------------------------------------]
00219     private:
00220         _Type m_DefaultValue;   /**< Default value */
00221         _Type m_Value;          /**< Stored value */
00222 
00223 
00224 };
00225 
00226 
00227 /**
00228 *  @brief
00229 *    Storage type of a variable
00230 *
00231 *  @remarks
00232 *    Implementation for get/set value storage
00233 *
00234 *  @remarks
00235 *    Base template declaration
00236 */
00237 template <typename T, typename STORAGE>
00238 class VarStorage<T, STORAGE, StorageGetSet> {
00239 
00240 
00241     //[-------------------------------------------------------]
00242     //[ Public data types                                     ]
00243     //[-------------------------------------------------------]
00244     public:
00245         // Storage type
00246         typedef typename Type<T>::_Type _Type;
00247 
00248 
00249     //[-------------------------------------------------------]
00250     //[ Public functions                                      ]
00251     //[-------------------------------------------------------]
00252     public:
00253         /**
00254         *  @brief
00255         *    Constructor
00256         *
00257         *  @param[in] DefaultValue
00258         *    Default value of var
00259         */
00260         VarStorage(const _Type &DefaultValue) :
00261             m_DefaultValue(DefaultValue),
00262             m_pObject(nullptr)
00263         {
00264             // We do NOT set the default value here, because this constructor is called inside
00265             // the constructor of the object holding the attribute. If we set a value here, it
00266             // would cause the set/get-methods to be called and then we end up calling a method
00267             // of a not-fully-initialized-yet object. That should be avoided, so you have to
00268             // initialize you external values by yourself.
00269             //StorageSet(DefaultValue);
00270         }
00271 
00272         /**
00273         *  @brief
00274         *    Constructor
00275         *
00276         *  @param[in] DefaultValue
00277         *    Default value
00278         *  @param[in] pObject
00279         *    Pointer to object holding the attribute
00280         */
00281         VarStorage(const _Type &DefaultValue, Object *pObject) :
00282             m_DefaultValue(DefaultValue),
00283             m_pObject(pObject)
00284         {
00285             // We do NOT set the default value here (see above)
00286             //StorageSet(DefaultValue);
00287         }
00288 
00289         /**
00290         *  @brief
00291         *    Get default value
00292         *
00293         *  @return
00294         *    Default value
00295         */
00296         inline _Type GetDefault() const
00297         {
00298             return m_DefaultValue;
00299         }
00300 
00301 
00302     //[-------------------------------------------------------]
00303     //[ Protected functions                                   ]
00304     //[-------------------------------------------------------]
00305     protected:
00306         /**
00307         *  @brief
00308         *    Get stored value
00309         *
00310         *  @return
00311         *    Value
00312         */
00313         inline _Type StorageGet() const
00314         {
00315             return STORAGE::Get(m_pObject);
00316         }
00317 
00318         /**
00319         *  @brief
00320         *    Set stored value
00321         *
00322         *  @param[in] Value
00323         *    Value
00324         */
00325         inline void StorageSet(const _Type &Value)
00326         {
00327             STORAGE::Set(m_pObject, Value);
00328         }
00329 
00330 
00331     //[-------------------------------------------------------]
00332     //[ Private data                                          ]
00333     //[-------------------------------------------------------]
00334     private:
00335         _Type   m_DefaultValue; /**< Default value */
00336         Object *m_pObject;      /**< Pointer to object */
00337 
00338 
00339 };
00340 
00341 
00342 /**
00343 *  @brief
00344 *    Storage type of a variable
00345 *
00346 *  @remarks
00347 *    Implementation for get/set value storage
00348 *
00349 *  @remarks
00350 *    Base template declaration
00351 */
00352 template <typename T, typename STORAGE>
00353 class VarStorage<T, STORAGE, StorageModifyAttr> {
00354 
00355 
00356     //[-------------------------------------------------------]
00357     //[ Public data types                                     ]
00358     //[-------------------------------------------------------]
00359     public:
00360         // Storage type
00361         typedef typename Type<T>::_Type _Type;
00362 
00363 
00364     //[-------------------------------------------------------]
00365     //[ Public functions                                      ]
00366     //[-------------------------------------------------------]
00367     public:
00368         /**
00369         *  @brief
00370         *    Constructor
00371         *
00372         *  @param[in] DefaultValue
00373         *    Default value of var
00374         */
00375         VarStorage(const _Type &DefaultValue) :
00376             m_DefaultValue(DefaultValue),
00377             m_pObject(nullptr)
00378         {
00379             // Set default value only, if the base storage is not GetSet (see above for explanation)
00380             bool bInitDefault = (CheckStorage<typename STORAGE::BaseStorageType>::InitDefault == 1);
00381             if (bInitDefault) {
00382                 // Set default value
00383                 StorageSet(DefaultValue);
00384             }
00385         }
00386 
00387         /**
00388         *  @brief
00389         *    Constructor
00390         *
00391         *  @param[in] DefaultValue
00392         *    Default value
00393         *  @param[in] pObject
00394         *    Pointer to object holding the attribute
00395         */
00396         VarStorage(const _Type &DefaultValue, Object *pObject) :
00397             m_DefaultValue(DefaultValue),
00398             m_pObject(pObject)
00399         {
00400             // Set default value only, if the base storage is not GetSet (see above for explanation)
00401             bool bInitDefault = (CheckStorage<typename STORAGE::BaseStorageType>::InitDefault == 1);
00402             if (bInitDefault) {
00403                 // Set default value
00404                 StorageSet(DefaultValue);
00405             }
00406         }
00407 
00408         /**
00409         *  @brief
00410         *    Get default value
00411         *
00412         *  @return
00413         *    Default value
00414         */
00415         inline _Type GetDefault() const
00416         {
00417             return m_DefaultValue;
00418         }
00419 
00420 
00421     //[-------------------------------------------------------]
00422     //[ Protected functions                                   ]
00423     //[-------------------------------------------------------]
00424     protected:
00425         /**
00426         *  @brief
00427         *    Get stored value
00428         *
00429         *  @return
00430         *    Value
00431         */
00432         inline _Type StorageGet() const
00433         {
00434             return STORAGE::Get(m_pObject);
00435         }
00436 
00437         /**
00438         *  @brief
00439         *    Set stored value
00440         *
00441         *  @param[in] Value
00442         *    Value
00443         */
00444         inline void StorageSet(const _Type &Value)
00445         {
00446             STORAGE::Set(m_pObject, Value);
00447         }
00448 
00449 
00450     //[-------------------------------------------------------]
00451     //[ Private data                                          ]
00452     //[-------------------------------------------------------]
00453     private:
00454         _Type   m_DefaultValue; /**< Default value */
00455         Object *m_pObject;      /**< Pointer to object */
00456 
00457 
00458 };
00459 
00460 
00461 /**
00462 *  @brief
00463 *    Helper class to choose a storage type
00464 *
00465 *  @remarks
00466 *    Base template declaration
00467 */
00468 template <typename STORAGE, typename CLASS_GS, typename CLASS_MOD>
00469 class StorageChooser {
00470 };
00471 
00472 
00473 /**
00474 *  @brief
00475 *    Helper class to choose a storage type
00476 *
00477 *  @remarks
00478 *    Implementation for direct value storage
00479 */
00480 template <typename CLASS_GS, typename CLASS_MOD>
00481 class StorageChooser<StorageDirectValue, CLASS_GS, CLASS_MOD> {
00482     public:
00483         // Choose direct-value storage
00484         typedef StorageDirectValue Storage;
00485 };
00486 
00487 /**
00488 *  @brief
00489 *    Helper class to choose a storage type
00490 *
00491 *  @remarks
00492 *    Implementation for get/set-method storage
00493 */
00494 template <typename CLASS_GS, typename CLASS_MOD>
00495 class StorageChooser<StorageGetSet, CLASS_GS, CLASS_MOD> {
00496     public:
00497         // Choose get/set storage
00498         typedef CLASS_GS Storage;
00499 };
00500 
00501 /**
00502 *  @brief
00503 *    Helper class to choose a storage type
00504 *
00505 *  @remarks
00506 *    Implementation for modified-attribute storage
00507 */
00508 template <typename CLASS_GS, typename CLASS_MOD>
00509 class StorageChooser<StorageModifyAttr, CLASS_GS, CLASS_MOD> {
00510     public:
00511         // Choose modify storage
00512         typedef CLASS_MOD Storage;
00513 };
00514 
00515 
00516 //[-------------------------------------------------------]
00517 //[ Namespace                                             ]
00518 //[-------------------------------------------------------]
00519 } // PLCore
00520 
00521 
00522 #endif // __PLCORE_VAR_STORAGE_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:04
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported