2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 std::atomic<std::size_t> mRef{1};
30 template <class... Args>
31 explicit model(Args&&... args) : mValue(std::forward<Args>(args)...)
40 using element_type = T;
44 static model default_s;
51 if (mModel && (--mModel->mRef == 0)) delete mModel;
54 template <class... Args>
55 vcow_ptr(Args&&... args) : mModel(new model(std::forward<Args>(args)...))
59 vcow_ptr(const vcow_ptr& x) noexcept : mModel(x.mModel)
64 vcow_ptr(vcow_ptr&& x) noexcept : mModel(x.mModel)
70 auto operator=(const vcow_ptr& x) noexcept -> vcow_ptr&
72 return *this = vcow_ptr(x);
75 auto operator=(vcow_ptr&& x) noexcept -> vcow_ptr&
77 auto tmp = std::move(x);
82 auto operator*() const noexcept -> const element_type& { return read(); }
84 auto operator-> () const noexcept -> const element_type* { return &read(); }
86 int refCount() const noexcept
93 bool unique() const noexcept
97 return mModel->mRef == 1;
100 auto write() -> element_type&
102 if (!unique()) *this = vcow_ptr(read());
104 return mModel->mValue;
107 auto read() const noexcept -> const element_type&
111 return mModel->mValue;
114 friend inline void swap(vcow_ptr& x, vcow_ptr& y) noexcept
116 std::swap(x.mModel, y.mModel);