simComponent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SIMCOMPONENT_H_
  23. #define _SIMCOMPONENT_H_
  24. #ifndef _SIMBASE_H_
  25. #include "sim/simBase.h"
  26. #endif
  27. #ifndef _STREAM_H_
  28. #include "io/stream.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. class SimComponent : public SimObject
  32. {
  33. typedef SimObject Parent;
  34. private:
  35. VectorPtr<SimComponent *> mComponentList; ///< The Component List
  36. void *mMutex; ///< Component List Mutex
  37. SimObjectPtr<SimComponent> mOwner; ///< The component which owns this one.
  38. bool _registerComponents( SimComponent *owner );
  39. void _unregisterComponents();
  40. protected:
  41. bool mEnabled;
  42. // Non-const getOwner for derived classes
  43. SimComponent *_getOwner() { return mOwner; }
  44. /// Returns a const reference to private mComponentList
  45. typedef VectorPtr<SimComponent *>::iterator SimComponentIterator;
  46. VectorPtr<SimComponent *> &lockComponentList()
  47. {
  48. Mutex::lockMutex( mMutex );
  49. return mComponentList;
  50. };
  51. void unlockComponentList()
  52. {
  53. Mutex::unlockMutex( mMutex );
  54. }
  55. /// onComponentRegister is called on each component by it's owner. If a component
  56. /// has no owner, onComponentRegister will not be called on it. The purpose
  57. /// of onComponentRegister is to allow a component to check for any external
  58. /// interfaces, or other dependencies which it needs to function. If any component
  59. /// in a component hierarchy returns false from it's onComponentRegister call
  60. /// the entire hierarchy is invalid, and SimObject::onAdd will fail on the
  61. /// top-level component. To put it another way, if a component contains other
  62. /// components, it will be registered successfully with Sim if each subcomponent
  63. /// returns true from onComponentRegister. If a component does not contain
  64. /// other components, it will not receive an onComponentRegister call.
  65. ///
  66. /// Overloads of this method must pass the call along to their parent, as is
  67. /// shown in the example below.
  68. ///
  69. /// @code
  70. /// bool FooComponent::onComponentRegister( SimComponent *owner )
  71. /// {
  72. /// if( !Parent::onComponentRegister( owner ) )
  73. /// return false;
  74. /// ...
  75. /// }
  76. /// @endcode
  77. virtual bool onComponentRegister( SimComponent *owner )
  78. {
  79. mOwner = owner;
  80. return true;
  81. }
  82. /// onUnregister is called when the owner is unregistering. Your object should
  83. /// do cleanup here, as well as pass a call up the chain to the parent.
  84. virtual void onComponentUnRegister()
  85. {
  86. mOwner = NULL;
  87. }
  88. public:
  89. DECLARE_CONOBJECT(SimComponent);
  90. /// Constructor
  91. /// Add this component
  92. SimComponent();
  93. /// Destructor
  94. /// Remove this component and destroy child references
  95. virtual ~SimComponent();
  96. public:
  97. virtual bool onAdd();
  98. virtual void onRemove();
  99. static void initPersistFields();
  100. virtual bool processArguments(S32 argc, const char **argv);
  101. /// Will return true if this object contains components.
  102. bool hasComponents() const { return ( mComponentList.size() > 0 ); };
  103. /// The component which owns this object
  104. const SimComponent *getOwner() const { return mOwner; };
  105. // Component Information
  106. inline virtual StringTableEntry getComponentName() { return StringTable->insert( getClassName() ); };
  107. /// Add Component to this one
  108. virtual bool addComponent( SimComponent *component );
  109. /// Remove Component from this one
  110. virtual bool removeComponent( SimComponent *component );
  111. /// Clear Child components of this one
  112. virtual bool clearComponents() { mComponentList.clear(); return true; };
  113. virtual bool onComponentAdd(SimComponent *target);
  114. virtual void onComponentRemove(SimComponent *target);
  115. inline U32 getComponentCount() { return mComponentList.size(); }
  116. inline SimComponent *getComponent( const U32 index ) { return mComponentList[index]; }
  117. static bool setEnabled( void* obj, const char* data ) { static_cast<SimComponent*>(obj)->setEnabled( dAtob( data ) ); return false; };
  118. virtual void setEnabled( const bool enabled ) { mEnabled = enabled; }
  119. bool isEnabled() const { return mEnabled; }
  120. static bool writeEnabled( void* obj, StringTableEntry pFieldName ) { return static_cast<SimComponent*>(obj)->mEnabled == false; }
  121. virtual void write(Stream &stream, U32 tabStop, U32 flags = 0);
  122. virtual bool writeField(StringTableEntry fieldname, const char* value);
  123. virtual void onUpdate( void ) {}
  124. virtual void onAddToScene( void ) {}
  125. virtual void onRemoveFromScene( void ) {}
  126. /// Call the given method and arguments on this component or, if not handled by
  127. /// this component, then on its children.
  128. /// @return True if the given method and arguments was handled by the component or
  129. /// one of its children.
  130. bool callMethodOnComponents( U32 argc, const char* argv[], const char** result );
  131. };
  132. #endif // _SIMCOMPONENT_H_