| 1 |
// |
| 2 |
// Copyright (c) 2010-2017 Benjamin Kaufmann |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to |
| 6 |
// deal in the Software without restriction, including without limitation the |
| 7 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
// IN THE SOFTWARE. |
| 21 |
// |
| 22 |
// |
| 23 |
// NOTE: ProgramOptions is inspired by Boost.Program_options |
| 24 |
// see: www.boost.org/libs/program_options |
| 25 |
// |
| 26 |
#ifndef PROGRAM_OPTIONS_REFCOUNTABLE_H_INCLUDED |
| 27 |
#define PROGRAM_OPTIONS_REFCOUNTABLE_H_INCLUDED |
| 28 |
namespace Potassco { namespace ProgramOptions { namespace detail { |
| 29 |
|
| 30 |
class RefCountable { |
| 31 |
public: |
| 32 |
RefCountable() : refCount_(1) {} |
| 33 |
int addRef() { return ++refCount_; } |
| 34 |
int release() { return --refCount_; } |
| 35 |
int refCount() const { return refCount_; } |
| 36 |
private: |
| 37 |
int refCount_; |
| 38 |
}; |
| 39 |
|
| 40 |
template <class T> |
| 41 |
class IntrusiveSharedPtr { |
| 42 |
public: |
| 43 |
typedef T element_type; |
| 44 |
explicit IntrusiveSharedPtr(T* p = 0) throw() |
| 45 |
: ptr_(p) { /* NO add ref */ |
| 46 |
} |
| 47 |
IntrusiveSharedPtr(const IntrusiveSharedPtr& o) throw() |
| 48 |
: ptr_(o.ptr_) { |
| 49 |
addRef(); |
| 50 |
} |
| 51 |
~IntrusiveSharedPtr() throw () { release(); } |
| 52 |
IntrusiveSharedPtr& operator=(const IntrusiveSharedPtr& other) { |
| 53 |
other.addRef(); |
| 54 |
this->release(); |
| 55 |
this->ptr_ = other.ptr_; |
| 56 |
return *this; |
| 57 |
} |
| 58 |
T& operator*() const throw() { return *ptr_; } |
| 59 |
T* operator->() const throw() { return ptr_; } |
| 60 |
T* get() const throw() { return ptr_; } |
| 61 |
void reset() throw() { release(); ptr_ = 0; } |
| 62 |
bool unique() const throw() { return !ptr_ || ptr_->refCount() == 1; } |
| 63 |
int count() const throw() { return ptr_ ? ptr_->refCount() : 0; } |
| 64 |
void swap(IntrusiveSharedPtr& b) { |
| 65 |
T* temp = ptr_; |
| 66 |
ptr_ = b.ptr_; |
| 67 |
b.ptr_ = temp; |
| 68 |
} |
| 69 |
private: |
| 70 |
T* ptr_; |
| 71 |
void addRef() const { if (ptr_) ptr_->addRef(); } |
| 72 |
void release() const { |
| 73 |
if (ptr_ && ptr_->release() == 0) { |
| 74 |
delete ptr_; |
| 75 |
} |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
} // namespace detail |
| 80 |
} // namespace ProgramOptions |
| 81 |
} // namespace Potassco |
| 82 |
#endif |