• 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_REQUEST_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_REQUEST_H_
7 
8 #include <utility>
9 
10 #include "base/macros.h"
11 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
12 
13 namespace mojo {
14 
15 // AssociatedInterfaceRequest represents an associated interface request. It is
16 // similar to InterfaceRequest except that it doesn't own a message pipe handle.
17 template <typename Interface>
18 class AssociatedInterfaceRequest {
19  public:
20   // Constructs an empty AssociatedInterfaceRequest, representing that the
21   // client is not requesting an implementation of Interface.
AssociatedInterfaceRequest()22   AssociatedInterfaceRequest() {}
AssociatedInterfaceRequest(decltype (nullptr))23   AssociatedInterfaceRequest(decltype(nullptr)) {}
24 
25   // Takes the interface endpoint handle from another
26   // AssociatedInterfaceRequest.
AssociatedInterfaceRequest(AssociatedInterfaceRequest && other)27   AssociatedInterfaceRequest(AssociatedInterfaceRequest&& other) {
28     handle_ = std::move(other.handle_);
29   }
30   AssociatedInterfaceRequest& operator=(AssociatedInterfaceRequest&& other) {
31     if (this != &other)
32       handle_ = std::move(other.handle_);
33     return *this;
34   }
35 
36   // Assigning to nullptr resets the AssociatedInterfaceRequest to an empty
37   // state, closing the interface endpoint handle currently bound to it (if
38   // any).
decltype(nullptr)39   AssociatedInterfaceRequest& operator=(decltype(nullptr)) {
40     handle_.reset();
41     return *this;
42   }
43 
44   // Indicates whether the request currently contains a valid interface endpoint
45   // handle.
is_pending()46   bool is_pending() const { return handle_.is_valid(); }
47 
Bind(ScopedInterfaceEndpointHandle handle)48   void Bind(ScopedInterfaceEndpointHandle handle) {
49     handle_ = std::move(handle);
50   }
51 
PassHandle()52   ScopedInterfaceEndpointHandle PassHandle() {
53     return std::move(handle_);
54   }
55 
handle()56   const ScopedInterfaceEndpointHandle& handle() const { return handle_; }
57 
Equals(const AssociatedInterfaceRequest & other)58   bool Equals(const AssociatedInterfaceRequest& other) const {
59     if (this == &other)
60       return true;
61 
62     // Now that the two refer to different objects, they are equivalent if
63     // and only if they are both invalid.
64     return !is_pending() && !other.is_pending();
65   }
66 
67  private:
68   ScopedInterfaceEndpointHandle handle_;
69 
70   DISALLOW_COPY_AND_ASSIGN(AssociatedInterfaceRequest);
71 };
72 
73 // Makes an AssociatedInterfaceRequest bound to the specified associated
74 // endpoint.
75 template <typename Interface>
MakeAssociatedRequest(ScopedInterfaceEndpointHandle handle)76 AssociatedInterfaceRequest<Interface> MakeAssociatedRequest(
77     ScopedInterfaceEndpointHandle handle) {
78   AssociatedInterfaceRequest<Interface> request;
79   request.Bind(std::move(handle));
80   return request;
81 }
82 
83 }  // namespace mojo
84 
85 #endif  // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_REQUEST_H_
86