2
0

message.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef _MESSAGE_H_
  23. #define _MESSAGE_H_
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. #ifndef _NETCONNECTION_H_
  28. #include "sim/netConnection.h"
  29. #endif
  30. // Forward Refs
  31. class MessageQueue;
  32. /// @addtogroup msgsys Message System
  33. ///
  34. /// Most of the message system docs are currently just stubs and will
  35. /// be fleshed out soon.
  36. ///
  37. // @{
  38. //-----------------------------------------------------------------------------
  39. // Message Base Class
  40. //-----------------------------------------------------------------------------
  41. //-----------------------------------------------------------------------------
  42. /// @brief Base class for messages
  43. ///
  44. /// Message is the base class for C++ defined messages, and may also be used
  45. /// in script for script defined messages if no C++ subclass is appropriate.
  46. ///
  47. /// Messages are reference counted and will be automatically deleted when
  48. /// their reference count reaches zero. When you dispatch a message, a
  49. /// reference will be added before the dispatch and freed after the dispatch.
  50. /// This allows for temporary messages with no additional code. If you want
  51. /// to keep the message around, for example to dispatch it to multiple
  52. /// queues, call addReference() before dispatching it and freeReference()
  53. /// when you are done with it. Never delete a Message object directly
  54. /// unless addReference() has not been called or the message has not been
  55. /// dispatched.
  56. ///
  57. /// Message IDs are pooled similarly to datablocks, with the exception that
  58. /// IDs are reused. If you keep a message for longer than a single dispatch,
  59. /// then you should ensure that you clear any script variables that refer
  60. /// to it after the last freeReference(). If you don't, then it is probable
  61. /// that the object ID will become valid again in the future and could cause
  62. /// hard to track down bugs.
  63. ///
  64. /// Messages have a unique type to simplify message handling code. For object
  65. /// messages, the type is defined as either the script defined class name
  66. /// or the C++ class name if no script class was defined. The message type
  67. /// may be obtained through the getType() method.
  68. ///
  69. /// By convention, any data for the message is held in script accessible
  70. /// fields. Messages that need to be handled in C++ as well as script
  71. /// provide the relevant data through persistent fields in a subclass of
  72. /// Message to provide best performance on the C++ side. Script defined
  73. /// messages usually their through dynamic fields, and may be accessed in
  74. /// C++ using the SimObject::getDataField() method.
  75. //-----------------------------------------------------------------------------
  76. class Message : public SimObject
  77. {
  78. typedef SimObject Parent;
  79. public:
  80. Message();
  81. DECLARE_CONOBJECT(Message);
  82. DECLARE_CALLBACK( void, onAdd, () );
  83. DECLARE_CALLBACK( void, onRemove, () );
  84. //-----------------------------------------------------------------------------
  85. /// @brief Obtain next available #SimObjectId for messages
  86. ///
  87. /// This is used in combination with the newmsg script operator to provide
  88. /// ID pooling for messages and works similarly to datablock IDs.
  89. ///
  90. /// By default, the 64 IDs following the datablock IDs are used for messages.
  91. /// As message objects generally have a short life time this prevents them
  92. /// from eating object IDs as if they haven't eaten for a year.
  93. ///
  94. /// Note that unlike SimObjects and datablocks, Messages IDs are re-used.
  95. /// If you store a message object in script and do not clear the variable
  96. /// containing the object ID after freeing the message, it is probable that
  97. /// the object ID will become valid again.
  98. ///
  99. /// @return Next available SimObjectId
  100. //-----------------------------------------------------------------------------
  101. static SimObjectId getNextMessageID();
  102. virtual bool onAdd();
  103. virtual void onRemove();
  104. //-----------------------------------------------------------------------------
  105. /// @brief Get the type of the message
  106. ///
  107. /// The message type is either the script class name or the C++ class name
  108. /// if it has not been overridden in script. This allows easy identification
  109. /// of message types with minimum effort.
  110. ///
  111. /// @return Type of message
  112. //-----------------------------------------------------------------------------
  113. const char *getType();
  114. //-----------------------------------------------------------------------------
  115. /// @brief Add a reference to the reference count of this message
  116. ///
  117. /// Use freeReference() to free the reference when you are done with it.
  118. ///
  119. /// @see freeReference()
  120. //-----------------------------------------------------------------------------
  121. void addReference() { incRefCount(); }
  122. //-----------------------------------------------------------------------------
  123. /// @brief Free a reference to this message
  124. ///
  125. /// @see addReference()
  126. //-----------------------------------------------------------------------------
  127. void freeReference() { decRefCount(); }
  128. };
  129. // @}
  130. #endif // _MESSAGE_H_