| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ | 5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ | 6 #define MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ |
| 7 | 7 |
| 8 namespace mojo { | 8 namespace mojo { |
| 9 namespace internal { | 9 namespace internal { |
| 10 | 10 |
| 11 // Used to allocate an instance of T that can be shared via reference counting. | 11 // Used to allocate an instance of T that can be shared via reference counting. |
| 12 template <typename T> | 12 template <typename T> |
| 13 class SharedData { | 13 class SharedData { |
| 14 public: | 14 public: |
| 15 ~SharedData() { | 15 ~SharedData() { |
| 16 holder_->Release(); | 16 holder_->Release(); |
| 17 } | 17 } |
| 18 | 18 |
| 19 SharedData() : holder_(new Holder()) { |
| 20 } |
| 21 |
| 19 explicit SharedData(const T& value) : holder_(new Holder(value)) { | 22 explicit SharedData(const T& value) : holder_(new Holder(value)) { |
| 20 } | 23 } |
| 21 | 24 |
| 22 SharedData(const SharedData<T>& other) : holder_(other.holder_) { | 25 SharedData(const SharedData<T>& other) : holder_(other.holder_) { |
| 23 holder_->Retain(); | 26 holder_->Retain(); |
| 24 } | 27 } |
| 25 | 28 |
| 26 SharedData<T>& operator=(const SharedData<T>& other) { | 29 SharedData<T>& operator=(const SharedData<T>& other) { |
| 27 if (other.holder_ == holder_) | 30 if (other.holder_ == holder_) |
| 28 return *this; | 31 return *this; |
| 29 holder_->Release(); | 32 holder_->Release(); |
| 30 holder_ = other.holder_; | 33 holder_ = other.holder_; |
| 31 holder_->Retain(); | 34 holder_->Retain(); |
| 32 } | 35 } |
| 33 | 36 |
| 37 void reset() { |
| 38 holder_->Release(); |
| 39 holder_ = new Holder(); |
| 40 } |
| 41 |
| 42 void reset(const T& value) { |
| 43 holder_->Release(); |
| 44 holder_ = new Holder(value); |
| 45 } |
| 46 |
| 34 void set_value(const T& value) { | 47 void set_value(const T& value) { |
| 35 holder_->value = value; | 48 holder_->value = value; |
| 36 } | 49 } |
| 50 T* mutable_value() { |
| 51 return &holder_->value; |
| 52 } |
| 37 const T& value() const { | 53 const T& value() const { |
| 38 return holder_->value; | 54 return holder_->value; |
| 39 } | 55 } |
| 40 | 56 |
| 41 private: | 57 private: |
| 42 SharedData(); // NOT IMPLEMENTED | |
| 43 | |
| 44 class Holder { | 58 class Holder { |
| 45 public: | 59 public: |
| 46 Holder() : value(), ref_count_(1) { | 60 Holder() : value(), ref_count_(1) { |
| 47 } | 61 } |
| 48 Holder(const T& value) : value(value), ref_count_(1) { | 62 Holder(const T& value) : value(value), ref_count_(1) { |
| 49 } | 63 } |
| 50 | 64 |
| 51 void Retain() { ++ref_count_; } | 65 void Retain() { ++ref_count_; } |
| 52 void Release() { if (--ref_count_ == 0) delete this; } | 66 void Release() { if (--ref_count_ == 0) delete this; } |
| 53 | 67 |
| 54 T value; | 68 T value; |
| 55 | 69 |
| 56 private: | 70 private: |
| 57 int ref_count_; | 71 int ref_count_; |
| 58 MOJO_DISALLOW_COPY_AND_ASSIGN(Holder); | 72 MOJO_DISALLOW_COPY_AND_ASSIGN(Holder); |
| 59 }; | 73 }; |
| 60 | 74 |
| 61 Holder* holder_; | 75 Holder* holder_; |
| 62 }; | 76 }; |
| 63 | 77 |
| 64 } // namespace internal | 78 } // namespace internal |
| 65 } // namespace mojo | 79 } // namespace mojo |
| 66 | 80 |
| 67 #endif // MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ | 81 #endif // MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_ |
| OLD | NEW |