message.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  51. IMPLEMENT_CONOBJECT(Message);
  52. //////////////////////////////////////////////////////////////////////////
  53. // Public Methods
  54. //////////////////////////////////////////////////////////////////////////
  55. SimObjectId Message::getNextMessageID()
  56. {
  57. for(S32 i = MessageObjectIdFirst;i < MessageObjectIdLast;i++)
  58. {
  59. if(Sim::gIdDictionary->find(i) == NULL)
  60. return i;
  61. }
  62. // Oh shit ...
  63. return 0xffffffff;
  64. }
  65. //////////////////////////////////////////////////////////////////////////
  66. const char *Message::getType()
  67. {
  68. if(mClassName && mClassName[0] != 0)
  69. return mClassName;
  70. return getClassName();
  71. }
  72. //////////////////////////////////////////////////////////////////////////
  73. void Message::addReference()
  74. {
  75. mRefCount++;
  76. }
  77. void Message::freeReference()
  78. {
  79. AssertFatal(mRefCount >= 0, "Negative refcount ? Someone's not cleaning up properly!");
  80. mRefCount--;
  81. if(mRefCount <= 0)
  82. {
  83. // [tom, 8/26/2006] When messages are dispatched, the calling code assumes
  84. // that dispatchMessage() will free the message unless a reference has been
  85. // added.
  86. //
  87. // It is possible if dispatchMessage() fails for the message to not get added
  88. // to the sim, and thus we need to delete this; in that case.
  89. if( isProperlyAdded() )
  90. deleteObject();
  91. else
  92. delete this;
  93. return;
  94. }
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. // Console Methods
  98. //////////////////////////////////////////////////////////////////////////
  99. ConsoleMethod(Message, getType, const char *, 2, 2, "() Get message type (script class name or C++ class name if no script defined class)"
  100. "@return The type as a string")
  101. {
  102. return object->getType();
  103. }
  104. //////////////////////////////////////////////////////////////////////////
  105. ConsoleMethod(Message, addReference, void, 2, 2, "() Increment the reference count for this message\n"
  106. "@return No Return Value.")
  107. {
  108. object->addReference();
  109. }
  110. ConsoleMethod(Message, freeReference, void, 2, 2, "() Decrement the reference count for this message\n"
  111. "@return No Return Value.")
  112. {
  113. object->freeReference();
  114. }