message.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "message_ScriptBinding.h"
  40. //////////////////////////////////////////////////////////////////////////
  41. namespace Sim
  42. {
  43. extern SimIdDictionary *gIdDictionary;
  44. }
  45. //////////////////////////////////////////////////////////////////////////
  46. // Constructor/Destructor
  47. //////////////////////////////////////////////////////////////////////////
  48. Message::Message()
  49. {
  50. mRefCount = 0;
  51. }
  52. IMPLEMENT_CONOBJECT(Message);
  53. //////////////////////////////////////////////////////////////////////////
  54. // Public Methods
  55. //////////////////////////////////////////////////////////////////////////
  56. SimObjectId Message::getNextMessageID()
  57. {
  58. for(S32 i = MessageObjectIdFirst;i < MessageObjectIdLast;i++)
  59. {
  60. if(Sim::gIdDictionary->find(i) == NULL)
  61. return i;
  62. }
  63. // Oh shit ...
  64. return 0xffffffff;
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. const char *Message::getType()
  68. {
  69. if(mClassName && mClassName[0] != 0)
  70. return mClassName;
  71. return getClassName();
  72. }
  73. //////////////////////////////////////////////////////////////////////////
  74. void Message::addReference()
  75. {
  76. mRefCount++;
  77. }
  78. void Message::freeReference()
  79. {
  80. AssertFatal(mRefCount >= 0, "Negative refcount ? Someone's not cleaning up properly!");
  81. mRefCount--;
  82. if(mRefCount <= 0)
  83. {
  84. // [tom, 8/26/2006] When messages are dispatched, the calling code assumes
  85. // that dispatchMessage() will free the message unless a reference has been
  86. // added.
  87. //
  88. // It is possible if dispatchMessage() fails for the message to not get added
  89. // to the sim, and thus we need to delete this; in that case.
  90. if( isProperlyAdded() )
  91. deleteObject();
  92. else
  93. delete this;
  94. return;
  95. }
  96. }