simpleComponent.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "component/simpleComponent.h"
  23. IMPLEMENT_CONOBJECT(SimpleComponent);
  24. ConsoleDocClass( SimpleComponent,
  25. "@brief The purpose of this component is to provide a minimalistic component that "
  26. "exposes a simple, cached interface\n\n"
  27. "Soon to be deprecated, internal only.\n\n "
  28. "@internal");
  29. //////////////////////////////////////////////////////////////////////////
  30. // It may seem like some weak sauce to use a unit test for this, however
  31. // it is very, very easy to set breakpoints in a unit test, and trace
  32. // execution in the debugger, so I will use a unit test.
  33. //
  34. // Note I am not using much actual 'test' functionality, just providing
  35. // an easy place to examine the functionality that was described and implemented
  36. // in the header file.
  37. //
  38. // If you want to run this code, simply run Torque, pull down the console, and
  39. // type:
  40. // unitTest_runTests("Components/SimpleComponent");
  41. #include "unit/test.h"
  42. using namespace UnitTesting;
  43. CreateUnitTest(TestSimpleComponent, "Components/SimpleComponent")
  44. {
  45. void run()
  46. {
  47. // When instantiating, and working with a SimObject in C++ code, such as
  48. // a unit test, you *may not* allocate a SimObject off of the stack.
  49. //
  50. // For example:
  51. // SimpleComponent sc;
  52. // is a stack allocation. This memory is allocated off of the program stack
  53. // when the function is called. SimObject deletion is done via SimObject::deleteObject()
  54. // and the last command of this method is 'delete this;' That command will
  55. // cause an assert if it is called on stack-allocated memory. Therefor, when
  56. // instantiating SimObjects in C++ code, it is imperitive that you keep in
  57. // mind that if any script calls 'delete()' on that SimObject, or any other
  58. // C++ code calls 'deleteObject()' on that SimObject, it will crash.
  59. SimpleComponent *sc = new SimpleComponent();
  60. // SimObject::registerObject must be called on a SimObject before it is
  61. // fully 'hooked in' to the engine.
  62. //
  63. // Tracing execution of this function will let you see onAdd get called on
  64. // the component, and you will see it cache the interface we exposed.
  65. sc->registerObject();
  66. // It is *not* required that a component always be owned by a component (obviously)
  67. // however I am using an owner so that you can trace execution of recursive
  68. // calls to cache interfaces and such.
  69. SimComponent *testOwner = new SimComponent();
  70. // Add the test component to it's owner. This will set the 'mOwner' field
  71. // of 'sc' to the address of 'testOwner'
  72. testOwner->addComponent( sc );
  73. // If you step-into this registerObject the same way as the previous one,
  74. // you will be able to see the recursive caching of the exposed interface.
  75. testOwner->registerObject();
  76. // Now to prove that object composition is working properly, lets ask
  77. // both of these components for their interface lists...
  78. // The ComponentInterfaceList is a typedef for type 'VectorPtr<ComponentInterface *>'
  79. // and it will be used by getInterfaces() to store the results of the interface
  80. // query. This is the "complete" way to obtain an interface, and it is too
  81. // heavy-weight for most cases. A simplified query will be performed next,
  82. // to demonstrate the usage of both.
  83. ComponentInterfaceList iLst;
  84. // This query requests all interfaces, on all components, regardless of name
  85. // or owner.
  86. sc->getInterfaces( &iLst,
  87. // This is the type field. I am passing NULL here to signify that the query
  88. // should match all values of 'type' in the list.
  89. NULL,
  90. // The name field, let's pass NULL again just so when you trace execution
  91. // you can see how queries work in the simple case, first.
  92. NULL );
  93. // Lets process the list that we've gotten back, and find the interface that
  94. // we want.
  95. SimpleComponentInterface *scQueriedInterface = NULL;
  96. for( ComponentInterfaceListIterator i = iLst.begin(); i != iLst.end(); i++ )
  97. {
  98. scQueriedInterface = dynamic_cast<SimpleComponentInterface *>( *i );
  99. if( scQueriedInterface != NULL )
  100. break;
  101. }
  102. AssertFatal( scQueriedInterface != NULL, "No valid SimpleComponentInterface was found in query" );
  103. // Lets do it again, only we will execute the query on the parent instead,
  104. // in a simplified way. Remember the parent component doesn't expose any
  105. // interfaces at all, so the success of this behavior is entirely dependent
  106. // on the recursive registration that occurs in registerInterfaces()
  107. SimpleComponentInterface *ownerQueriedInterface = testOwner->getInterface<SimpleComponentInterface>();
  108. AssertFatal( ownerQueriedInterface != NULL, "No valid SimpleComponentInterface was found in query" );
  109. // We should now have two pointers to the same interface obtained by querying
  110. // different components.
  111. test( ownerQueriedInterface == scQueriedInterface, "This really shouldn't be possible to fail given the setup of the test" );
  112. // Lets call the method that was exposed on the component via the interface.
  113. // Trace the execution of this function, if you wish.
  114. test( ownerQueriedInterface->isFortyTwo( 42 ), "Don't panic, but it's a bad day in the component system." );
  115. test( scQueriedInterface->isFortyTwo( 42 ), "Don't panic, but it's a bad day in the component system." );
  116. // So there you have it. Writing a simple component that exposes a cached
  117. // interface, and testing it. It's time to clean up.
  118. testOwner->removeComponent( sc );
  119. sc->deleteObject();
  120. testOwner->deleteObject();
  121. // Interfaces do not need to be freed. In Juggernaught, these will be ref-counted
  122. // for more robust behavior. Right now, however, the values of our two interface
  123. // pointers, scQueriedInterface and ownerQueriedInterface, reference invalid
  124. // memory.
  125. }
  126. };