• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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_ASSOCIATED_INTERFACE_PTR_INFO_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_INFO_H_
7 
8 #include <stdint.h>
9 #include <utility>
10 
11 #include "base/macros.h"
12 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
13 
14 namespace mojo {
15 
16 // AssociatedInterfacePtrInfo stores necessary information to construct an
17 // associated interface pointer. It is similar to InterfacePtrInfo except that
18 // it doesn't own a message pipe handle.
19 template <typename Interface>
20 class AssociatedInterfacePtrInfo {
21  public:
AssociatedInterfacePtrInfo()22   AssociatedInterfacePtrInfo() : version_(0u) {}
AssociatedInterfacePtrInfo(std::nullptr_t)23   AssociatedInterfacePtrInfo(std::nullptr_t) : version_(0u) {}
24 
AssociatedInterfacePtrInfo(AssociatedInterfacePtrInfo && other)25   AssociatedInterfacePtrInfo(AssociatedInterfacePtrInfo&& other)
26       : handle_(std::move(other.handle_)), version_(other.version_) {
27     other.version_ = 0u;
28   }
29 
AssociatedInterfacePtrInfo(ScopedInterfaceEndpointHandle handle,uint32_t version)30   AssociatedInterfacePtrInfo(ScopedInterfaceEndpointHandle handle,
31                              uint32_t version)
32       : handle_(std::move(handle)), version_(version) {}
33 
~AssociatedInterfacePtrInfo()34   ~AssociatedInterfacePtrInfo() {}
35 
36   AssociatedInterfacePtrInfo& operator=(AssociatedInterfacePtrInfo&& other) {
37     if (this != &other) {
38       handle_ = std::move(other.handle_);
39       version_ = other.version_;
40       other.version_ = 0u;
41     }
42 
43     return *this;
44   }
45 
is_valid()46   bool is_valid() const { return handle_.is_valid(); }
47 
48   explicit operator bool() const { return handle_.is_valid(); }
49 
PassHandle()50   ScopedInterfaceEndpointHandle PassHandle() {
51     return std::move(handle_);
52   }
handle()53   const ScopedInterfaceEndpointHandle& handle() const { return handle_; }
set_handle(ScopedInterfaceEndpointHandle handle)54   void set_handle(ScopedInterfaceEndpointHandle handle) {
55     handle_ = std::move(handle);
56   }
57 
version()58   uint32_t version() const { return version_; }
set_version(uint32_t version)59   void set_version(uint32_t version) { version_ = version; }
60 
Equals(const AssociatedInterfacePtrInfo & other)61   bool Equals(const AssociatedInterfacePtrInfo& other) const {
62     if (this == &other)
63       return true;
64 
65     // Now that the two refer to different objects, they are equivalent if
66     // and only if they are both invalid.
67     return !is_valid() && !other.is_valid();
68   }
69 
70  private:
71   ScopedInterfaceEndpointHandle handle_;
72   uint32_t version_;
73 
74   DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrInfo);
75 };
76 
77 }  // namespace mojo
78 
79 #endif  // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_INFO_H_
80