message.h 6.3 KB

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