PixelLightAPI  .
ElementManager.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: ElementManager.h                               *
00003  *
00004  *  Copyright (C) 2002-2012 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_ELEMENT_MANAGER_H__
00024 #define __PLCORE_ELEMENT_MANAGER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/String/String.h"
00032 #include "PLCore/Container/Array.h"
00033 #include "PLCore/Container/HashMap.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Namespace                                             ]
00038 //[-------------------------------------------------------]
00039 namespace PLCore {
00040 
00041 
00042 //[-------------------------------------------------------]
00043 //[ Forward declarations                                  ]
00044 //[-------------------------------------------------------]
00045 template <class AType> class Element;
00046 
00047 
00048 //[-------------------------------------------------------]
00049 //[ Classes                                               ]
00050 //[-------------------------------------------------------]
00051 /**
00052 *  @brief
00053 *    Abstract element manager template
00054 */
00055 template <class AType> class ElementManager {
00056 
00057 
00058     //[-------------------------------------------------------]
00059     //[ Friends                                               ]
00060     //[-------------------------------------------------------]
00061     friend class Element<AType>;
00062 
00063 
00064     //[-------------------------------------------------------]
00065     //[ Public functions                                      ]
00066     //[-------------------------------------------------------]
00067     public:
00068         /**
00069         *  @brief
00070         *    Returns the name of the manager
00071         *
00072         *  @return
00073         *    Name of the manager
00074         *
00075         *  @note
00076         *    - The manager name has no special usage, it's just for completeness
00077         */
00078         String GetManagerName() const;
00079 
00080         /**
00081         *  @brief
00082         *    Sets the name of the manager
00083         *
00084         *  @param[in] sName
00085         *    New manager name
00086         *
00087         *  @return
00088         *    'true' if all went fine else 'false'
00089         *
00090         *  @see
00091         *    - GetManagerName()
00092         */
00093         bool SetManagerName(const String &sName = "");
00094 
00095         /**
00096         *  @brief
00097         *   Clear the manager, delete all elements
00098         *
00099         *  @param[in] bProtectedToo
00100         *    Delete protected elements, too?
00101         *
00102         *  @return
00103         *    'true' if all went fine, else 'false'
00104         */
00105         bool Clear(bool bProtectedToo = true);
00106 
00107         /**
00108         *  @brief
00109         *    Creates a new element
00110         *
00111         *  @param[in] sName
00112         *    Element name, if "" an unused name is set automatically
00113         *
00114         *  @return
00115         *    Pointer to the created element, a null pointer if there was an error
00116         *
00117         *  @note
00118         *    - If there's already a element with this name, this element is returned
00119         */
00120         AType *Create(const String &sName = "");
00121 
00122         /**
00123         *  @brief
00124         *    Adds a element
00125         *
00126         *  @param[in] cElement
00127         *    Element to add
00128         *
00129         *  @return
00130         *    'true' if all went fine, else 'false'
00131         *    (maybe element is already within another manager)
00132         */
00133         bool Add(AType &cElement);
00134 
00135         /**
00136         *  @brief
00137         *    Unload a element
00138         *
00139         *  @param[in] cElement
00140         *    Element which should be unloaded
00141         *
00142         *  @return
00143         *    'true' if all went fine, else 'false'
00144         *
00145         *  @note
00146         *    - Protected elements are NOT unloaded!
00147         */
00148         bool Unload(AType &cElement);
00149 
00150         /**
00151         *  @brief
00152         *    Removes a element
00153         *
00154         *  @param[in] cElement
00155         *    Element to remove
00156         *
00157         *  @return
00158         *    'true' if all went fine, else 'false' (maybe element is not within the manager)
00159         *
00160         *  @note
00161         *    - This function will only remove the element from the manager, but
00162         *      NOT deleting it!
00163         */
00164         bool Remove(AType &cElement);
00165 
00166         /**
00167         *  @brief
00168         *    Unload unused elements
00169         *
00170         *  @return
00171         *    Number of unloaded elements
00172         *
00173         *  @see
00174         *    - Unload()
00175         */
00176         uint32 UnloadUnused();
00177 
00178         /**
00179         *  @brief
00180         *    Returns whether unused element are unloaded automatically or not
00181         *
00182         *  @return
00183         *    'true' if unused elements are unloaded automatically, else 'false'
00184         */
00185         bool GetUnloadUnused() const;
00186 
00187         /**
00188         *  @brief
00189         *    Sets whether unused element are unloaded automatically or not
00190         *
00191         *  @param[in] bUnloadUnused
00192         *    Unload unused elements automatically?
00193         */
00194         void SetUnloadUnused(bool bUnloadUnused = false);
00195 
00196         /**
00197         *  @brief
00198         *    Get the standard element
00199         *
00200         *  @return
00201         *    The standard element, a null pointer if there's no such element
00202         *
00203         *  @note
00204         *    - There should always be a standard element!
00205         *    - It is recommended that you protect your standard element
00206         */
00207         AType *GetStandard() const;
00208 
00209         /**
00210         *  @brief
00211         *    Set the standard element
00212         *
00213         *  @param[in] pElement
00214         *    Element which should be the standard element, a null pointer to set no such element
00215         *
00216         *  @return
00217         *    'true' if all went fine, else 'false' (maybe invalid element)
00218         *
00219         *  @see
00220         *    - GetStandard()
00221         */
00222         bool SetStandard(AType *pElement = nullptr);
00223 
00224         /**
00225         *  @brief
00226         *    Returns the number of elements within the manager
00227         *
00228         *  @return
00229         *    Number of elements within the manager
00230         */
00231         uint32 GetNumOfElements() const;
00232 
00233 
00234     //[-------------------------------------------------------]
00235     //[ Public virtual functions                              ]
00236     //[-------------------------------------------------------]
00237     public:
00238         /**
00239         *  @brief
00240         *    Initializes the manager
00241         *
00242         *  @return
00243         *    'true' if all went fine, else 'false'
00244         */
00245         virtual bool Init();
00246 
00247         /**
00248         *  @brief
00249         *    De-initializes the manager
00250         *
00251         *  @return
00252         *    'true' if all went fine, else 'false'
00253         */
00254         virtual bool DeInit();
00255 
00256         /**
00257         *  @brief
00258         *    Updates the manager
00259         *
00260         *  @return
00261         *    'true' if all went fine, else 'false'
00262         */
00263         virtual bool Update();
00264 
00265         /**
00266         *  @brief
00267         *    Copy operator
00268         *
00269         *  @param[in] cSource
00270         *    Source to copy from
00271         *
00272         *  @return
00273         *    This instance
00274         */
00275         virtual ElementManager<AType> &operator =(const ElementManager<AType> &cSource);
00276 
00277         /**
00278         *  @brief
00279         *    Returns the element at the given index
00280         *
00281         *  @param[in] nIndex
00282         *    Index of the element
00283         *
00284         *  @return
00285         *    The corresponding object, a null pointer if there's no match
00286         */
00287         virtual AType *GetByIndex(uint32 nIndex = 0) const;
00288 
00289         /**
00290         *  @brief
00291         *    Returns the index of the given element
00292         *
00293         *  @param[in] cElement
00294         *    The element for which the index should be retrieved
00295         *
00296         *  @return
00297         *    The index position, <0 on failure (e.g. the element is no member of this manager)
00298         */
00299         virtual int GetIndex(AType &cElement) const;
00300 
00301         /**
00302         *  @brief
00303         *    Moves an element within the element list
00304         *
00305         *  @param[in] nFromIndex
00306         *    The index of the element which should be moved
00307         *  @param[in] nToIndex
00308         *    The index to which the element should be moved
00309         *
00310         *  @note
00311         *    - This methods assumes, that both index values are within 0 and GetNumOfElements()-1
00312         */
00313         virtual void MoveElement(uint32 nFromIndex, uint32 nToIndex);
00314 
00315         /**
00316         *  @brief
00317         *    Returns the element with the given name
00318         *
00319         *  @param[in] sName
00320         *    Element name
00321         *
00322         *  @return
00323         *    The corresponding element, a null pointer if there's no match
00324         *
00325         *  @note
00326         *    - You can overload this function to specialize the behavior. This is done
00327         *      for instance within SceneContainer to be able to use 'absolute names'
00328         *      like 'Root.MyScene.MyNode', too.
00329         */
00330         virtual AType *GetByName(const String &sName) const;
00331 
00332 
00333     //[-------------------------------------------------------]
00334     //[ Protected functions                                   ]
00335     //[-------------------------------------------------------]
00336     protected:
00337         /**
00338         *  @brief
00339         *    Constructor
00340         */
00341         ElementManager();
00342 
00343         /**
00344         *  @brief
00345         *    Destructor
00346         */
00347         virtual ~ElementManager();
00348 
00349         /**
00350         *  @brief
00351         *    Sets the element name
00352         *
00353         *  @param[in] cElement
00354         *    Element to set the name
00355         *  @param[in] sName
00356         *    New element name
00357         *
00358         *  @return
00359         *    'true' if all went fine, else 'false' (maybe the name is already used)
00360         *
00361         *  @see
00362         *    - GetName()
00363         *
00364         *  @note
00365         *    - You can overload this function if there are 'reserved' names which are NOT
00366         *      allowed to be used or if special characters are not allowed.
00367         */
00368         bool SetElementName(AType &cElement, const String &sName);
00369 
00370 
00371     //[-------------------------------------------------------]
00372     //[ Protected virtual functions                           ]
00373     //[-------------------------------------------------------]
00374     protected:
00375         /**
00376         *  @brief
00377         *    Creates a new element
00378         *
00379         *  @param[in] sName
00380         *    Element name
00381         *
00382         *  @return
00383         *    Pointer to the created element, a null pointer if there was an error
00384         *
00385         *  @note
00386         *    - This function is used inside the function Create(). If AType is an abstract
00387         *      class you have to overwrite this function to create an instance of the class.
00388         */
00389         virtual AType *CreateElement(const String &sName = "") = 0;
00390 
00391 
00392     //[-------------------------------------------------------]
00393     //[ Protected data                                        ]
00394     //[-------------------------------------------------------]
00395     protected:
00396         String                   m_sManagerName;        /**< Manager name */
00397         AType                   *m_pStandardElement;    /**< Standard element, can be a null pointer */
00398         bool                     m_bUnloadUnused;       /**< Unload unused resources? */
00399         Array<AType*>            m_lstElements;         /**< Element list */
00400         HashMap<String, AType*>  m_mapElements;         /**< Element map */
00401 
00402 
00403 };
00404 
00405 
00406 //[-------------------------------------------------------]
00407 //[ Namespace                                             ]
00408 //[-------------------------------------------------------]
00409 } // PLCore
00410 
00411 
00412 //[-------------------------------------------------------]
00413 //[ Implementation                                        ]
00414 //[-------------------------------------------------------]
00415 #include "PLCore/Container/ElementManager.inl"
00416 
00417 
00418 #endif // __PLCORE_ELEMENT_MANAGER_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:53
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported