1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_
8 #include "mojo/public/cpp/system/macros.h"
13 // Used to allocate an instance of T that can be shared via reference counting.
17 ~SharedData() { holder_->Release(); }
19 SharedData() : holder_(new Holder()) {}
21 explicit SharedData(const T& value) : holder_(new Holder(value)) {}
23 SharedData(const SharedData<T>& other) : holder_(other.holder_) {
27 SharedData<T>& operator=(const SharedData<T>& other) {
28 if (other.holder_ == holder_)
31 holder_ = other.holder_;
38 holder_ = new Holder();
41 void reset(const T& value) {
43 holder_ = new Holder(value);
46 void set_value(const T& value) { holder_->value = value; }
47 T* mutable_value() { return &holder_->value; }
48 const T& value() const { return holder_->value; }
53 Holder() : value(), ref_count_(1) {}
54 Holder(const T& value) : value(value), ref_count_(1) {}
56 void Retain() { ++ref_count_; }
58 if (--ref_count_ == 0)
66 MOJO_DISALLOW_COPY_AND_ASSIGN(Holder);
72 } // namespace internal
75 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_