?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  3. // Copyright 2004-11 by Raw Material Software Ltd.
  4. // SPDX-License-Identifier: MIT
  5.  
  6. //==============================================================================
  7. /*
  8.   This is a derivative work used by permission from part of
  9.   JUCE, available at http://www.rawaterialsoftware.com
  10.  
  11.   License: The MIT License (http://www.opensource.org/licenses/mit-license.php)
  12.  
  13.   Permission is hereby granted, free of charge, to any person obtaining a copy
  14.   of this software and associated documentation files (the "Software"), to deal
  15.   in the Software without restriction, including without limitation the rights
  16.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17.   copies of the Software, and to permit persons to whom the Software is
  18.   furnished to do so, subject to the following conditions:
  19.  
  20.   The above copyright notice and this permission notice shall be included in all
  21.   copies or substantial portions of the Software.
  22.  
  23.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29.   SOFTWARE.
  30.  
  31.   This file incorporates work covered by the following copyright and
  32.   permission notice:
  33.  
  34.     This file is part of the JUCE library - "Jules' Utility Class Extensions"
  35.     Copyright 2004-11 by Raw Material Software Ltd.
  36. */
  37. //==============================================================================
  38.  
  39. #pragma once
  40.  
  41. #include <cassert>
  42. #include <utility>
  43.  
  44. namespace luabridge {
  45.  
  46. //==============================================================================
  47. /**
  48.   Adds reference-counting to an object.
  49.  
  50.   To add reference-counting to a class, derive it from this class, and
  51.   use the RefCountedObjectPtr class to point to it.
  52.  
  53.   e.g. @code
  54.   class MyClass : public RefCountedObjectType
  55.   {
  56.       void foo();
  57.  
  58.       // This is a neat way of declaring a typedef for a pointer class,
  59.       // rather than typing out the full templated name each time..
  60.       typedef RefCountedObjectPtr<MyClass> Ptr;
  61.   };
  62.  
  63.   MyClass::Ptr p = new MyClass();
  64.   MyClass::Ptr p2 = p;
  65.   p = 0;
  66.   p2->foo();
  67.   @endcode
  68.  
  69.   Once a new RefCountedObjectType has been assigned to a pointer, be
  70.   careful not to delete the object manually.
  71. */
  72. template<class CounterType>
  73. class RefCountedObjectType
  74. {
  75. public:
  76.     //==============================================================================
  77.     /** Increments the object's reference count.
  78.  
  79.         This is done automatically by the smart pointer, but is public just
  80.         in case it's needed for nefarious purposes.
  81.     */
  82.     void incReferenceCount() const { ++refCount; }
  83.  
  84.     /** Decreases the object's reference count.
  85.  
  86.         If the count gets to zero, the object will be deleted.
  87.     */
  88.     void decReferenceCount() const
  89.     {
  90.         assert(getReferenceCount() > 0);
  91.  
  92.         if (--refCount == 0)
  93.             delete this;
  94.     }
  95.  
  96.     /** Returns the object's current reference count.
  97.      * @returns The reference count.
  98.      */
  99.     int getReferenceCount() const { return static_cast<int>(refCount); }
  100.  
  101. protected:
  102.     //==============================================================================
  103.     /** Creates the reference-counted object (with an initial ref count of zero). */
  104.     RefCountedObjectType() : refCount() {}
  105.  
  106.     /** Destructor. */
  107.     virtual ~RefCountedObjectType()
  108.     {
  109.         // it's dangerous to delete an object that's still referenced by something else!
  110.         assert(getReferenceCount() == 0);
  111.     }
  112.  
  113. private:
  114.     //==============================================================================
  115.     CounterType mutable refCount;
  116. };
  117.  
  118. //==============================================================================
  119.  
  120. /** Non thread-safe reference counted object.
  121.  
  122.     This creates a RefCountedObjectType that uses a non-atomic integer
  123.     as the counter.
  124. */
  125. typedef RefCountedObjectType<int> RefCountedObject;
  126.  
  127. //==============================================================================
  128. /**
  129.     A smart-pointer class which points to a reference-counted object.
  130.  
  131.     The template parameter specifies the class of the object you want to point
  132.     to - the easiest way to make a class reference-countable is to simply make
  133.     it inherit from RefCountedObjectType, but if you need to, you could roll
  134.     your own reference-countable class by implementing a pair of methods called
  135.     incReferenceCount() and decReferenceCount().
  136.  
  137.     When using this class, you'll probably want to create a typedef to
  138.     abbreviate the full templated name - e.g.
  139.  
  140.     @code
  141.  
  142.     typedef RefCountedObjectPtr <MyClass> MyClassPtr;
  143.  
  144.     @endcode
  145. */
  146. template<class ReferenceCountedObjectClass>
  147. class RefCountedObjectPtr
  148. {
  149. public:
  150.     /** The class being referenced by this pointer. */
  151.     typedef ReferenceCountedObjectClass ReferencedType;
  152.  
  153.     //==============================================================================
  154.     /** Creates a pointer to a null object. */
  155.     RefCountedObjectPtr() : referencedObject(nullptr) {}
  156.  
  157.     /** Creates a pointer to an object.
  158.         This will increment the object's reference-count if it is non-null.
  159.  
  160.         @param refCountedObject A reference counted object to own.
  161.     */
  162.     RefCountedObjectPtr(ReferenceCountedObjectClass* const refCountedObject)
  163.         : referencedObject(refCountedObject)
  164.     {
  165.         if (refCountedObject != nullptr)
  166.             refCountedObject->incReferenceCount();
  167.     }
  168.  
  169.     /** Copies another pointer.
  170.         This will increment the object's reference-count (if it is non-null).
  171.  
  172.         @param other Another pointer.
  173.     */
  174.     RefCountedObjectPtr(const RefCountedObjectPtr& other) : referencedObject(other.referencedObject)
  175.     {
  176.         if (referencedObject != nullptr)
  177.             referencedObject->incReferenceCount();
  178.     }
  179.  
  180.     /**
  181.       Takes-over the object from another pointer.
  182.  
  183.       @param other Another pointer.
  184.     */
  185.     RefCountedObjectPtr(RefCountedObjectPtr&& other) : referencedObject(other.referencedObject)
  186.     {
  187.         other.referencedObject = nullptr;
  188.     }
  189.  
  190.     /** Copies another pointer.
  191.         This will increment the object's reference-count (if it is non-null).
  192.  
  193.         @param other Another pointer.
  194.     */
  195.     template<class DerivedClass>
  196.     RefCountedObjectPtr(const RefCountedObjectPtr<DerivedClass>& other)
  197.         : referencedObject(static_cast<ReferenceCountedObjectClass*>(other.getObject()))
  198.     {
  199.         if (referencedObject != nullptr)
  200.             referencedObject->incReferenceCount();
  201.     }
  202.  
  203.     /** Changes this pointer to point at a different object.
  204.  
  205.         The reference count of the old object is decremented, and it might be
  206.         deleted if it hits zero. The new object's count is incremented.
  207.  
  208.         @param other A pointer to assign from.
  209.         @returns This pointer.
  210.     */
  211.     RefCountedObjectPtr& operator=(const RefCountedObjectPtr& other)
  212.     {
  213.         return operator=(other.referencedObject);
  214.     }
  215.  
  216.     /** Changes this pointer to point at a different object.
  217.         The reference count of the old object is decremented, and it might be
  218.         deleted if it hits zero. The new object's count is incremented.
  219.  
  220.         @param other A pointer to assign from.
  221.         @returns This pointer.
  222.     */
  223.     template<class DerivedClass>
  224.     RefCountedObjectPtr& operator=(const RefCountedObjectPtr<DerivedClass>& other)
  225.     {
  226.         return operator=(static_cast<ReferenceCountedObjectClass*>(other.getObject()));
  227.     }
  228.  
  229.     /**
  230.       Takes-over the object from another pointer.
  231.  
  232.       @param other A pointer to assign from.
  233.       @returns This pointer.
  234.      */
  235.     RefCountedObjectPtr& operator=(RefCountedObjectPtr&& other)
  236.     {
  237.         std::swap(referencedObject, other.referencedObject);
  238.         return *this;
  239.     }
  240.  
  241.     /** Changes this pointer to point at a different object.
  242.         The reference count of the old object is decremented, and it might be
  243.         deleted if it hits zero. The new object's count is incremented.
  244.  
  245.         @param newObject A reference counted object to own.
  246.         @returns This pointer.
  247.     */
  248.     RefCountedObjectPtr& operator=(ReferenceCountedObjectClass* const newObject)
  249.     {
  250.         if (referencedObject != newObject)
  251.         {
  252.             if (newObject != nullptr)
  253.                 newObject->incReferenceCount();
  254.  
  255.             ReferenceCountedObjectClass* const oldObject = referencedObject;
  256.             referencedObject = newObject;
  257.  
  258.             if (oldObject != nullptr)
  259.                 oldObject->decReferenceCount();
  260.         }
  261.  
  262.         return *this;
  263.     }
  264.  
  265.     /** Destructor.
  266.         This will decrement the object's reference-count, and may delete it if it
  267.         gets to zero.
  268.     */
  269.     ~RefCountedObjectPtr()
  270.     {
  271.         if (referencedObject != nullptr)
  272.             referencedObject->decReferenceCount();
  273.     }
  274.  
  275.     /** Returns the object that this pointer references.
  276.         The returned pointer may be null.
  277.  
  278.         @returns The pointee.
  279.     */
  280.     operator ReferenceCountedObjectClass*() const { return referencedObject; }
  281.  
  282.     /** Returns the object that this pointer references.
  283.         The returned pointer may be null.
  284.  
  285.         @returns The pointee.
  286.     */
  287.     ReferenceCountedObjectClass* operator->() const { return referencedObject; }
  288.  
  289.     /** Returns the object that this pointer references.
  290.         The returned pointer may be null.
  291.  
  292.         @returns The pointee.
  293.     */
  294.     ReferenceCountedObjectClass* getObject() const { return referencedObject; }
  295.  
  296. private:
  297.     //==============================================================================
  298.     ReferenceCountedObjectClass* referencedObject;
  299. };
  300.  
  301. //==============================================================================
  302.  
  303. // forward declaration
  304. template<class T>
  305. struct ContainerTraits;
  306.  
  307. template<class T>
  308. struct ContainerTraits<RefCountedObjectPtr<T>>
  309. {
  310.     typedef T Type;
  311.  
  312.     static T* get(RefCountedObjectPtr<T> const& c) { return c.getObject(); }
  313. };
  314.  
  315. //==============================================================================
  316.  
  317. } // namespace luabridge
  318.