Component.hpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #ifndef Component_hpp
00023 #define Component_hpp
00024 
00025 #include "comp.hpp"
00026 #include "Composite.hpp"
00027 #include "util/error/Log.hpp"
00028 #include "util/type/String.hpp"
00029 
00030 namespace se_core {
00034     class _SeCoreExport Component {
00035     public:
00041         Component(int type, Composite* owner, const ComponentFactory* factory = 0);
00042 
00046         virtual const char* name() const;
00047 
00051         virtual ~Component();
00052 
00053 
00054         inline Composite* owner() {
00055             return owner_;
00056         }
00057 
00058 
00062         inline const Composite* owner() const {
00063             return owner_;
00064         }
00065 
00066 
00067         inline Component* component(int type) {
00068             return owner_->component(type);
00069         }
00070 
00071         inline const Component* component(int type) const {
00072             return owner_->component(type);
00073         }
00074 
00075 
00076         int type() const {
00077             return type_;
00078         }
00079 
00080 
00081         const ComponentFactory* factory() {
00082             return factory_;
00083         }
00084 
00085         bool isActive();
00086         bool isDead() const;
00087 
00088     protected:
00089         friend class Composite;
00090 
00093         virtual void setActive(bool state) {}
00094 
00097         virtual void setDead() {}
00098 
00101         virtual void parentChanged(Composite* newParent, Composite* oldParent) {}
00102 
00105         virtual void zoneChanged(int type, Composite* newArea, Composite* oldArea);
00106 
00109         virtual void init();
00112         virtual void cleanup();
00113 
00117         int type_;
00118         Composite* owner_;
00119 
00120         const ComponentFactory* factory_;
00121 
00122     protected:
00132         template <class T, int type> class ComponentPtr {
00133         public:
00134             enum { TYPE = type };
00135 
00136             ComponentPtr(T* c) {
00137                 component_ = c;
00138             }
00139             ComponentPtr(T& c) {
00140                 component_ = &c;
00141             }
00142             ComponentPtr(Component& c) {
00143                 component_ = static_cast<T*>(c.component(TYPE));
00144             }
00145             ComponentPtr(Component* c) {
00146                 if(c)
00147                     component_ = static_cast<T*>(c->component(TYPE));
00148                 else
00149                     component_ = 0;
00150             }
00151             ComponentPtr(Composite* c) {
00152                 if(c)
00153                     component_ = static_cast<T*>(c->component(TYPE));
00154                 else
00155                     component_ = 0;
00156             }
00157             ComponentPtr(Composite& c) {
00158                 component_ = static_cast<T*>(c.component(TYPE));
00159             }
00160 
00161             ComponentPtr(const Component& c) {
00162                 component_ = (T*)(c.owner()->component(TYPE));
00163             }
00164             ComponentPtr(const Composite& c) {
00165                 component_ = (T*)(c.component(TYPE));
00166             }
00167             ComponentPtr(const Component* c) {
00168                 if(c)
00169                     component_ = (T*)(c->owner()->component(TYPE));
00170                 else
00171                     component_ = 0;
00172             }
00173             ComponentPtr(const Composite* c) {
00174                 if(c)
00175                     component_ = (T*)(c->component(TYPE));
00176                 else
00177                     component_ = 0;
00178             }
00179 
00180             inline T* operator->() {
00181                 Assert(component_);
00182                 return component_;
00183             }
00184 
00185             inline T& operator*() {
00186                 Assert(component_);
00187                 return *component_;
00188             }
00189 
00190             inline const T* operator->() const {
00191                 Assert(component_);
00192                 return component_;
00193             }
00194 
00195             inline const T& operator*() const {
00196                 Assert(component_);
00197                 return *component_;
00198             }
00199 
00200             ComponentPtr& operator=(T& c) {
00201                 component_ = &c;
00202                 return *this;
00203             }
00204 
00205             ComponentPtr& operator=(T* c) {
00206                 component_ = c;
00207                 return *this;
00208             }
00209 
00210             ComponentPtr& operator=(Component* c) {
00211                 if(c)
00212                     component_ = static_cast<T*>(c->owner()->component(TYPE));
00213                 else
00214                     component_ = 0;
00215                 return *this;
00216             }
00217 
00218             ComponentPtr& operator=(Component& c) {
00219                 component_ = static_cast<T*>(c.owner()->component(TYPE));
00220                 return *this;
00221             }
00222 
00223             ComponentPtr& operator=(Composite* c) {
00224                 if(c)
00225                     component_ = static_cast<T*>(c->component(TYPE));
00226                 else
00227                     component_ = 0;
00228                 return *this;
00229             }
00230 
00231             ComponentPtr& operator=(Composite& c) {
00232                 component_ = static_cast<T*>(c.component(TYPE));
00233                 return *this;
00234             }
00235 
00236             operator T*() {
00237                 return component_;
00238             }
00239 
00240             operator const T*() const {
00241                 return component_;
00242             }
00243 
00244             bool isNull() const {
00245                 return component_ == 0;
00246             }
00247 
00248             bool operator==(const T* c) const {
00249                 return component_ == c;
00250             }
00251 
00252         private:
00253             T* component_;
00254         };
00255     };
00256 
00257 }
00258 
00259 #endif

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:10 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo