| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VCOWPTR_H |
| 20 | #define VCOWPTR_H |
| 21 |
| 22 | #include <assert.h> |
| 23 | #include "vglobal.h" |
| 24 |
| 25 | template <typename T> |
| 26 | class vcow_ptr { |
| 27 | struct model { |
| 28 | std::atomic<std::size_t> mRef{1}; |
| 29 |
| 30 | model() = default; |
| 31 |
| 32 | template <class... Args> |
| 33 | explicit model(Args&&... args) : mValue(std::forward<Args>(args)...) |
| 34 | { |
| 35 | } |
| 36 |
| 37 | T mValue; |
| 38 | }; |
| 39 | model* mModel; |
| 40 |
| 41 | public: |
| 42 | using element_type = T; |
| 43 |
| 44 | vcow_ptr() |
| 45 | { |
| 46 | static model default_s; |
| 47 | mModel = &default_s; |
| 48 | ++mModel->mRef; |
| 49 | } |
| 50 |
| 51 | ~vcow_ptr() |
| 52 | { |
| 53 | if (mModel && (--mModel->mRef == 0)) delete mModel; |
| 54 | } |
| 55 |
| 56 | template <class... Args> |
| 57 | vcow_ptr(Args&&... args) : mModel(new model(std::forward<Args>(args)...)) |
| 58 | { |
| 59 | } |
| 60 |
| 61 | vcow_ptr(const vcow_ptr& x) noexcept : mModel(x.mModel) |
| 62 | { |
| 63 | assert(mModel); |
| 64 | ++mModel->mRef; |
| 65 | } |
| 66 | vcow_ptr(vcow_ptr&& x) noexcept : mModel(x.mModel) |
| 67 | { |
| 68 | assert(mModel); |
| 69 | x.mModel = nullptr; |
| 70 | } |
| 71 |
| 72 | auto operator=(const vcow_ptr& x) noexcept -> vcow_ptr& |
| 73 | { |
| 74 | return *this = vcow_ptr(x); |
| 75 | } |
| 76 |
| 77 | auto operator=(vcow_ptr&& x) noexcept -> vcow_ptr& |
| 78 | { |
| 79 | auto tmp = std::move(x); |
| 80 | swap(*this, tmp); |
| 81 | return *this; |
| 82 | } |
| 83 |
| 84 | auto operator*() const noexcept -> const element_type& { return read(); } |
| 85 |
| 86 | auto operator-> () const noexcept -> const element_type* { return &read(); } |
| 87 |
| 88 | int refCount() const noexcept |
| 89 | { |
| 90 | assert(mModel); |
| 91 |
| 92 | return mModel->mRef; |
| 93 | } |
| 94 |
| 95 | bool unique() const noexcept |
| 96 | { |
| 97 | assert(mModel); |
| 98 |
| 99 | return mModel->mRef == 1; |
| 100 | } |
| 101 |
| 102 | auto write() -> element_type& |
| 103 | { |
| 104 | if (!unique()) *this = vcow_ptr(read()); |
| 105 |
| 106 | return mModel->mValue; |
| 107 | } |
| 108 |
| 109 | auto read() const noexcept -> const element_type& |
| 110 | { |
| 111 | assert(mModel); |
| 112 |
| 113 | return mModel->mValue; |
| 114 | } |
| 115 |
| 116 | friend inline void swap(vcow_ptr& x, vcow_ptr& y) noexcept |
| 117 | { |
| 118 | std::swap(x.mModel, y.mModel); |
| 119 | } |
| 120 | }; |
| 121 |
| 122 | #endif // VCOWPTR_H |
| 123 |