Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Side by Side Diff: mojo/public/bindings/lib/shared_data.h

Issue 220243007: Mojo: Move mojo/public/bindings/lib to mojo/public/cpp/bindings/lib. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/lib/scratch_buffer.cc ('k') | mojo/public/bindings/lib/shared_ptr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_
7
8 namespace mojo {
9 namespace internal {
10
11 // Used to allocate an instance of T that can be shared via reference counting.
12 template <typename T>
13 class SharedData {
14 public:
15 ~SharedData() {
16 holder_->Release();
17 }
18
19 SharedData() : holder_(new Holder()) {
20 }
21
22 explicit SharedData(const T& value) : holder_(new Holder(value)) {
23 }
24
25 SharedData(const SharedData<T>& other) : holder_(other.holder_) {
26 holder_->Retain();
27 }
28
29 SharedData<T>& operator=(const SharedData<T>& other) {
30 if (other.holder_ == holder_)
31 return *this;
32 holder_->Release();
33 holder_ = other.holder_;
34 holder_->Retain();
35 }
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
47 void set_value(const T& value) {
48 holder_->value = value;
49 }
50 T* mutable_value() {
51 return &holder_->value;
52 }
53 const T& value() const {
54 return holder_->value;
55 }
56
57 private:
58 class Holder {
59 public:
60 Holder() : value(), ref_count_(1) {
61 }
62 Holder(const T& value) : value(value), ref_count_(1) {
63 }
64
65 void Retain() { ++ref_count_; }
66 void Release() { if (--ref_count_ == 0) delete this; }
67
68 T value;
69
70 private:
71 int ref_count_;
72 MOJO_DISALLOW_COPY_AND_ASSIGN(Holder);
73 };
74
75 Holder* holder_;
76 };
77
78 } // namespace internal
79 } // namespace mojo
80
81 #endif // MOJO_PUBLIC_BINDINGS_LIB_SHARED_DATA_H_
OLDNEW
« no previous file with comments | « mojo/public/bindings/lib/scratch_buffer.cc ('k') | mojo/public/bindings/lib/shared_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698