message.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "platform/platform.h"
  23. #include "util/messaging/message.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/util/safeDelete.h"
  26. #include "core/stream/bitStream.h"
  27. #include "console/engineAPI.h"
  28. //-----------------------------------------------------------------------------
  29. namespace Sim
  30. {
  31. extern SimIdDictionary *gIdDictionary;
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Constructor/Destructor
  35. //-----------------------------------------------------------------------------
  36. Message::Message()
  37. {
  38. }
  39. IMPLEMENT_CONOBJECT(Message);
  40. ConsoleDocClass( Message,
  41. "@brief Base class for messages\n\n"
  42. "Message is the base class for C++ defined messages, and may also be used "
  43. "in script for script defined messages if no C++ subclass is appropriate.\n\n"
  44. "Messages are reference counted and will be automatically deleted when "
  45. "their reference count reaches zero. When you dispatch a message, a "
  46. "reference will be added before the dispatch and freed after the dispatch. "
  47. "This allows for temporary messages with no additional code. If you want "
  48. "to keep the message around, for example to dispatch it to multiple "
  49. "queues, call addReference() before dispatching it and freeReference() "
  50. "when you are done with it. Never delete a Message object directly "
  51. "unless addReference() has not been called or the message has not been "
  52. "dispatched.\n\n"
  53. "Message IDs are pooled similarly to datablocks, with the exception that "
  54. "IDs are reused. If you keep a message for longer than a single dispatch, "
  55. "then you should ensure that you clear any script variables that refer "
  56. "to it after the last freeReference(). If you don't, then it is probable "
  57. "that the object ID will become valid again in the future and could cause "
  58. "hard to track down bugs.\n\n"
  59. "Messages have a unique type to simplify message handling code. For object "
  60. "messages, the type is defined as either the script defined class name "
  61. "or the C++ class name if no script class was defined. The message type "
  62. "may be obtained through the getType() method.\n\n"
  63. "By convention, any data for the message is held in script accessible "
  64. "fields. Messages that need to be handled in C++ as well as script "
  65. "provide the relevant data through persistent fields in a subclass of "
  66. "Message to provide best performance on the C++ side. Script defined "
  67. "messages usually their through dynamic fields, and may be accessed in "
  68. "C++ using the SimObject::getDataField() method.\n\n"
  69. "@ingroup Messaging\n"
  70. );
  71. //-----------------------------------------------------------------------------
  72. IMPLEMENT_CALLBACK(Message, onAdd, void, (),(),
  73. "Script callback when a message is first created and registered.\n\n"
  74. "@tsexample\n"
  75. "function Message::onAdd(%this)\n"
  76. "{\n"
  77. " // Perform on add code here\n"
  78. "}\n"
  79. "@endtsexample\n\n"
  80. );
  81. bool Message::onAdd()
  82. {
  83. if(! Parent::onAdd())
  84. return false;
  85. onAdd_callback();
  86. //Con::executef(this, "onAdd");
  87. return true;
  88. }
  89. IMPLEMENT_CALLBACK(Message, onRemove, void, (),(),
  90. "Script callback when a message is deleted.\n\n"
  91. "@tsexample\n"
  92. "function Message::onRemove(%this)\n"
  93. "{\n"
  94. " // Perform on remove code here\n"
  95. "}\n"
  96. "@endtsexample\n\n"
  97. );
  98. void Message::onRemove()
  99. {
  100. onRemove_callback();
  101. //Con::executef(this, "onRemove");
  102. Parent::onRemove();
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Public Methods
  106. //-----------------------------------------------------------------------------
  107. SimObjectId Message::getNextMessageID()
  108. {
  109. for(S32 i = MessageObjectIdFirst;i < MessageObjectIdLast;i++)
  110. {
  111. if(Sim::gIdDictionary->find(i) == NULL)
  112. return i;
  113. }
  114. // Oh no ...
  115. return 0xffffffff;
  116. }
  117. //-----------------------------------------------------------------------------
  118. const char *Message::getType()
  119. {
  120. if(mClassName && mClassName[0] != 0)
  121. return mClassName;
  122. return getClassName();
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Console Methods
  126. //-----------------------------------------------------------------------------
  127. DefineEngineMethod(Message, getType, const char *, (), , "() Get message type (script class name or C++ class name if no script defined class)")
  128. {
  129. return object->getType();
  130. }
  131. //-----------------------------------------------------------------------------
  132. DefineEngineMethod(Message, addReference, void, (), , "() Increment the reference count for this message")
  133. {
  134. object->addReference();
  135. }
  136. DefineEngineMethod(Message, freeReference, void, (), , "() Decrement the reference count for this message")
  137. {
  138. object->freeReference();
  139. }