HandleObject.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include <stddef.h>
  23. namespace se {
  24. class Object;
  25. /**
  26. * HandleObject is a helper class for easily release, root and unroot an non-native-binding se::Object.
  27. {
  28. se::HandleObject obj(se::Object::createPlainObject());
  29. obj->setProperty(...);
  30. otherObject->setProperty("foo", se::Value(obj));
  31. }
  32. is equal to:
  33. {
  34. se::Object* obj = se::Object::createPlainObject();
  35. obj->root(); // root after object created to avoid object is garbage collected
  36. obj->setProperty(...);
  37. otherObject->setProperty("foo", se::Value(obj));
  38. obj->unroot(); // unroot object after obj is used.
  39. obj->decRef(); // Decrement referent count to avoid memory leak.
  40. }
  41. HandleObject should not be used to create a native binding object since the created binding object
  42. should be holded by JavaScript VM and released in finalize callback internally.
  43. */
  44. class HandleObject
  45. {
  46. public:
  47. /**
  48. * @brief The constructor of HandleObject
  49. * @param[in] obj The se::Object to attach.
  50. */
  51. HandleObject(Object* obj);
  52. /**
  53. * @brief The destructor of HandleObject
  54. */
  55. ~HandleObject();
  56. /**
  57. * @brief The pointer operator
  58. * @return The se::Object attached.
  59. */
  60. inline Object* operator->() const
  61. {
  62. return _obj;
  63. }
  64. /**
  65. * @brief Gets the se::Object attached.
  66. * @return The se::Object attached.
  67. */
  68. inline Object* get() const
  69. {
  70. return _obj;
  71. }
  72. /**
  73. * @brief Tests whether HandleObject holds an invalid se::Object.
  74. * @return true if HandleObject holds an invalid se::Object, otherwise false.
  75. */
  76. inline bool isEmpty() const
  77. {
  78. return (_obj == nullptr);
  79. }
  80. private:
  81. HandleObject(const HandleObject&) = delete;
  82. void operator=(const HandleObject&) = delete;
  83. HandleObject(HandleObject&&) = delete;
  84. void operator=(HandleObject&&) = delete;
  85. void* operator new(size_t size) = delete;
  86. void operator delete(void*, size_t) = delete;
  87. Object* _obj;
  88. friend class Object;
  89. };
  90. } // namespace se {