Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / bindings / struct_ptr.h
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_STRUCT_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
7
8 #include <new>
9
10 #include "mojo/public/cpp/bindings/type_converter.h"
11 #include "mojo/public/cpp/environment/logging.h"
12 #include "mojo/public/cpp/system/macros.h"
13
14 namespace mojo {
15 namespace internal {
16
17 template <typename Struct>
18 class StructHelper {
19  public:
20   template <typename Ptr>
21   static void Initialize(Ptr* ptr) {
22     ptr->Initialize();
23   }
24 };
25
26 }  // namespace internal
27
28 template <typename Struct>
29 class StructPtr {
30   MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr, RValue);
31
32  public:
33
34   StructPtr() : ptr_(nullptr) {}
35   ~StructPtr() { delete ptr_; }
36
37   StructPtr(RValue other) : ptr_(nullptr) { Take(other.object); }
38   StructPtr& operator=(RValue other) {
39     Take(other.object);
40     return *this;
41   }
42
43   template <typename U>
44   U To() const {
45     return TypeConverter<U, StructPtr>::Convert(*this);
46   }
47
48   void reset() {
49     if (ptr_) {
50       delete ptr_;
51       ptr_ = nullptr;
52     }
53   }
54
55   bool is_null() const { return ptr_ == nullptr; }
56
57   Struct& operator*() const {
58     MOJO_DCHECK(ptr_);
59     return *ptr_;
60   }
61   Struct* operator->() const {
62     MOJO_DCHECK(ptr_);
63     return ptr_;
64   }
65   Struct* get() const { return ptr_; }
66
67   void Swap(StructPtr* other) { std::swap(ptr_, other->ptr_); }
68
69   // Please note that calling this method will fail compilation if the value
70   // type |Struct| doesn't have a Clone() method defined (which usually means
71   // that it contains Mojo handles).
72   StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); }
73
74   bool Equals(const StructPtr& other) const {
75     if (is_null() || other.is_null())
76       return is_null() && other.is_null();
77     return ptr_->Equals(*other.ptr_);
78   }
79
80  private:
81   typedef Struct* StructPtr::*Testable;
82
83  public:
84   operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; }
85
86  private:
87   friend class internal::StructHelper<Struct>;
88   void Initialize() {
89     MOJO_DCHECK(!ptr_);
90     ptr_ = new Struct();
91   }
92
93   void Take(StructPtr* other) {
94     reset();
95     Swap(other);
96   }
97
98   Struct* ptr_;
99 };
100
101 // Designed to be used when Struct is small and copyable.
102 template <typename Struct>
103 class InlinedStructPtr {
104   MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr, RValue);
105
106  public:
107
108   InlinedStructPtr() : is_null_(true) {}
109   ~InlinedStructPtr() {}
110
111   InlinedStructPtr(RValue other) : is_null_(true) { Take(other.object); }
112   InlinedStructPtr& operator=(RValue other) {
113     Take(other.object);
114     return *this;
115   }
116
117   template <typename U>
118   U To() const {
119     return TypeConverter<U, InlinedStructPtr>::Convert(*this);
120   }
121
122   void reset() {
123     is_null_ = true;
124     value_. ~Struct();
125     new (&value_) Struct();
126   }
127
128   bool is_null() const { return is_null_; }
129
130   Struct& operator*() const {
131     MOJO_DCHECK(!is_null_);
132     return value_;
133   }
134   Struct* operator->() const {
135     MOJO_DCHECK(!is_null_);
136     return &value_;
137   }
138   Struct* get() const { return &value_; }
139
140   void Swap(InlinedStructPtr* other) {
141     std::swap(value_, other->value_);
142     std::swap(is_null_, other->is_null_);
143   }
144
145   InlinedStructPtr Clone() const {
146     return is_null() ? InlinedStructPtr() : value_.Clone();
147   }
148   bool Equals(const InlinedStructPtr& other) const {
149     if (is_null() || other.is_null())
150       return is_null() && other.is_null();
151     return value_.Equals(other.value_);
152   }
153
154  private:
155   typedef Struct InlinedStructPtr::*Testable;
156
157  public:
158   operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; }
159
160  private:
161   friend class internal::StructHelper<Struct>;
162   void Initialize() { is_null_ = false; }
163
164   void Take(InlinedStructPtr* other) {
165     reset();
166     Swap(other);
167   }
168
169   mutable Struct value_;
170   bool is_null_;
171 };
172
173 }  // namespace mojo
174
175 #endif  // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_