reference.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* reference.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. void Reference::reference(){
  50. refcount.ref();
  51. }
  52. bool Reference::unreference(){
  53. return refcount.unref();
  54. }
  55. Reference::Reference() {
  56. refcount.init();
  57. refcount_init.init();
  58. }
  59. Reference::~Reference() {
  60. }
  61. Variant WeakRef::get_ref() const {
  62. if (ref==0)
  63. return Variant();
  64. Object *obj = ObjectDB::get_instance(ref);
  65. if (!obj)
  66. return Variant();
  67. Reference *r = obj->cast_to<Reference>();
  68. if (r) {
  69. return REF(r);
  70. }
  71. return obj;
  72. }
  73. void WeakRef::set_obj(Object *p_object) {
  74. ref=p_object ? p_object->get_instance_ID() : 0;
  75. }
  76. void WeakRef::set_ref(const REF& p_ref) {
  77. ref=p_ref.is_valid() ? p_ref->get_instance_ID() : 0;
  78. }
  79. WeakRef::WeakRef() {
  80. ref=0;
  81. }
  82. void WeakRef::_bind_methods() {
  83. ObjectTypeDB::bind_method(_MD("get_ref:Object"),&WeakRef::get_ref);
  84. }
  85. #if 0
  86. Reference * RefBase::get_reference_from_ref(const RefBase &p_base) {
  87. return p_base.get_reference();
  88. }
  89. void RefBase::ref_inc(Reference *p_reference) {
  90. p_reference->refcount.ref();
  91. }
  92. bool RefBase::ref_dec(Reference *p_reference) {
  93. bool ref = p_reference->refcount.unref();
  94. return ref;
  95. }
  96. Reference *RefBase::first_ref(Reference *p_reference) {
  97. if (p_reference->refcount.ref()) {
  98. // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
  99. // at the same time, which is never likely to happen (would be crazy to do)
  100. // so don't do it.
  101. if (p_reference->refcount_init.get()>0) {
  102. p_reference->refcount_init.unref();
  103. p_reference->refcount.unref(); // first referencing is already 1, so compensate for the ref above
  104. }
  105. return p_reference;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. char * RefBase::get_refptr_data(const RefPtr &p_refptr) const {
  111. return p_refptr.data;
  112. }
  113. #endif