| 1 | |
| 2 | // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, |
| 6 | // are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions, and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions, and the following disclaimer in the documentation |
| 13 | // and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 | // may be used to endorse or promote products derived from this software without |
| 17 | // specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 | // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 | // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 | // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 | // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 | // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 | // |
| 32 | |
| 33 | #ifndef XSENS_NONINTRUSIVE_SHARED_POINTER_H |
| 34 | #define XSENS_NONINTRUSIVE_SHARED_POINTER_H |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <algorithm> // std::swap |
| 38 | |
| 39 | namespace xsens |
| 40 | { |
| 41 | |
| 42 | /*! \brief non-intrusive shared pointer class similair to boost::shared_ptr |
| 43 | |
| 44 | The NonIntrusiveSharedPointer class template stores a pointer to a dynamically allocated object, typically with a C++ new-expression. |
| 45 | The object pointed to is guaranteed to be deleted when the last NonIntrusiveSharedPointer pointing to it is destroyed or reset. |
| 46 | Every NonIntrusiveSharedPointer meets the CopyConstructible and Assignable requirements of the C++ Standard Library, and so can be used in standard library containers. |
| 47 | */ |
| 48 | template <typename T> |
| 49 | class NonIntrusiveSharedPointer |
| 50 | { |
| 51 | public: |
| 52 | typedef T ElementType; //!< Provides the type of the template parameter T. |
| 53 | |
| 54 | /*! \brief NonIntrusiveSharedPointer Constructor. |
| 55 | Constructs a NonIntrusiveSharedPointer which takes ownership of the pointer p. |
| 56 | The pointer p has to be allocated by a C++ new-expression or may be null. |
| 57 | \param[in] p The pointer the NonIntrusiveSharedPointer takes ownership of |
| 58 | */ |
| 59 | explicit NonIntrusiveSharedPointer(ElementType* p = 0) : m_data(0) { |
| 60 | if (p != 0) { |
| 61 | m_data = new Data(p); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /*! \brief NonIntrusiveSharedPointer Destructor. |
| 66 | Destroys a NonIntrusiveSharedPointer instance. If this was the last reference to the owned |
| 67 | pointer, the pointer will be deleted. |
| 68 | */ |
| 69 | ~NonIntrusiveSharedPointer() throw() { |
| 70 | if (m_data != 0) { |
| 71 | if (--m_data->m_count == 0) { |
| 72 | delete m_data->m_pointer; |
| 73 | delete m_data; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /*! \brief NonIntrusiveSharedPointer Copy constructor. |
| 79 | Constructs a NonIntrusiveSharedPointer that shares ownership with the other NonIntrusiveSharedPointer |
| 80 | (may be empty if other is empty (null) |
| 81 | \param[in] other The shared pointer to copy from (share ownership of container pointer) |
| 82 | */ |
| 83 | NonIntrusiveSharedPointer(NonIntrusiveSharedPointer const & other) : m_data(other.m_data) { |
| 84 | if (m_data != 0) { |
| 85 | ++m_data->m_count; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /*! \brief NonIntrusiveSharedPointer assignment operator |
| 90 | Equivalent to NonIntrusiveSharedPointer(other).swap(*this) |
| 91 | */ |
| 92 | NonIntrusiveSharedPointer& operator = (NonIntrusiveSharedPointer other) { // intentional pass-by-value serves as a temporary copy |
| 93 | other.swap(*this); |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | /*! |
| 98 | Exchanges the contents of the two NonIntrusiveSharedPointers. |
| 99 | \param[in] other The NonIntrusiveSharedPointer to swap contents with. |
| 100 | */ |
| 101 | inline void swap(NonIntrusiveSharedPointer<T>& other) throw() { |
| 102 | std::swap(m_data, other.m_data); |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | \pre The stored pointer may not be null and the NonIntrusiveSharedPointer may not be empty. |
| 107 | \returns a reference to the object pointed to by the stored pointer. |
| 108 | */ |
| 109 | inline ElementType& operator* () const throw() { |
| 110 | assert(m_data != 0 && m_data->m_pointer != 0); |
| 111 | return *m_data->m_pointer; |
| 112 | } |
| 113 | |
| 114 | /*! |
| 115 | \pre The stored pointer may not be null and the NonIntrusiveSharedPointer may not be empty. |
| 116 | \returns the stored pointer |
| 117 | */ |
| 118 | inline ElementType* operator -> () const throw() { |
| 119 | assert(m_data != 0 && m_data->m_pointer != 0); |
| 120 | return m_data->m_pointer; |
| 121 | } |
| 122 | |
| 123 | /*! |
| 124 | \returns the stored pointer or 0 if empty |
| 125 | */ |
| 126 | inline ElementType* get() const throw() { |
| 127 | return (m_data != 0) ? m_data->m_pointer : 0; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /*! \brief Equivalent of NonIntrusiveSharedPointer<T>(p).swap(*this) |
| 132 | */ |
| 133 | void reset(ElementType* p = 0) throw() { |
| 134 | if (m_data == 0 || m_data->m_pointer != p) { |
| 135 | NonIntrusiveSharedPointer<T>(p).swap(*this); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /*! \brief Returns true if the shared pointer points to a NULL object */ |
| 140 | inline bool isNull() const { |
| 141 | return get() == 0; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | /*! |
| 146 | A struct that holds the reference count and the actual stored (shared) pointer. |
| 147 | An instance of this struct is shared (by means of the m_data member) by NonIntrusiveSharedPointers |
| 148 | that shared the same data. |
| 149 | */ |
| 150 | struct Data { |
| 151 | Data(ElementType* p) : m_pointer(p), m_count(1) {} |
| 152 | ElementType* m_pointer; |
| 153 | size_t m_count; |
| 154 | }; |
| 155 | |
| 156 | Data* m_data; //!< A pointer to the shared data. |
| 157 | }; |
| 158 | |
| 159 | /*! |
| 160 | Equivalent to p1.swap(p2) |
| 161 | Matches the interface of std::swap. Provided as an aid to generic programming. |
| 162 | */ |
| 163 | template <typename T> |
| 164 | void swap(NonIntrusiveSharedPointer<T>& p1, NonIntrusiveSharedPointer<T>& p2) throw() { |
| 165 | p1.swap(p2); |
| 166 | } |
| 167 | |
| 168 | } // namespace xsens |
| 169 | |
| 170 | namespace std |
| 171 | { |
| 172 | /*! |
| 173 | Equivalent to p1.swap(p2) |
| 174 | Matches the interface of std::swap. Provided as an aid to generic programming. |
| 175 | */ |
| 176 | template <typename T> |
| 177 | void swap(xsens::NonIntrusiveSharedPointer<T>& p1, xsens::NonIntrusiveSharedPointer<T>& p2) throw() |
| 178 | { |
| 179 | p1.swap(p2); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #endif |