message.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.cc
  28. ** Author: Tom Bampton
  29. ** Created: 19/8/2006
  30. ** Purpose:
  31. ** Message
  32. **
  33. */
  34. #include "platform/platform.h"
  35. #include "console/consoleTypes.h"
  36. #include "messaging/message.h"
  37. #include "memory/safeDelete.h"
  38. #include "io/bitStream.h"
  39. //////////////////////////////////////////////////////////////////////////
  40. namespace Sim
  41. {
  42. extern SimIdDictionary *gIdDictionary;
  43. }
  44. //////////////////////////////////////////////////////////////////////////
  45. // Constructor/Destructor
  46. //////////////////////////////////////////////////////////////////////////
  47. Message::Message()
  48. {
  49. mRefCount = 0;
  50. mNSLinkMask = LinkSuperClassName | LinkClassName;
  51. }
  52. IMPLEMENT_CONOBJECT(Message);
  53. //////////////////////////////////////////////////////////////////////////
  54. bool Message::onAdd()
  55. {
  56. if(! Parent::onAdd())
  57. return false;
  58. linkNamespaces();
  59. Con::executef(this, 1, "onAdd");
  60. return true;
  61. }
  62. void Message::onRemove()
  63. {
  64. Con::executef(this, 1, "onRemove");
  65. unlinkNamespaces();
  66. Parent::onRemove();
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. // Public Methods
  70. //////////////////////////////////////////////////////////////////////////
  71. SimObjectId Message::getNextMessageID()
  72. {
  73. for(S32 i = MessageObjectIdFirst;i < MessageObjectIdLast;i++)
  74. {
  75. if(Sim::gIdDictionary->find(i) == NULL)
  76. return i;
  77. }
  78. // Oh shit ...
  79. return 0xffffffff;
  80. }
  81. //////////////////////////////////////////////////////////////////////////
  82. const char *Message::getType()
  83. {
  84. if(mClassName && mClassName[0] != 0)
  85. return mClassName;
  86. return getClassName();
  87. }
  88. //////////////////////////////////////////////////////////////////////////
  89. void Message::addReference()
  90. {
  91. mRefCount++;
  92. }
  93. void Message::freeReference()
  94. {
  95. AssertFatal(mRefCount >= 0, "Negative refcount ? Someone's not cleaning up properly!");
  96. mRefCount--;
  97. if(mRefCount <= 0)
  98. {
  99. // [tom, 8/26/2006] When messages are dispatched, the calling code assumes
  100. // that dispatchMessage() will free the message unless a reference has been
  101. // added.
  102. //
  103. // It is possible if dispatchMessage() fails for the message to not get added
  104. // to the sim, and thus we need to delete this; in that case.
  105. if( isProperlyAdded() )
  106. deleteObject();
  107. else
  108. delete this;
  109. return;
  110. }
  111. }
  112. //////////////////////////////////////////////////////////////////////////
  113. // Console Methods
  114. //////////////////////////////////////////////////////////////////////////
  115. ConsoleMethod(Message, getType, const char *, 2, 2, "() Get message type (script class name or C++ class name if no script defined class)"
  116. "@return The type as a string")
  117. {
  118. return object->getType();
  119. }
  120. //////////////////////////////////////////////////////////////////////////
  121. ConsoleMethod(Message, addReference, void, 2, 2, "() Increment the reference count for this message\n"
  122. "@return No Return Value.")
  123. {
  124. object->addReference();
  125. }
  126. ConsoleMethod(Message, freeReference, void, 2, 2, "() Decrement the reference count for this message\n"
  127. "@return No Return Value.")
  128. {
  129. object->freeReference();
  130. }