/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_globalproperty.cpp

http://github.com/xbmc/xbmc · C++ · 107 lines · 52 code · 15 blank · 40 comment · 10 complexity · 837d5eccb498bf4fb4ecb91aa14b72dd MD5 · raw file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. #include "as_config.h"
  24. #include "as_property.h"
  25. BEGIN_AS_NAMESPACE
  26. asCGlobalProperty::asCGlobalProperty()
  27. {
  28. memory = 0;
  29. memoryAllocated = false;
  30. realAddress = 0;
  31. initFunc = 0;
  32. refCount.set(1);
  33. }
  34. asCGlobalProperty::~asCGlobalProperty()
  35. {
  36. if( memoryAllocated ) { asDELETEARRAY(memory); }
  37. if( initFunc )
  38. initFunc->Release();
  39. }
  40. void asCGlobalProperty::AddRef()
  41. {
  42. refCount.atomicInc();
  43. }
  44. void asCGlobalProperty::Release()
  45. {
  46. // The property doesn't delete itself. The
  47. // engine will do that at a later time
  48. if( refCount.atomicDec() == 1 && initFunc )
  49. {
  50. // Since the initFunc holds a reference to the property,
  51. // we'll release it when we reach refCount 1. This will
  52. // break the circle and allow the engine to free the property
  53. // without the need for a GC run.
  54. initFunc->Release();
  55. initFunc = 0;
  56. }
  57. }
  58. void *asCGlobalProperty::GetAddressOfValue()
  59. {
  60. return (memoryAllocated || realAddress) ? memory : &storage;
  61. }
  62. // The global property structure is responsible for allocating the storage
  63. // method for script declared variables. Each allocation is independent of
  64. // other global properties, so that variables can be added and removed at
  65. // any time.
  66. void asCGlobalProperty::AllocateMemory()
  67. {
  68. if( type.GetSizeOnStackDWords() > 2 )
  69. {
  70. memory = asNEWARRAY(asDWORD, type.GetSizeOnStackDWords());
  71. memoryAllocated = true;
  72. }
  73. }
  74. void asCGlobalProperty::SetRegisteredAddress(void *p)
  75. {
  76. realAddress = p;
  77. if( type.IsObject() && !type.IsReference() && !type.IsObjectHandle() )
  78. {
  79. // The global property is a pointer to a pointer
  80. memory = &realAddress;
  81. }
  82. else
  83. memory = p;
  84. }
  85. END_AS_NAMESPACE