diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj
index a43e84be8..bd9f426ed 100644
--- a/Builds/VisualStudio2012/beast.vcxproj
+++ b/Builds/VisualStudio2012/beast.vcxproj
@@ -194,12 +194,13 @@
-
+
+
diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters
index ba2e38a65..e4c94c5e8 100644
--- a/Builds/VisualStudio2012/beast.vcxproj.filters
+++ b/Builds/VisualStudio2012/beast.vcxproj.filters
@@ -560,9 +560,6 @@
beast_core\time
-
- beast_core\memory
- beast_core\diagnostic
@@ -980,6 +977,12 @@
beast\mpl
+
+ beast_core\memory
+
+
+ beast_core\memory
+
diff --git a/modules/beast_asio/async/beast_SharedHandler.h b/modules/beast_asio/async/beast_SharedHandler.h
index f8a1be9d1..e6c573eb3 100644
--- a/modules/beast_asio/async/beast_SharedHandler.h
+++ b/modules/beast_asio/async/beast_SharedHandler.h
@@ -53,7 +53,7 @@ public:
// For asio::async_result<>
typedef void result_type;
- typedef SharedObjectPtr Ptr;
+ typedef SharedPtr Ptr;
virtual void operator() ();
virtual void operator() (error_code const&);
@@ -92,11 +92,11 @@ private:
//--------------------------------------------------------------------------
-// For SharedObjectPtr
+// For SharedPtr
template <>
struct ContainerDeletePolicy
{
- // SharedObjectPtr will use this when
+ // SharedPtr will use this when
// the reference count drops to zero.
//
inline static void destroy (SharedHandler* handler)
diff --git a/modules/beast_core/beast_core.h b/modules/beast_core/beast_core.h
index d15b155c9..2e4702d07 100644
--- a/modules/beast_core/beast_core.h
+++ b/modules/beast_core/beast_core.h
@@ -310,6 +310,8 @@ extern BEAST_API void BEAST_CALLTYPE logAssertion (char const* file, int line) n
#include "diagnostic/beast_ProtectedCall.h"
#include "containers/beast_AbstractFifo.h"
#include "containers/beast_ArrayAllocationBase.h"
+#include "memory/SharedObject.h"
+#include "memory/SharedPtr.h"
#include "containers/beast_DynamicObject.h"
#include "containers/beast_ElementComparator.h"
#include "maths/beast_Random.h"
@@ -350,7 +352,6 @@ extern BEAST_API void BEAST_CALLTYPE logAssertion (char const* file, int line) n
#include "memory/beast_Memory.h"
#include "memory/beast_MemoryBlock.h"
#include "memory/beast_OptionalScopedPointer.h"
-#include "memory/beast_SharedObject.h"
#include "memory/beast_ScopedPointer.h"
#include "memory/beast_SharedSingleton.h"
#include "memory/beast_WeakReference.h"
diff --git a/modules/beast_core/containers/beast_DynamicObject.h b/modules/beast_core/containers/beast_DynamicObject.h
index 87ba4c0b7..bd6e9f049 100644
--- a/modules/beast_core/containers/beast_DynamicObject.h
+++ b/modules/beast_core/containers/beast_DynamicObject.h
@@ -25,7 +25,6 @@
#define BEAST_DYNAMICOBJECT_H_INCLUDED
#include "beast_NamedValueSet.h"
-#include "../memory/beast_SharedObject.h"
//==============================================================================
@@ -50,7 +49,7 @@ public:
/** Destructor. */
virtual ~DynamicObject();
- typedef SharedObjectPtr Ptr;
+ typedef SharedPtr Ptr;
//==============================================================================
/** Returns true if the object has a property with this name.
diff --git a/modules/beast_core/containers/beast_SharedObjectArray.h b/modules/beast_core/containers/beast_SharedObjectArray.h
index e79bf53a0..5224a4f73 100644
--- a/modules/beast_core/containers/beast_SharedObjectArray.h
+++ b/modules/beast_core/containers/beast_SharedObjectArray.h
@@ -24,7 +24,6 @@
#ifndef BEAST_SHAREDOBJECTARRAY_H_INCLUDED
#define BEAST_SHAREDOBJECTARRAY_H_INCLUDED
-#include "../memory/beast_SharedObject.h"
#include "beast_ArrayAllocationBase.h"
#include "beast_ElementComparator.h"
#include "../threads/beast_CriticalSection.h"
@@ -47,7 +46,7 @@ template ObjectClassPtr;
+ typedef SharedPtr ObjectClassPtr;
//==============================================================================
/** Creates an empty array.
diff --git a/modules/beast_core/containers/beast_SharedTable.h b/modules/beast_core/containers/beast_SharedTable.h
index 890a1f0ca..11d517961 100644
--- a/modules/beast_core/containers/beast_SharedTable.h
+++ b/modules/beast_core/containers/beast_SharedTable.h
@@ -159,7 +159,7 @@ private:
class Data : public SharedObject
{
public:
- typedef SharedObjectPtr Ptr;
+ typedef SharedPtr Ptr;
explicit Data (int numEntries)
: m_numEntries (numEntries)
@@ -200,7 +200,7 @@ private:
{
}
- SharedObjectPtr m_data;
+ SharedPtr m_data;
};
template
diff --git a/modules/beast_core/maths/beast_Expression.cpp b/modules/beast_core/maths/beast_Expression.cpp
index ecd363020..e47082a30 100644
--- a/modules/beast_core/maths/beast_Expression.cpp
+++ b/modules/beast_core/maths/beast_Expression.cpp
@@ -30,20 +30,20 @@ public:
virtual Type getType() const noexcept = 0;
virtual Term* clone() const = 0;
- virtual SharedObjectPtr resolve (const Scope&, int recursionDepth) = 0;
+ virtual SharedPtr resolve (const Scope&, int recursionDepth) = 0;
virtual String toString() const = 0;
virtual double toDouble() const { return 0; }
virtual int getInputIndexFor (const Term*) const { return -1; }
virtual int getOperatorPrecedence() const { return 0; }
virtual int getNumInputs() const { return 0; }
virtual Term* getInput (int) const { return nullptr; }
- virtual SharedObjectPtr negated();
+ virtual SharedPtr negated();
- virtual SharedObjectPtr createTermToEvaluateInput (const Scope&, const Term* /*inputTerm*/,
+ virtual SharedPtr createTermToEvaluateInput (const Scope&, const Term* /*inputTerm*/,
double /*overallTarget*/, Term* /*topLevelTerm*/) const
{
bassertfalse;
- return SharedObjectPtr();
+ return SharedPtr();
}
virtual String getName() const
@@ -76,7 +76,7 @@ public:
//==============================================================================
struct Expression::Helpers
{
- typedef SharedObjectPtr TermPtr;
+ typedef SharedPtr TermPtr;
static void checkRecursionDepth (const int depth)
{
@@ -929,13 +929,13 @@ Expression& Expression::operator= (const Expression& other)
#if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
Expression::Expression (Expression&& other) noexcept
- : term (static_cast &&> (other.term))
+ : term (static_cast &&> (other.term))
{
}
Expression& Expression::operator= (Expression&& other) noexcept
{
- term = static_cast &&> (other.term);
+ term = static_cast &&> (other.term);
return *this;
}
#endif
@@ -1077,7 +1077,7 @@ int Expression::getNumInputs() const { return term->getNumInp
Expression Expression::getInput (int index) const { return Expression (term->getInput (index)); }
//==============================================================================
-SharedObjectPtr Expression::Term::negated()
+SharedPtr Expression::Term::negated()
{
return new Helpers::Negate (this);
}
diff --git a/modules/beast_core/maths/beast_Expression.h b/modules/beast_core/maths/beast_Expression.h
index 8f4b548bd..8c217a6c6 100644
--- a/modules/beast_core/maths/beast_Expression.h
+++ b/modules/beast_core/maths/beast_Expression.h
@@ -24,7 +24,6 @@
#ifndef BEAST_EXPRESSION_H_INCLUDED
#define BEAST_EXPRESSION_H_INCLUDED
-#include "../memory/beast_SharedObject.h"
#include "../containers/beast_Array.h"
#include "../memory/beast_ScopedPointer.h"
@@ -260,8 +259,8 @@ private:
friend class Term;
friend struct Helpers;
friend class ScopedPointer;
- friend class SharedObjectPtr;
- SharedObjectPtr term;
+ friend class SharedPtr;
+ SharedPtr term;
explicit Expression (Term*);
};
diff --git a/modules/beast_core/memory/SharedObject.h b/modules/beast_core/memory/SharedObject.h
new file mode 100644
index 000000000..68147654d
--- /dev/null
+++ b/modules/beast_core/memory/SharedObject.h
@@ -0,0 +1,192 @@
+//------------------------------------------------------------------------------
+/*
+ This file is part of Beast: https://github.com/vinniefalco/Beast
+ Copyright 2013, Vinnie Falco
+
+ Portions of this file are from JUCE.
+ Copyright (c) 2013 - Raw Material Software Ltd.
+ Please visit http://www.juce.com
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+//==============================================================================
+
+#ifndef BEAST_SHAREDOBJECT_H_INCLUDED
+#define BEAST_SHAREDOBJECT_H_INCLUDED
+
+//==============================================================================
+/**
+ Adds reference-counting to an object.
+
+ To add reference-counting to a class, derive it from this class, and
+ use the SharedPtr class to point to it.
+
+ e.g. @code
+ class MyClass : public SharedObject
+ {
+ void foo();
+
+ // This is a neat way of declaring a typedef for a pointer class,
+ // rather than typing out the full templated name each time..
+ typedef SharedPtr Ptr;
+ };
+
+ MyClass::Ptr p = new MyClass();
+ MyClass::Ptr p2 = p;
+ p = nullptr;
+ p2->foo();
+ @endcode
+
+ Once a new SharedObject has been assigned to a pointer, be
+ careful not to delete the object manually.
+
+ This class uses an Atomic value to hold the reference count, so that it
+ the pointers can be passed between threads safely. For a faster but non-thread-safe
+ version, use SingleThreadedSharedObject instead.
+
+ @see SharedPtr, SharedObjectArray, SingleThreadedSharedObject
+*/
+class BEAST_API SharedObject : public Uncopyable
+{
+public:
+ //==============================================================================
+ /** Increments the object's reference count.
+
+ This is done automatically by the smart pointer, but is public just
+ in case it's needed for nefarious purposes.
+ */
+ inline void incReferenceCount() noexcept
+ {
+ ++refCount;
+ }
+
+ /** Decreases the object's reference count.
+
+ If doDelete is true the object will be deleted when the reference
+ count drops to zero. The delete is performed using the regular
+ operator and does NOT go through the ContainerDeletePolicy.
+
+ The return value indicates if the reference count dropped to zero,
+ so callers who know the derived type can use the ContainerDeletePolicy.
+ */
+ inline bool decReferenceCount (bool doDelete = true) noexcept
+ {
+ bassert (getReferenceCount() > 0);
+ if (--refCount == 0)
+ {
+ if (doDelete)
+ delete this;
+ return true;
+ }
+ return false;
+ }
+
+ /** Returns the object's current reference count. */
+ inline int getReferenceCount() const noexcept { return refCount.get(); }
+
+protected:
+ //==============================================================================
+ /** Creates the reference-counted object (with an initial ref count of zero). */
+ SharedObject()
+ {
+ }
+
+ /** Destructor. */
+ virtual ~SharedObject()
+ {
+ // it's dangerous to delete an object that's still referenced by something else!
+ bassert (getReferenceCount() == 0);
+ }
+
+ /** Resets the reference count to zero without deleting the object.
+ You should probably never need to use this!
+ */
+ void resetReferenceCount() noexcept
+ {
+ refCount = 0;
+ }
+
+private:
+ //==============================================================================
+ Atomic refCount;
+};
+
+//==============================================================================
+/**
+ Adds reference-counting to an object.
+
+ This is effectively a version of the SharedObject class, but which
+ uses a non-atomic counter, and so is not thread-safe (but which will be more
+ efficient).
+ For more details on how to use it, see the SharedObject class notes.
+
+ @see SharedObject, SharedPtr, SharedObjectArray
+*/
+class BEAST_API SingleThreadedSharedObject : public Uncopyable
+{
+public:
+ //==============================================================================
+ /** Increments the object's reference count.
+
+ This is done automatically by the smart pointer, but is public just
+ in case it's needed for nefarious purposes.
+ */
+ inline void incReferenceCount() noexcept
+ {
+ ++refCount;
+ }
+
+ /** Decreases the object's reference count.
+
+ If doDelete is true the object will be deleted when the reference
+ count drops to zero. The delete is performed using the regular
+ operator and does NOT go through the ContainerDeletePolicy.
+
+ The return value indicates if the reference count dropped to zero,
+ so callers who know the derived type can use the ContainerDeletePolicy.
+ */
+ inline bool decReferenceCount (bool doDelete = true) noexcept
+ {
+ bassert (getReferenceCount() > 0);
+ if (--refCount == 0)
+ {
+ if (doDelete)
+ delete this;
+ else
+ return true;
+ }
+ return false;
+ }
+
+ /** Returns the object's current reference count. */
+ inline int getReferenceCount() const noexcept { return refCount; }
+
+
+protected:
+ //==============================================================================
+ /** Creates the reference-counted object (with an initial ref count of zero). */
+ SingleThreadedSharedObject() : refCount (0) {}
+
+ /** Destructor. */
+ virtual ~SingleThreadedSharedObject()
+ {
+ // it's dangerous to delete an object that's still referenced by something else!
+ bassert (getReferenceCount() == 0);
+ }
+
+private:
+ //==============================================================================
+ int refCount;
+};
+
+#endif
diff --git a/modules/beast_core/memory/SharedPtr.h b/modules/beast_core/memory/SharedPtr.h
new file mode 100644
index 000000000..ac12f89d6
--- /dev/null
+++ b/modules/beast_core/memory/SharedPtr.h
@@ -0,0 +1,309 @@
+//------------------------------------------------------------------------------
+/*
+ This file is part of Beast: https://github.com/vinniefalco/Beast
+ Copyright 2013, Vinnie Falco
+
+ Portions of this file are from JUCE.
+ Copyright (c) 2013 - Raw Material Software Ltd.
+ Please visit http://www.juce.com
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+//==============================================================================
+
+#ifndef BEAST_CORE_SHAREDPTR_H_INCLUDED
+#define BEAST_CORE_SHAREDPTR_H_INCLUDED
+
+// Visual Studio doesn't seem to do very well when it comes
+// to templated constructors and assignments so we provide
+// non-templated versions when U and T are the same type.
+//
+#ifndef BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS
+#define BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS 1
+#endif
+
+/** A smart-pointer container.
+
+ Requirement:
+ T must support the SharedObject concept.
+
+ The template parameter specifies the class of the object you want to point
+ to - the easiest way to make a class reference-countable is to simply make
+ it inherit from SharedObject, but if you need to, you could roll your own
+ reference-countable class by implementing a pair of methods called
+ incReferenceCount() and decReferenceCount().
+
+ When using this class, you'll probably want to create a typedef to
+ abbreviate the full templated name - e.g.
+
+ @code
+
+ typedef SharedPtr MyClassPtr;
+
+ @endcode
+
+ @see SharedObject, SharedObjectArray
+*/
+template
+class SharedPtr
+{
+public:
+ typedef T value_type;
+
+ /** The class being referenced by this container. */
+ typedef T ReferencedType;
+
+ /** Construct a container pointing to nothing. */
+ SharedPtr () noexcept
+ : m_p (nullptr)
+ {
+ }
+
+ /** Construct a container holding an object.
+ This will increment the object's reference-count if it is non-null.
+ Requirement:
+ U* must be convertible to T*
+ */
+ /// @{
+ SharedPtr (T* t) noexcept
+ : m_p (acquire (t))
+ {
+ }
+
+ template
+ SharedPtr (U* u) noexcept
+ : m_p (acquire (u))
+ {
+ }
+ /// @}
+
+ /** Construct a container holding an object from another container.
+ This will increment the object's reference-count (if it is non-null).
+ Requirement:
+ U* must be convertible to T*
+ */
+ /// @{
+#if BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS
+ SharedPtr (SharedPtr const& sp) noexcept
+ : m_p (acquire (sp.get ()))
+ {
+ }
+#endif
+
+ template
+ SharedPtr (SharedPtr const& sp) noexcept
+ : m_p (acquire (sp.get ()))
+ {
+ }
+ /// @}
+
+ /** Assign a different object to the container.
+ The previous object beind held, if any, loses a reference count and
+ will be destroyed if it is the last reference.
+ Requirement:
+ U* must be convertible to T*
+ */
+ /// @{
+#if BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS
+ SharedPtr& operator= (T* t)
+ {
+ return assign (t);
+ }
+#endif
+
+ template
+ SharedPtr& operator= (U* u)
+ {
+ return assign (u);
+ }
+ /// @}
+
+ /** Assign an object from another container to this one. */
+ /// @{
+ SharedPtr& operator= (SharedPtr const& sp)
+ {
+ return assign (sp.get ());
+ }
+
+ /** Assign an object from another container to this one. */
+ template
+ SharedPtr& operator= (SharedPtr const& sp)
+ {
+ return assign (sp.get ());
+ }
+ /// @}
+
+#if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
+ /** Construct a container with an object transferred from another container.
+ The originating container loses its reference to the object.
+ Requires:
+ U* must be convertible to T*
+ */
+ /// @{
+#if BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS
+ SharedPtr (SharedPtr && sp) noexcept
+ : m_p (sp.swap (nullptr))
+ {
+ }
+#endif
+
+ template
+ SharedPtr (SharedPtr && sp) noexcept
+ : m_p (sp.swap (nullptr))
+ {
+ }
+ /// @}
+
+ /** Transfer ownership of another object to this container.
+ The originating container loses its reference to the object.
+ Requires:
+ U* must be convertible to T*
+ */
+ /// @{
+#if BEAST_SHAREDPTR_PROVIDE_COMPILER_WORKAROUNDS
+ SharedPtr& operator= (SharedPtr && sp)
+ {
+ return assign (sp.swap (nullptr));
+ }
+#endif
+
+ template
+ SharedPtr& operator= (SharedPtr && sp)
+ {
+ return assign (sp.swap (nullptr));
+ }
+ /// @}
+#endif
+
+ /** Destroy the container and release the held reference, if any.
+ */
+ ~SharedPtr ()
+ {
+ if (m_p != nullptr)
+ if (m_p->decReferenceCount (false))
+ ContainerDeletePolicy ::destroy (m_p);
+ }
+
+ /** Returns `true` if the container is not pointing to an object. */
+ bool empty () const noexcept
+ {
+ return m_p == nullptr;
+ }
+
+ /** Returns the object that this pointer references if any, else nullptr. */
+ operator T* () const noexcept
+ {
+ return m_p;
+ }
+
+ /** Returns the object that this pointer references if any, else nullptr. */
+ T* operator-> () const noexcept
+ {
+ return m_p;
+ }
+
+ /** Returns the object that this pointer references if any, else nullptr. */
+ T* get () const noexcept
+ {
+ return m_p;
+ }
+
+ /** Returns the object that this pointer references if any, else nullptr.
+ DEPRECATED
+ */
+ T* getObject () const noexcept
+ {
+ return m_p;
+ }
+
+private:
+ // Acquire a reference to u for the caller.
+ //
+ template
+ static T* acquire (U* u) noexcept
+ {
+ if (u != nullptr)
+ u->incReferenceCount ();
+ return u;
+ }
+
+ // Swap ownership of the currently referenced object.
+ // The caller receives a pointer to the original object,
+ // and this container is left with the passed object. No
+ // reference counts are changed.
+ //
+ template
+ T* swap (U* u)
+ {
+ T* const t (m_p);
+ m_p = u;
+ return t;
+ }
+
+ // Acquire ownership of u.
+ // Any previous reference is released.
+ //
+ template
+ SharedPtr& assign (U* u)
+ {
+ if (m_p != u)
+ SharedPtr old (
+ this->swap (acquire (u)));
+ return *this;
+ }
+
+ T* m_p;
+};
+
+//------------------------------------------------------------------------------
+
+/// SharedPtr comparisons.
+/// @{
+template
+bool operator== (SharedPtr const& lhs, U* const rhs) noexcept
+{
+ return lhs.get() == rhs;
+}
+
+template
+bool operator== (SharedPtr const& lhs, SharedPtr const& rhs) noexcept
+{
+ return lhs.get() == rhs.get();
+}
+
+template
+bool operator== (T const* lhs, SharedPtr const& rhs) noexcept
+{
+ return lhs == rhs.get();
+}
+
+template
+bool operator!= (SharedPtr const& lhs, T const* rhs) noexcept
+{
+ return lhs.get() != rhs;
+}
+
+template
+bool operator!= (SharedPtr const& lhs, SharedPtr const& rhs) noexcept
+{
+ return lhs.get() != rhs.get();
+}
+
+template
+bool operator!= (T const* lhs, SharedPtr const& rhs) noexcept
+{
+ return lhs != rhs.get();
+}
+/// @}
+
+#endif
diff --git a/modules/beast_core/memory/beast_SharedObject.h b/modules/beast_core/memory/beast_SharedObject.h
deleted file mode 100644
index 65db29bab..000000000
--- a/modules/beast_core/memory/beast_SharedObject.h
+++ /dev/null
@@ -1,411 +0,0 @@
-//------------------------------------------------------------------------------
-/*
- This file is part of Beast: https://github.com/vinniefalco/Beast
- Copyright 2013, Vinnie Falco
-
- Portions of this file are from JUCE.
- Copyright (c) 2013 - Raw Material Software Ltd.
- Please visit http://www.juce.com
-
- Permission to use, copy, modify, and/or distribute this software for any
- purpose with or without fee is hereby granted, provided that the above
- copyright notice and this permission notice appear in all copies.
-
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-*/
-//==============================================================================
-
-#ifndef BEAST_SHAREDOBJECT_H_INCLUDED
-#define BEAST_SHAREDOBJECT_H_INCLUDED
-
-//==============================================================================
-/**
- Adds reference-counting to an object.
-
- To add reference-counting to a class, derive it from this class, and
- use the SharedObjectPtr class to point to it.
-
- e.g. @code
- class MyClass : public SharedObject
- {
- void foo();
-
- // This is a neat way of declaring a typedef for a pointer class,
- // rather than typing out the full templated name each time..
- typedef SharedObjectPtr Ptr;
- };
-
- MyClass::Ptr p = new MyClass();
- MyClass::Ptr p2 = p;
- p = nullptr;
- p2->foo();
- @endcode
-
- Once a new SharedObject has been assigned to a pointer, be
- careful not to delete the object manually.
-
- This class uses an Atomic value to hold the reference count, so that it
- the pointers can be passed between threads safely. For a faster but non-thread-safe
- version, use SingleThreadedSharedObject instead.
-
- @see SharedObjectPtr, SharedObjectArray, SingleThreadedSharedObject
-*/
-class BEAST_API SharedObject : public Uncopyable
-{
-public:
- //==============================================================================
- /** Increments the object's reference count.
-
- This is done automatically by the smart pointer, but is public just
- in case it's needed for nefarious purposes.
- */
- inline void incReferenceCount() noexcept
- {
- ++refCount;
- }
-
- /** Decreases the object's reference count.
-
- If doDelete is true the object will be deleted when the reference
- count drops to zero. The delete is performed using the regular
- operator and does NOT go through the ContainerDeletePolicy.
-
- The return value indicates if the reference count dropped to zero,
- so callers who know the derived type can use the ContainerDeletePolicy.
- */
- inline bool decReferenceCount (bool doDelete = true) noexcept
- {
- bassert (getReferenceCount() > 0);
- if (--refCount == 0)
- {
- if (doDelete)
- delete this;
- return true;
- }
- return false;
- }
-
- /** Returns the object's current reference count. */
- inline int getReferenceCount() const noexcept { return refCount.get(); }
-
-protected:
- //==============================================================================
- /** Creates the reference-counted object (with an initial ref count of zero). */
- SharedObject()
- {
- }
-
- /** Destructor. */
- virtual ~SharedObject()
- {
- // it's dangerous to delete an object that's still referenced by something else!
- bassert (getReferenceCount() == 0);
- }
-
- /** Resets the reference count to zero without deleting the object.
- You should probably never need to use this!
- */
- void resetReferenceCount() noexcept
- {
- refCount = 0;
- }
-
-private:
- //==============================================================================
- Atomic refCount;
-};
-
-//==============================================================================
-/**
- Adds reference-counting to an object.
-
- This is effectively a version of the SharedObject class, but which
- uses a non-atomic counter, and so is not thread-safe (but which will be more
- efficient).
- For more details on how to use it, see the SharedObject class notes.
-
- @see SharedObject, SharedObjectPtr, SharedObjectArray
-*/
-class BEAST_API SingleThreadedSharedObject : public Uncopyable
-{
-public:
- //==============================================================================
- /** Increments the object's reference count.
-
- This is done automatically by the smart pointer, but is public just
- in case it's needed for nefarious purposes.
- */
- inline void incReferenceCount() noexcept
- {
- ++refCount;
- }
-
- /** Decreases the object's reference count.
-
- If doDelete is true the object will be deleted when the reference
- count drops to zero. The delete is performed using the regular
- operator and does NOT go through the ContainerDeletePolicy.
-
- The return value indicates if the reference count dropped to zero,
- so callers who know the derived type can use the ContainerDeletePolicy.
- */
- inline bool decReferenceCount (bool doDelete = true) noexcept
- {
- bassert (getReferenceCount() > 0);
- if (--refCount == 0)
- {
- if (doDelete)
- delete this;
- return true;
- }
- return false;
- }
-
-
- /** Returns the object's current reference count. */
- inline int getReferenceCount() const noexcept { return refCount; }
-
-
-protected:
- //==============================================================================
- /** Creates the reference-counted object (with an initial ref count of zero). */
- SingleThreadedSharedObject() : refCount (0) {}
-
- /** Destructor. */
- virtual ~SingleThreadedSharedObject()
- {
- // it's dangerous to delete an object that's still referenced by something else!
- bassert (getReferenceCount() == 0);
- }
-
-private:
- //==============================================================================
- int refCount;
-};
-
-
-//==============================================================================
-/**
- A smart-pointer class which points to a reference-counted object.
-
- The template parameter specifies the class of the object you want to point to - the easiest
- way to make a class reference-countable is to simply make it inherit from SharedObject,
- but if you need to, you could roll your own reference-countable class by implementing a pair of
- mathods called incReferenceCount() and decReferenceCount().
-
- When using this class, you'll probably want to create a typedef to abbreviate the full
- templated name - e.g.
- @code typedef SharedObjectPtr MyClassPtr;@endcode
-
- @see SharedObject, SharedObjectArray
-*/
-template
-class SharedObjectPtr
-{
-public:
- /** The class being referenced by this pointer. */
- typedef Object ReferencedType;
-
- //==============================================================================
- /** Creates a pointer to a null object. */
- inline SharedObjectPtr() noexcept
- : referencedObject (nullptr)
- {
- }
-
- /** Creates a pointer to an object.
-
- This will increment the object's reference-count if it is non-null.
- */
- inline SharedObjectPtr (Object* const refCountedObject) noexcept
- : referencedObject (refCountedObject)
- {
- if (refCountedObject != nullptr)
- refCountedObject->incReferenceCount();
- }
-
- /** Copies another pointer.
- This will increment the object's reference-count (if it is non-null).
- */
- inline SharedObjectPtr (const SharedObjectPtr& other) noexcept
- : referencedObject (other.referencedObject)
- {
- if (referencedObject != nullptr)
- referencedObject->incReferenceCount();
- }
-
- #if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
- /** Takes-over the object from another pointer. */
- inline SharedObjectPtr (SharedObjectPtr&& other) noexcept
- : referencedObject (other.referencedObject)
- {
- other.referencedObject = nullptr;
- }
- #endif
-
- /** Copies another pointer.
- This will increment the object's reference-count (if it is non-null).
- */
- template
- inline SharedObjectPtr (const SharedObjectPtr& other) noexcept
- : referencedObject (static_cast