All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.nan_persistent_pre_12_inl.h Maven / Gradle / Ivy

Go to download

Native Abstractions for Node.js: C++ header for Node 0.8 -> 18 compatibility

There is a newer version: 2.19.0
Show newest version
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2018 NAN contributors
 *
 * MIT License 
 ********************************************************************/

#ifndef NAN_PERSISTENT_PRE_12_INL_H_
#define NAN_PERSISTENT_PRE_12_INL_H_

template
class PersistentBase {
  v8::Persistent persistent;
  template
  friend v8::Local New(const PersistentBase &p);
  template
  friend v8::Local New(const Persistent &p);
  template
  friend v8::Local New(const Global &p);
  template friend class ReturnValue;

 public:
  inline PersistentBase() :
      persistent() {}

  inline void Reset() {
    persistent.Dispose();
    persistent.Clear();
  }

  template
  inline void Reset(const v8::Local &other) {
    TYPE_CHECK(T, S);

    if (!persistent.IsEmpty()) {
      persistent.Dispose();
    }

    if (other.IsEmpty()) {
      persistent.Clear();
    } else {
      persistent = v8::Persistent::New(other);
    }
  }

  template
  inline void Reset(const PersistentBase &other) {
    TYPE_CHECK(T, S);

    if (!persistent.IsEmpty()) {
      persistent.Dispose();
    }

    if (other.IsEmpty()) {
      persistent.Clear();
    } else {
      persistent = v8::Persistent::New(other.persistent);
    }
  }

  inline bool IsEmpty() const { return persistent.IsEmpty(); }

  inline void Empty() { persistent.Clear(); }

  template
  inline bool operator==(const PersistentBase &that) const {
    return this->persistent == that.persistent;
  }

  template
  inline bool operator==(const v8::Local &that) const {
    return this->persistent == that;
  }

  template
  inline bool operator!=(const PersistentBase &that) const {
    return !operator==(that);
  }

  template
  inline bool operator!=(const v8::Local &that) const {
    return !operator==(that);
  }

  template
  inline void SetWeak(
    P *parameter
    , typename WeakCallbackInfo

::Callback callback , WeakCallbackType type); inline void ClearWeak() { persistent.ClearWeak(); } inline void MarkIndependent() { persistent.MarkIndependent(); } inline bool IsIndependent() const { return persistent.IsIndependent(); } inline bool IsNearDeath() const { return persistent.IsNearDeath(); } inline bool IsWeak() const { return persistent.IsWeak(); } private: inline explicit PersistentBase(v8::Persistent that) : persistent(that) { } inline explicit PersistentBase(T *val) : persistent(val) {} template friend class Persistent; template friend class Global; friend class ObjectWrap; }; template class NonCopyablePersistentTraits { public: typedef Persistent > NonCopyablePersistent; static const bool kResetInDestructor = false; template inline static void Copy(const Persistent &source, NonCopyablePersistent *dest) { Uncompilable(); } template inline static void Uncompilable() { TYPE_CHECK(O, v8::Primitive); } }; template struct CopyablePersistentTraits { typedef Persistent > CopyablePersistent; static const bool kResetInDestructor = true; template static inline void Copy(const Persistent &source, CopyablePersistent *dest) {} }; template class Persistent : public PersistentBase { public: inline Persistent() {} template inline Persistent(v8::Handle that) : PersistentBase(v8::Persistent::New(that)) { TYPE_CHECK(T, S); } inline Persistent(const Persistent &that) : PersistentBase() { Copy(that); } template inline Persistent(const Persistent &that) : PersistentBase() { Copy(that); } inline Persistent &operator=(const Persistent &that) { Copy(that); return *this; } template inline Persistent &operator=(const Persistent &that) { Copy(that); return *this; } inline ~Persistent() { if (M::kResetInDestructor) this->Reset(); } private: inline T *operator*() const { return *PersistentBase::persistent; } template inline void Copy(const Persistent &that) { TYPE_CHECK(T, S); this->Reset(); if (!that.IsEmpty()) { this->persistent = v8::Persistent::New(that.persistent); M::Copy(that, this); } } }; template class Global : public PersistentBase { struct RValue { inline explicit RValue(Global* obj) : object(obj) {} Global* object; }; public: inline Global() : PersistentBase(0) { } template inline Global(v8::Local that) // NOLINT(runtime/explicit) : PersistentBase(v8::Persistent::New(that)) { TYPE_CHECK(T, S); } template inline Global(const PersistentBase &that) // NOLINT(runtime/explicit) : PersistentBase(that) { TYPE_CHECK(T, S); } /** * Move constructor. */ inline Global(RValue rvalue) // NOLINT(runtime/explicit) : PersistentBase(rvalue.object->persistent) { rvalue.object->Reset(); } inline ~Global() { this->Reset(); } /** * Move via assignment. */ template inline Global &operator=(Global rhs) { TYPE_CHECK(T, S); this->Reset(rhs.persistent); rhs.Reset(); return *this; } /** * Cast operator for moves. */ inline operator RValue() { return RValue(this); } /** * Pass allows returning uniques from functions, etc. */ Global Pass() { return Global(RValue(this)); } private: Global(Global &); void operator=(Global &); template friend class ReturnValue; }; #endif // NAN_PERSISTENT_PRE_12_INL_H_