summaryrefslogtreecommitdiffstats
path: root/mojo/public/bindings/interface.h
blob: a472b074faeff70d967a22aa3190060669295730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_BINDINGS_INTERFACE_H_
#define MOJO_PUBLIC_BINDINGS_INTERFACE_H_

#include <assert.h>

#include "mojo/public/bindings/message.h"
#include "mojo/public/system/core_cpp.h"

namespace mojo {


// NoInterface is for use in cases when a non-existent or empty interface is
// needed (e.g., when the Mojom "Peer" attribute is not present).

class NoInterface;

class NoInterfaceStub : public MessageReceiver {
 public:
  NoInterfaceStub(NoInterface* unused) {}
  virtual bool Accept(Message* message) MOJO_OVERRIDE;
};

class NoInterface {
 public:
  typedef NoInterfaceStub _Stub;
  typedef NoInterface _Peer;
};


// AnyInterface is for use in cases where any interface would do (e.g., see the
// Shell::Connect method).

typedef NoInterface AnyInterface;


// InterfaceHandle<S>

template <typename S>
class InterfaceHandle : public MessagePipeHandle {
 public:
  InterfaceHandle() {}
  explicit InterfaceHandle(MojoHandle value) : MessagePipeHandle(value) {}
};


// Interface<S>

template <typename S>
struct Interface {
  typedef InterfaceHandle<S> Handle;
  typedef ScopedHandleBase<InterfaceHandle<S> > ScopedHandle;
};

template <>
struct Interface<mojo::NoInterface> {
  typedef MessagePipeHandle Handle;
  typedef ScopedMessagePipeHandle ScopedHandle;
};


// InterfacePipe<S,P> is used to construct a MessagePipe with typed interfaces
// on either end.

template <typename S, typename P = typename S::_Peer>
class InterfacePipe {
 public:
  InterfacePipe() {
    typename Interface<S>::Handle h0;
    typename Interface<P>::Handle h1;
    MojoResult result MOJO_ALLOW_UNUSED =
        MojoCreateMessagePipe(h0.mutable_value(), h1.mutable_value());
    assert(result == MOJO_RESULT_OK);
    handle_to_self.reset(h0);
    handle_to_peer.reset(h1);
  }

  typename Interface<S>::ScopedHandle handle_to_self;
  typename Interface<P>::ScopedHandle handle_to_peer;
};

// TODO(darin): Once we have the ability to use C++11 features, consider
// defining a template alias for ScopedInterfaceHandle<S>.

}  // namespace mojo

#endif  // MOJO_PUBLIC_BINDINGS_SCOPED_INTERFACE_H_