OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 | 9 |
10 #include "mojo/public/cpp/bindings/message.h" | 10 #include "mojo/public/cpp/bindings/message.h" |
11 #include "mojo/public/cpp/system/core.h" | 11 #include "mojo/public/cpp/system/core.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 | 15 |
16 // NoInterface is for use in cases when a non-existent or empty interface is | 16 // NoInterface is for use in cases when a non-existent or empty interface is |
17 // needed (e.g., when the Mojom "Peer" attribute is not present). | 17 // needed (e.g., when the Mojom "Peer" attribute is not present). |
18 | 18 |
19 class NoInterface; | 19 class NoInterfaceProxy; |
| 20 class NoInterfaceStub; |
| 21 |
| 22 class NoInterface { |
| 23 public: |
| 24 typedef NoInterfaceProxy Proxy_; |
| 25 typedef NoInterfaceStub Stub_; |
| 26 typedef NoInterface Client_; |
| 27 virtual ~NoInterface() {} |
| 28 virtual void SetClient(NoInterface* client) {} |
| 29 }; |
| 30 |
| 31 class NoInterfaceProxy : public NoInterface { |
| 32 public: |
| 33 explicit NoInterfaceProxy(MessageReceiver* receiver) {} |
| 34 }; |
20 | 35 |
21 class NoInterfaceStub : public MessageReceiver { | 36 class NoInterfaceStub : public MessageReceiver { |
22 public: | 37 public: |
23 NoInterfaceStub(NoInterface* unused) {} | 38 NoInterfaceStub() {} |
| 39 void set_sink(NoInterface* sink) {} |
24 virtual bool Accept(Message* message) MOJO_OVERRIDE; | 40 virtual bool Accept(Message* message) MOJO_OVERRIDE; |
25 virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder) | 41 virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder) |
26 MOJO_OVERRIDE; | 42 MOJO_OVERRIDE; |
27 }; | 43 }; |
28 | 44 |
29 class NoInterface { | |
30 public: | |
31 typedef NoInterfaceStub _Stub; | |
32 typedef NoInterface _Peer; | |
33 }; | |
34 | |
35 | 45 |
36 // AnyInterface is for use in cases where any interface would do (e.g., see the | 46 // AnyInterface is for use in cases where any interface would do (e.g., see the |
37 // Shell::Connect method). | 47 // Shell::Connect method). |
38 | 48 |
39 typedef NoInterface AnyInterface; | 49 typedef NoInterface AnyInterface; |
40 | 50 |
41 | |
42 // InterfaceHandle<S> | |
43 | |
44 template <typename S> | |
45 class InterfaceHandle : public MessagePipeHandle { | |
46 public: | |
47 InterfaceHandle() {} | |
48 explicit InterfaceHandle(MojoHandle value) : MessagePipeHandle(value) {} | |
49 }; | |
50 | |
51 | |
52 // Interface<S> | |
53 | |
54 template <typename S> | |
55 struct Interface { | |
56 typedef InterfaceHandle<S> Handle; | |
57 typedef ScopedHandleBase<InterfaceHandle<S> > ScopedHandle; | |
58 }; | |
59 | |
60 template <> | |
61 struct Interface<mojo::NoInterface> { | |
62 typedef MessagePipeHandle Handle; | |
63 typedef ScopedMessagePipeHandle ScopedHandle; | |
64 }; | |
65 | |
66 | |
67 // InterfacePipe<S,P> is used to construct a MessagePipe with typed interfaces | |
68 // on either end. | |
69 | |
70 template <typename S, typename P = typename S::_Peer> | |
71 class InterfacePipe { | |
72 public: | |
73 InterfacePipe() { | |
74 typename Interface<S>::Handle h0; | |
75 typename Interface<P>::Handle h1; | |
76 MojoResult result MOJO_ALLOW_UNUSED = | |
77 MojoCreateMessagePipe(h0.mutable_value(), h1.mutable_value()); | |
78 assert(result == MOJO_RESULT_OK); | |
79 handle_to_self.reset(h0); | |
80 handle_to_peer.reset(h1); | |
81 } | |
82 | |
83 typename Interface<S>::ScopedHandle handle_to_self; | |
84 typename Interface<P>::ScopedHandle handle_to_peer; | |
85 }; | |
86 | |
87 // TODO(darin): Once we have the ability to use C++11 features, consider | |
88 // defining a template alias for ScopedInterfaceHandle<S>. | |
89 | |
90 } // namespace mojo | 51 } // namespace mojo |
91 | 52 |
92 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ | 53 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_H_ |
OLD | NEW |