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