From f0cc7c4c8da18f2f3bd905ff3d1b10ebb8cd78d9 Mon Sep 17 00:00:00 2001 From: Joe Loser Date: Wed, 23 May 2018 21:32:59 -0400 Subject: [PATCH] Replace beast::SharedPtr with std::shared_ptr --- src/ripple/beast/core/SharedObject.h | 134 -------- src/ripple/beast/core/SharedPtr.h | 327 ------------------- src/ripple/peerfinder/impl/Logic.h | 31 +- src/ripple/peerfinder/impl/Source.h | 3 +- src/ripple/peerfinder/impl/SourceStrings.cpp | 4 +- src/ripple/peerfinder/impl/SourceStrings.h | 5 +- 6 files changed, 22 insertions(+), 482 deletions(-) delete mode 100644 src/ripple/beast/core/SharedObject.h delete mode 100644 src/ripple/beast/core/SharedPtr.h diff --git a/src/ripple/beast/core/SharedObject.h b/src/ripple/beast/core/SharedObject.h deleted file mode 100644 index fad833877..000000000 --- a/src/ripple/beast/core/SharedObject.h +++ /dev/null @@ -1,134 +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_SMART_PTR_SHAREDOBJECT_H_INCLUDED -#define BEAST_SMART_PTR_SHAREDOBJECT_H_INCLUDED - -#include -#include - -#include - -namespace beast { - -//============================================================================== -/** - 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 using Ptr = for a pointer class, - // rather than typing out the full templated name each time.. - using Ptr = SharedPtr; - }; - - 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 std::atomic value to hold the reference count, so - that the pointers can be passed between threads safely. - - @see SharedPtr, SharedObjectArray -*/ -class SharedObject -{ -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. - */ - void incReferenceCount() const noexcept - { - ++refCount; - } - - /** Decreases the object's reference count. */ - void decReferenceCount () const - { - assert (getReferenceCount() > 0); - if (--refCount == 0) - destroy (); - } - - /** Returns the object's current reference count. */ - int getReferenceCount() const noexcept - { - return refCount.load(); - } - -protected: - //============================================================================== - /** Creates the reference-counted object (with an initial ref count of zero). */ - SharedObject() - : refCount (0) - { - } - - SharedObject (SharedObject const&) = delete; - SharedObject& operator= (SharedObject const&) = delete; - - /** Destructor. */ - virtual ~SharedObject() - { - // it's dangerous to delete an object that's still referenced by something else! - assert (getReferenceCount() == 0); - } - - /** Destroy the object. - Derived classes can override this for different behaviors. - */ - virtual void destroy () const - { - delete this; - } - - /** Resets the reference count to zero without deleting the object. - You should probably never need to use this! - */ - void resetReferenceCount() noexcept - { - refCount.store (0); - } - -private: - //============================================================================== - std::atomic mutable refCount; -}; - -} - -#endif diff --git a/src/ripple/beast/core/SharedPtr.h b/src/ripple/beast/core/SharedPtr.h deleted file mode 100644 index 7d08829de..000000000 --- a/src/ripple/beast/core/SharedPtr.h +++ /dev/null @@ -1,327 +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_SMART_PTR_SHAREDPTR_H_INCLUDED -#define BEAST_SMART_PTR_SHAREDPTR_H_INCLUDED - -#include -#include - -namespace beast { - -// 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 using MyClassPtr = to - abbreviate the full templated name - e.g. - - @code - - using MyClassPtr = SharedPtr ; - - @endcode - - @see SharedObject, SharedObjectArray -*/ -template -class SharedPtr -{ -public: - using value_type = T; - - /** The class being referenced by this container. */ - using ReferencedType = T; - - /** 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.template 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.template swap (nullptr)); - } - /** @} */ -#endif - - /** Destroy the container and release the held reference, if any. - */ - ~SharedPtr () - { - release (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; - } - -private: - // Acquire a reference to u for the caller. - // - template - static T* acquire (U* u) noexcept - { - if (u != nullptr) - u->incReferenceCount (); - return u; - } - - static void release (T* t) - { - if (t != nullptr) - t->decReferenceCount (); - } - - // 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) - release (this->swap (acquire (u))); - return *this; - } - - T* m_p; -}; - -//------------------------------------------------------------------------------ - -// bind() helpers for pointer to member function - -template -const T* get_pointer (SharedPtr const& ptr) -{ - return ptr.get(); -} - -template -T* get_pointer (SharedPtr& ptr) -{ - return ptr.get(); -} - -//------------------------------------------------------------------------------ - -/** 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, 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(); -} -/** @} */ - -} - -#endif diff --git a/src/ripple/peerfinder/impl/Logic.h b/src/ripple/peerfinder/impl/Logic.h index ff929649d..260966702 100644 --- a/src/ripple/peerfinder/impl/Logic.h +++ b/src/ripple/peerfinder/impl/Logic.h @@ -20,24 +20,24 @@ #ifndef RIPPLE_PEERFINDER_LOGIC_H_INCLUDED #define RIPPLE_PEERFINDER_LOGIC_H_INCLUDED -#include #include +#include +#include +#include #include #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include +#include #include #include +#include #include namespace ripple { @@ -54,8 +54,8 @@ public: // Maps remote endpoints to slots. Since a slot has a // remote endpoint upon construction, this holds all counts. // - using Slots = std::map >; + using Slots = std::map>; beast::Journal m_journal; clock_type& m_clock; @@ -69,7 +69,7 @@ public: // The source we are currently fetching. // This is used to cancel I/O during program exit. - beast::SharedPtr fetchSource_; + std::shared_ptr fetchSource_; // Configuration settings Config config_; @@ -78,7 +78,7 @@ public: Counts counts_; // A list of slots that should always be connected - std::map fixed_; + std::map fixed_; // Live livecache from mtENDPOINTS messages Livecache <> livecache_; @@ -92,13 +92,13 @@ public: // The addresses (but not port) we are connected to. This includes // outgoing connection attempts. Note that this set can contain // duplicates (since the port is not set) - std::multiset connectedAddresses_; + std::multiset connectedAddresses_; // Set of public keys belonging to active peers - std::set keys_; + std::set keys_; // A list of dynamic sources to consult as a fallback - std::vector > m_sources; + std::vector> m_sources; clock_type::time_point m_whenBroadcast; @@ -971,13 +971,13 @@ public: //-------------------------------------------------------------------------- void - addStaticSource (beast::SharedPtr const& source) + addStaticSource (std::shared_ptr const& source) { fetch (source); } void - addSource (beast::SharedPtr const& source) + addSource (std::shared_ptr const& source) { m_sources.push_back (source); } @@ -1004,7 +1004,8 @@ public: } // Fetch bootcache addresses from the specified source. - void fetch (beast::SharedPtr const& source) + void + fetch (std::shared_ptr const& source) { Source::Results results; diff --git a/src/ripple/peerfinder/impl/Source.h b/src/ripple/peerfinder/impl/Source.h index 53c42062f..cf7c4ea20 100644 --- a/src/ripple/peerfinder/impl/Source.h +++ b/src/ripple/peerfinder/impl/Source.h @@ -21,7 +21,6 @@ #define RIPPLE_PEERFINDER_SOURCE_H_INCLUDED #include -#include #include namespace ripple { @@ -35,7 +34,7 @@ namespace PeerFinder { be updated automatically. Another solution is to use a custom DNS server that hands out peer IP addresses when name lookups are performed. */ -class Source : public beast::SharedObject +class Source { public: /** The results of a fetch. */ diff --git a/src/ripple/peerfinder/impl/SourceStrings.cpp b/src/ripple/peerfinder/impl/SourceStrings.cpp index dafce633f..ae86685f1 100644 --- a/src/ripple/peerfinder/impl/SourceStrings.cpp +++ b/src/ripple/peerfinder/impl/SourceStrings.cpp @@ -61,10 +61,10 @@ private: //------------------------------------------------------------------------------ -beast::SharedPtr +std::shared_ptr SourceStrings::New (std::string const& name, Strings const& strings) { - return new SourceStringsImp (name, strings); + return std::make_shared (name, strings); } } diff --git a/src/ripple/peerfinder/impl/SourceStrings.h b/src/ripple/peerfinder/impl/SourceStrings.h index 9ae9bc217..147d88e4e 100644 --- a/src/ripple/peerfinder/impl/SourceStrings.h +++ b/src/ripple/peerfinder/impl/SourceStrings.h @@ -21,7 +21,7 @@ #define RIPPLE_PEERFINDER_SOURCESTRINGS_H_INCLUDED #include -#include +#include namespace ripple { namespace PeerFinder { @@ -34,7 +34,8 @@ public: using Strings = std::vector ; - static beast::SharedPtr New (std::string const& name, Strings const& strings); + static std::shared_ptr + New (std::string const& name, Strings const& strings); }; }