Turn audio into a shareable video. forked from nypublicradio/audiogram

node_object_wrap.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #ifndef SRC_NODE_OBJECT_WRAP_H_
  22. #define SRC_NODE_OBJECT_WRAP_H_
  23. #include "v8.h"
  24. #include <cassert>
  25. namespace node {
  26. class ObjectWrap {
  27. public:
  28. ObjectWrap() {
  29. refs_ = 0;
  30. }
  31. virtual ~ObjectWrap() {
  32. if (persistent().IsEmpty())
  33. return;
  34. persistent().ClearWeak();
  35. persistent().Reset();
  36. }
  37. template <class T>
  38. static inline T* Unwrap(v8::Local<v8::Object> handle) {
  39. assert(!handle.IsEmpty());
  40. assert(handle->InternalFieldCount() > 0);
  41. // Cast to ObjectWrap before casting to T. A direct cast from void
  42. // to T won't work right when T has more than one base class.
  43. void* ptr = handle->GetAlignedPointerFromInternalField(0);
  44. ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
  45. return static_cast<T*>(wrap);
  46. }
  47. inline v8::Local<v8::Object> handle() {
  48. return handle(v8::Isolate::GetCurrent());
  49. }
  50. inline v8::Local<v8::Object> handle(v8::Isolate* isolate) {
  51. return v8::Local<v8::Object>::New(isolate, persistent());
  52. }
  53. inline v8::Persistent<v8::Object>& persistent() {
  54. return handle_;
  55. }
  56. protected:
  57. inline void Wrap(v8::Local<v8::Object> handle) {
  58. assert(persistent().IsEmpty());
  59. assert(handle->InternalFieldCount() > 0);
  60. handle->SetAlignedPointerInInternalField(0, this);
  61. persistent().Reset(v8::Isolate::GetCurrent(), handle);
  62. MakeWeak();
  63. }
  64. inline void MakeWeak() {
  65. persistent().SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
  66. }
  67. /* Ref() marks the object as being attached to an event loop.
  68. * Refed objects will not be garbage collected, even if
  69. * all references are lost.
  70. */
  71. virtual void Ref() {
  72. assert(!persistent().IsEmpty());
  73. persistent().ClearWeak();
  74. refs_++;
  75. }
  76. /* Unref() marks an object as detached from the event loop. This is its
  77. * default state. When an object with a "weak" reference changes from
  78. * attached to detached state it will be freed. Be careful not to access
  79. * the object after making this call as it might be gone!
  80. * (A "weak reference" means an object that only has a
  81. * persistent handle.)
  82. *
  83. * DO NOT CALL THIS FROM DESTRUCTOR
  84. */
  85. virtual void Unref() {
  86. assert(!persistent().IsEmpty());
  87. assert(!persistent().IsWeak());
  88. assert(refs_ > 0);
  89. if (--refs_ == 0)
  90. MakeWeak();
  91. }
  92. int refs_; // ro
  93. private:
  94. static void WeakCallback(
  95. const v8::WeakCallbackInfo<ObjectWrap>& data) {
  96. ObjectWrap* wrap = data.GetParameter();
  97. assert(wrap->refs_ == 0);
  98. wrap->handle_.Reset();
  99. delete wrap;
  100. }
  101. v8::Persistent<v8::Object> handle_;
  102. };
  103. } // namespace node
  104. #endif // SRC_NODE_OBJECT_WRAP_H_