reference.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*************************************************************************/
  2. /* reference.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "reference.h"
  30. bool Reference::init_ref() {
  31. if (refcount.ref()) {
  32. // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
  33. // at the same time, which is never likely to happen (would be crazy to do)
  34. // so don't do it.
  35. if (refcount_init.get()>0) {
  36. refcount_init.unref();
  37. refcount.unref(); // first referencing is already 1, so compensate for the ref above
  38. }
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. void Reference::_bind_methods() {
  45. ObjectTypeDB::bind_method(_MD("init_ref"),&Reference::init_ref);
  46. ObjectTypeDB::bind_method(_MD("reference"),&Reference::reference);
  47. ObjectTypeDB::bind_method(_MD("unreference"),&Reference::unreference);
  48. }
  49. int Reference::reference_get_count() const {
  50. return refcount.get();
  51. }
  52. void Reference::reference(){
  53. refcount.ref();
  54. }
  55. bool Reference::unreference(){
  56. return refcount.unref();
  57. }
  58. Reference::Reference() {
  59. refcount.init();
  60. refcount_init.init();
  61. }
  62. Reference::~Reference() {
  63. }
  64. Variant WeakRef::get_ref() const {
  65. if (ref==0)
  66. return Variant();
  67. Object *obj = ObjectDB::get_instance(ref);
  68. if (!obj)
  69. return Variant();
  70. Reference *r = obj->cast_to<Reference>();
  71. if (r) {
  72. return REF(r);
  73. }
  74. return obj;
  75. }
  76. void WeakRef::set_obj(Object *p_object) {
  77. ref=p_object ? p_object->get_instance_ID() : 0;
  78. }
  79. void WeakRef::set_ref(const REF& p_ref) {
  80. ref=p_ref.is_valid() ? p_ref->get_instance_ID() : 0;
  81. }
  82. WeakRef::WeakRef() {
  83. ref=0;
  84. }
  85. void WeakRef::_bind_methods() {
  86. ObjectTypeDB::bind_method(_MD("get_ref:Object"),&WeakRef::get_ref);
  87. }
  88. #if 0
  89. Reference * RefBase::get_reference_from_ref(const RefBase &p_base) {
  90. return p_base.get_reference();
  91. }
  92. void RefBase::ref_inc(Reference *p_reference) {
  93. p_reference->refcount.ref();
  94. }
  95. bool RefBase::ref_dec(Reference *p_reference) {
  96. bool ref = p_reference->refcount.unref();
  97. return ref;
  98. }
  99. Reference *RefBase::first_ref(Reference *p_reference) {
  100. if (p_reference->refcount.ref()) {
  101. // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
  102. // at the same time, which is never likely to happen (would be crazy to do)
  103. // so don't do it.
  104. if (p_reference->refcount_init.get()>0) {
  105. p_reference->refcount_init.unref();
  106. p_reference->refcount.unref(); // first referencing is already 1, so compensate for the ref above
  107. }
  108. return p_reference;
  109. } else {
  110. return 0;
  111. }
  112. }
  113. char * RefBase::get_refptr_data(const RefPtr &p_refptr) const {
  114. return p_refptr.data;
  115. }
  116. #endif