OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 CONTENT_RENDERER_POSSIBLY_ASSOCIATED_INTERFACE_PTR_H_ |
| 6 #define CONTENT_RENDERER_POSSIBLY_ASSOCIATED_INTERFACE_PTR_H_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 10 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 // PossiblyAssociatedInterfacePtr<T> contains mojo::InterfacePtr<T> or |
| 15 // mojo::AssociatedInterfacePtr<T>, but not both. Mojo-related functions in |
| 16 // mojo::InterfacePtr<T> and mojo::AssociatedInterfacePtr<T> are not accessible, |
| 17 // but a user can access the raw pointer to the interface. |
| 18 template <typename T> |
| 19 class PossiblyAssociatedInterfacePtr final { |
| 20 public: |
| 21 PossiblyAssociatedInterfacePtr() {} |
| 22 PossiblyAssociatedInterfacePtr(std::nullptr_t) {} |
| 23 PossiblyAssociatedInterfacePtr(mojo::InterfacePtr<T> independent_ptr) |
| 24 : independent_ptr_(std::move(independent_ptr)) {} |
| 25 PossiblyAssociatedInterfacePtr(mojo::AssociatedInterfacePtr<T> associated_ptr) |
| 26 : associated_ptr_(std::move(associated_ptr)) {} |
| 27 |
| 28 PossiblyAssociatedInterfacePtr(PossiblyAssociatedInterfacePtr&& other) { |
| 29 independent_ptr_ = std::move(other.independent_ptr_); |
| 30 associated_ptr_ = std::move(other.associated_ptr_); |
| 31 } |
| 32 ~PossiblyAssociatedInterfacePtr() {} |
| 33 |
| 34 PossiblyAssociatedInterfacePtr& operator=( |
| 35 PossiblyAssociatedInterfacePtr&& other) { |
| 36 independent_ptr_ = std::move(other.independent_ptr_); |
| 37 associated_ptr_ = std::move(other.associated_ptr_); |
| 38 return *this; |
| 39 } |
| 40 |
| 41 T* get() const { |
| 42 return independent_ptr_ ? independent_ptr_.get() : associated_ptr_.get(); |
| 43 } |
| 44 T* operator->() const { return get(); } |
| 45 T& operator*() const { return *get(); } |
| 46 explicit operator bool() const { return get(); } |
| 47 |
| 48 private: |
| 49 mojo::InterfacePtr<T> independent_ptr_; |
| 50 mojo::AssociatedInterfacePtr<T> associated_ptr_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(PossiblyAssociatedInterfacePtr); |
| 53 }; |
| 54 |
| 55 } // namespace content |
| 56 |
| 57 #endif // CONTENT_RENDERER_POSSIBLY_ASSOCIATED_INTERFACE_PTR_H_ |
OLD | NEW |