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

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

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/router.cc ('k') | mojo/public/cpp/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_CPP_BINDINGS_LIB_SHARED_DATA_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_
7
8 #include "mojo/public/cpp/system/macros.h"
9
10 namespace mojo {
11 namespace internal {
12
13 // Used to allocate an instance of T that can be shared via reference counting.
14 template <typename T>
15 class SharedData {
16 public:
17 ~SharedData() { holder_->Release(); }
18
19 SharedData() : holder_(new Holder()) {}
20
21 explicit SharedData(const T& value) : holder_(new Holder(value)) {}
22
23 SharedData(const SharedData<T>& other) : holder_(other.holder_) {
24 holder_->Retain();
25 }
26
27 SharedData<T>& operator=(const SharedData<T>& other) {
28 if (other.holder_ == holder_)
29 return *this;
30 holder_->Release();
31 holder_ = other.holder_;
32 holder_->Retain();
33 return *this;
34 }
35
36 void reset() {
37 holder_->Release();
38 holder_ = new Holder();
39 }
40
41 void reset(const T& value) {
42 holder_->Release();
43 holder_ = new Holder(value);
44 }
45
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; }
49
50 private:
51 class Holder {
52 public:
53 Holder() : value(), ref_count_(1) {}
54 Holder(const T& value) : value(value), ref_count_(1) {}
55
56 void Retain() { ++ref_count_; }
57 void Release() {
58 if (--ref_count_ == 0)
59 delete this;
60 }
61
62 T value;
63
64 private:
65 int ref_count_;
66 MOJO_DISALLOW_COPY_AND_ASSIGN(Holder);
67 };
68
69 Holder* holder_;
70 };
71
72 } // namespace internal
73 } // namespace mojo
74
75 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SHARED_DATA_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/router.cc ('k') | mojo/public/cpp/bindings/lib/shared_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698