nan_persistent_pre_12_inl.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * NAN - Native Abstractions for Node.js
00003  *
00004  * Copyright (c) 2016 NAN contributors
00005  *
00006  * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
00007  ********************************************************************/
00008 
00009 #ifndef NAN_PERSISTENT_PRE_12_INL_H_
00010 #define NAN_PERSISTENT_PRE_12_INL_H_
00011 
00012 template<typename T>
00013 class PersistentBase {
00014   v8::Persistent<T> persistent;
00015   template<typename U>
00016   friend v8::Local<U> New(const PersistentBase<U> &p);
00017   template<typename U, typename M>
00018   friend v8::Local<U> New(const Persistent<U, M> &p);
00019   template<typename U>
00020   friend v8::Local<U> New(const Global<U> &p);
00021   template<typename S> friend class ReturnValue;
00022 
00023  public:
00024   inline PersistentBase() :
00025       persistent() {}
00026 
00027   inline void Reset() {
00028     persistent.Dispose();
00029     persistent.Clear();
00030   }
00031 
00032   template<typename S>
00033   inline void Reset(const v8::Local<S> &other) {
00034     TYPE_CHECK(T, S);
00035 
00036     if (!persistent.IsEmpty()) {
00037       persistent.Dispose();
00038     }
00039 
00040     if (other.IsEmpty()) {
00041       persistent.Clear();
00042     } else {
00043       persistent = v8::Persistent<T>::New(other);
00044     }
00045   }
00046 
00047   template<typename S>
00048   inline void Reset(const PersistentBase<S> &other) {
00049     TYPE_CHECK(T, S);
00050 
00051     if (!persistent.IsEmpty()) {
00052       persistent.Dispose();
00053     }
00054 
00055     if (other.IsEmpty()) {
00056       persistent.Clear();
00057     } else {
00058       persistent = v8::Persistent<T>::New(other.persistent);
00059     }
00060   }
00061 
00062   inline bool IsEmpty() const { return persistent.IsEmpty(); }
00063 
00064   inline void Empty() { persistent.Clear(); }
00065 
00066   template<typename S>
00067   inline bool operator==(const PersistentBase<S> &that) const {
00068     return this->persistent == that.persistent;
00069   }
00070 
00071   template<typename S>
00072   inline bool operator==(const v8::Local<S> &that) const {
00073     return this->persistent == that;
00074   }
00075 
00076   template<typename S>
00077   inline bool operator!=(const PersistentBase<S> &that) const {
00078     return !operator==(that);
00079   }
00080 
00081   template<typename S>
00082   inline bool operator!=(const v8::Local<S> &that) const {
00083     return !operator==(that);
00084   }
00085 
00086   template<typename P>
00087   inline void SetWeak(
00088     P *parameter
00089     , typename WeakCallbackInfo<P>::Callback callback
00090     , WeakCallbackType type);
00091 
00092   inline void ClearWeak() { persistent.ClearWeak(); }
00093 
00094   inline void MarkIndependent() { persistent.MarkIndependent(); }
00095 
00096   inline bool IsIndependent() const { return persistent.IsIndependent(); }
00097 
00098   inline bool IsNearDeath() const { return persistent.IsNearDeath(); }
00099 
00100   inline bool IsWeak() const { return persistent.IsWeak(); }
00101 
00102  private:
00103   inline explicit PersistentBase(v8::Persistent<T> that) :
00104       persistent(that) { }
00105   inline explicit PersistentBase(T *val) : persistent(val) {}
00106   template<typename S, typename M> friend class Persistent;
00107   template<typename S> friend class Global;
00108   friend class ObjectWrap;
00109 };
00110 
00111 template<typename T>
00112 class NonCopyablePersistentTraits {
00113  public:
00114   typedef Persistent<T, NonCopyablePersistentTraits<T> >
00115       NonCopyablePersistent;
00116   static const bool kResetInDestructor = false;
00117   template<typename S, typename M>
00118   inline static void Copy(const Persistent<S, M> &source,
00119                              NonCopyablePersistent *dest) {
00120     Uncompilable<v8::Object>();
00121   }
00122 
00123   template<typename O> inline static void Uncompilable() {
00124     TYPE_CHECK(O, v8::Primitive);
00125   }
00126 };
00127 
00128 template<typename T>
00129 struct CopyablePersistentTraits {
00130   typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
00131   static const bool kResetInDestructor = true;
00132   template<typename S, typename M>
00133   static inline void Copy(const Persistent<S, M> &source,
00134                              CopyablePersistent *dest) {}
00135 };
00136 
00137 template<typename T, typename M> class Persistent :
00138     public PersistentBase<T> {
00139  public:
00140   inline Persistent() {}
00141 
00142   template<typename S> inline Persistent(v8::Handle<S> that)
00143       : PersistentBase<T>(v8::Persistent<T>::New(that)) {
00144     TYPE_CHECK(T, S);
00145   }
00146 
00147   inline Persistent(const Persistent &that) : PersistentBase<T>() {
00148     Copy(that);
00149   }
00150 
00151   template<typename S, typename M2>
00152   inline Persistent(const Persistent<S, M2> &that) :
00153       PersistentBase<T>() {
00154     Copy(that);
00155   }
00156 
00157   inline Persistent &operator=(const Persistent &that) {
00158     Copy(that);
00159     return *this;
00160   }
00161 
00162   template <class S, class M2>
00163   inline Persistent &operator=(const Persistent<S, M2> &that) {
00164     Copy(that);
00165     return *this;
00166   }
00167 
00168   inline ~Persistent() {
00169     if (M::kResetInDestructor) this->Reset();
00170   }
00171 
00172  private:
00173   inline T *operator*() const { return *PersistentBase<T>::persistent; }
00174 
00175   template<typename S, typename M2>
00176   inline void Copy(const Persistent<S, M2> &that) {
00177     TYPE_CHECK(T, S);
00178 
00179     this->Reset();
00180 
00181     if (!that.IsEmpty()) {
00182       this->persistent = v8::Persistent<T>::New(that.persistent);
00183       M::Copy(that, this);
00184     }
00185   }
00186 };
00187 
00188 template<typename T>
00189 class Global : public PersistentBase<T> {
00190   struct RValue {
00191     inline explicit RValue(Global* obj) : object(obj) {}
00192     Global* object;
00193   };
00194 
00195  public:
00196   inline Global() : PersistentBase<T>(0) { }
00197 
00198   template <typename S>
00199   inline Global(v8::Local<S> that)
00200       : PersistentBase<T>(v8::Persistent<T>::New(that)) {
00201     TYPE_CHECK(T, S);
00202   }
00203 
00204   template <typename S>
00205   inline Global(const PersistentBase<S> &that)
00206     : PersistentBase<T>(that) {
00207     TYPE_CHECK(T, S);
00208   }
00212   inline Global(RValue rvalue)
00213     : PersistentBase<T>(rvalue.object->persistent) {
00214     rvalue.object->Reset();
00215   }
00216   inline ~Global() { this->Reset(); }
00220   template<typename S>
00221   inline Global &operator=(Global<S> rhs) {
00222     TYPE_CHECK(T, S);
00223     this->Reset(rhs.persistent);
00224     rhs.Reset();
00225     return *this;
00226   }
00230   inline operator RValue() { return RValue(this); }
00234   Global Pass() { return Global(RValue(this)); }
00235 
00236  private:
00237   Global(Global &);
00238   void operator=(Global &);
00239   template<typename S> friend class ReturnValue;
00240 };
00241 
00242 #endif  // NAN_PERSISTENT_PRE_12_INL_H_


dji_ronin
Author(s):
autogenerated on Sat Jun 8 2019 20:15:31