Skip to content

Commit 3fd8f81

Browse files
committed
Misc changes. Implement direct access for interfaces.
1 parent 1119cbd commit 3fd8f81

File tree

7 files changed

+348
-185
lines changed

7 files changed

+348
-185
lines changed

include/capnp/altc++/common.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
#ifndef CAPNP_ALTCXX_COMMON_H_
2525
#define CAPNP_ALTCXX_COMMON_H_
2626

27+
#include <kj/common.h>
28+
2729
namespace capnp {
2830
namespace altcxx {
2931

3032
template <bool b, typename T, typename F> struct Conditional_ {typedef T Type;};
3133
template <typename T, typename F> struct Conditional_<false, T, F> {typedef F Type;};
3234
template <bool b, typename T, typename F> using Conditional = typename Conditional_<b, T, F>::Type;
3335

36+
template <typename T, typename U> struct Same { static constexpr bool VALUE = false; };
37+
template <typename T> struct Same<T, T> { static constexpr bool VALUE = true; };
38+
template <typename T, typename U> inline constexpr bool same() { return Same<T, U>::VALUE; }
39+
3440
template <template <typename...> class Template, typename... Args>
3541
struct Apply {
3642
template <typename... FreeArgs>
@@ -42,6 +48,36 @@ struct Constant {
4248
static constexpr T VALUE = V;
4349
};
4450

51+
// ---------------------------------------------------------------------------------------
52+
53+
template <typename... Ts>
54+
struct Types {
55+
template <typename... Us>
56+
using Add = Types<Ts..., Us...>;
57+
};
58+
59+
template <typename Types1, typename Types2> struct Cat_;
60+
61+
template <typename... T1s, typename... T2s>
62+
struct Cat_<Types<T1s...>, Types<T2s...>> {
63+
typedef Types<T1s..., T2s...> Result;
64+
};
65+
66+
template <typename Types1, typename Types2>
67+
using Cat = typename Cat_<Types1, Types2>::Result;
68+
69+
template <typename T, typename Types> struct Contains;
70+
71+
template <typename T, typename T0, typename... Ts>
72+
struct Contains<T, Types<T0, Ts...>> {
73+
static constexpr bool VALUE = (same<T, T0>() || Contains<T, Types<Ts...>>::VALUE);
74+
};
75+
76+
template <typename T>
77+
struct Contains<T, Types<>> {
78+
static constexpr bool VALUE = false;
79+
};
80+
4581
} // namespace altcxx
4682
} // namespace capnp
4783

include/capnp/altc++/impl-rpc.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2014, Jakub Spiewak <j.m.spiewak@gmail.com>
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// 1. Redistributions of source code must retain the above copyright notice, this
8+
// list of conditions and the following disclaimer.
9+
// 2. Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
24+
#ifndef CAPNP_ALTCXX_IMPL_RPC_H_
25+
#define CAPNP_ALTCXX_IMPL_RPC_H_
26+
27+
#include <capnp/generated-header-support.h>
28+
#include <capnp/capability.h>
29+
#include "common.h"
30+
31+
namespace capnp {
32+
namespace altcxx {
33+
34+
struct ClientRoot {
35+
template <typename = void>
36+
using ClientBase = Capability::Client;
37+
};
38+
39+
template <typename T>
40+
class Client : public T::template ClientBase<typename T::Extends::template Add<ClientRoot>> {
41+
typedef typename T::template ClientBase<typename T::Extends::template Add<ClientRoot>> Base;
42+
43+
public:
44+
Client() = delete;
45+
Client(Client&&) = default;
46+
Client(decltype(nullptr)) : Base(nullptr) {}
47+
Client(kj::Exception&& e) : Base(kj::mv(e)) {}
48+
Client(kj::Own<ClientHook>&& hook) : Base(kj::mv(hook)) {}
49+
Client(kj::Own<typename T::Server>&& server) : Base(kj::mv(server)) {}
50+
Client(kj::Promise<typename T::Client>&& promise) : Base(kj::mv(promise)) {}
51+
52+
template <typename U, typename = kj::EnableIf<Contains<T, typename U::Extends>::VALUE>>
53+
Client(kj::Own<U>&& server) : Base(kj::mv(server)) {}
54+
55+
template <typename U, typename = kj::EnableIf<Contains<T, typename U::Extends>::VALUE>>
56+
Client(kj::Promise<U>&& promise) : Base(kj::mv(promise)) {}
57+
58+
Client& operator = (Client&&) = default;
59+
60+
template <typename U>
61+
Client& operator = (U&& val) {
62+
return *this = Client(kj::fwd<U>(val));
63+
}
64+
};
65+
66+
} // namespace altcxx
67+
} // namespace capnp
68+
69+
#endif // CAPNP_ALTCXX_IMPL_RPC_H_
70+

include/capnp/altc++/impl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ struct ReaderHelper {
5151
template <typename T>
5252
using TypeFor = ReaderFor<T>;
5353

54-
template <typename T>
55-
static Struct getStruct(Pointer ptr, const word* def) {
54+
static Struct getStruct(Pointer ptr, _::StructSize, const word* def) {
5655
return ptr.getStruct(def);
5756
}
5857

@@ -69,9 +68,8 @@ struct BuilderHelper {
6968
template <typename T>
7069
using TypeFor = BuilderFor<T>;
7170

72-
template <typename T>
73-
static Struct getStruct(Pointer ptr, const word* def) {
74-
return ptr.getStruct(_::structSize<T>(), def);
71+
static Struct getStruct(Pointer ptr, _::StructSize size, const word* def) {
72+
return ptr.getStruct(size, def);
7573
}
7674

7775
static _::StructReader asReader(Struct s) {
@@ -84,20 +82,22 @@ struct RootTransorm {
8482
typedef HelperT Helper;
8583
static constexpr int DEPTH = 0;
8684

87-
static typename Helper::Struct transform(void* ptr) {
85+
template <typename T>
86+
static typename Helper::Struct& transform(T* ptr) {
8887
return *reinterpret_cast<typename Helper::Struct*>(ptr);
8988
}
9089
};
9190

9291
// =======================================================================================
9392

94-
template <typename Transform>
95-
struct BasicImpl: public Transform::Helper {
93+
template <typename TransformT>
94+
struct BasicImpl: public TransformT::Helper {
95+
typedef TransformT Transform;
9696
typedef typename Transform::Helper Helper;
9797
static constexpr int DEPTH = Transform::DEPTH;
9898

9999
template <template <typename...> class NewTransform>
100-
using Push = BasicImpl<NewTransform<Transform> >;
100+
using Push = BasicImpl<NewTransform<Transform>>;
101101

102102
template <typename Friend>
103103
class UnionMember {
@@ -115,11 +115,11 @@ struct BasicImpl: public Transform::Helper {
115115

116116
template <typename T>
117117
static typename Helper::Struct asStruct(T* ptr) {
118-
return Transform::transform(reinterpret_cast<void*>(ptr));
118+
return Transform::transform(ptr);
119119
}
120120
};
121121

122-
struct ReaderImpl: public BasicImpl<RootTransorm<ReaderHelper> > {
122+
struct ReaderImpl: public BasicImpl<RootTransorm<ReaderHelper>> {
123123
template <typename Friend>
124124
struct UnionMember: private _::StructReader {
125125
friend Friend;
@@ -141,7 +141,7 @@ struct ReaderImpl: public BasicImpl<RootTransorm<ReaderHelper> > {
141141
};
142142
};
143143

144-
struct BuilderImpl: public BasicImpl<RootTransorm<BuilderHelper> > {
144+
struct BuilderImpl: public BasicImpl<RootTransorm<BuilderHelper>> {
145145
template <typename Friend>
146146
struct UnionMember: private _::StructBuilder {
147147
friend Friend;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2014, Jakub Spiewak <j.m.spiewak@gmail.com>
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// 1. Redistributions of source code must retain the above copyright notice, this
8+
// list of conditions and the following disclaimer.
9+
// 2. Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
24+
#ifndef CAPNP_ALTCXX_PROPERTY_RPC_H_
25+
#define CAPNP_ALTCXX_PROPERTY_RPC_H_
26+
27+
#include "property.h"
28+
#include "impl-rpc.h"
29+
30+
namespace capnp {
31+
namespace altcxx {
32+
33+
template <uint offset, typename Union, typename Parent>
34+
struct ClientPointer {
35+
class Client : public Capability::Client {
36+
public:
37+
Client(kj::Own<ClientHook>&& hook) : Capability::Client(kj::mv(hook)) {}
38+
39+
template <typename Params, typename Results>
40+
Request<Params, Results> newCall(uint64_t iId, uint16_t mId, kj::Maybe<MessageSize> hint) {
41+
return Capability::Client::newCall<Params, Results>(iId, mId, hint);
42+
}
43+
};
44+
45+
template <typename = void>
46+
class ClientBase {
47+
protected:
48+
template <typename Params, typename Results>
49+
Request<Params, Results> newCall(uint64_t iId, uint16_t mId, kj::Maybe<MessageSize> hint) {
50+
typename Parent::Helper::Struct s = Parent::transform(this);
51+
Union::check(s);
52+
typename Parent::Helper::Pointer ptr = s.getPointerField(offset);
53+
return Client(ptr.getCapability()).newCall<Params, Results>(iId, mId, hint);
54+
}
55+
};
56+
};
57+
58+
template <typename Impl, uint offset, typename T, typename Union = NotInUnion>
59+
struct InterfaceProperty: PointerProperty<Impl, offset, T, NoDefault, Union>,
60+
public T::template ClientBase<typename T::Extends::template Add<
61+
ClientPointer<offset, Union, typename Impl::Transform>>> {
62+
template <typename = kj::EnableIf<!Impl::CONST>>
63+
void set(typename T::Client&& val) {
64+
auto s = Impl::asStruct(this);
65+
Union::setDiscriminant(s);
66+
_::PointerHelpers<T>::set(s.getPointerField(offset), kj::mv(val));
67+
}
68+
69+
template <typename = kj::EnableIf<!Impl::CONST>>
70+
void set(typename T::Client& val) {
71+
auto s = Impl::asStruct(this);
72+
Union::setDiscriminant(s);
73+
_::PointerHelpers<T>::set(s.getPointerField(offset), val);
74+
}
75+
76+
template <typename U, typename... A>
77+
typename U::Client castAs(A&&... a) { return this->get().castAs<U>(kj::fwd<A>(a)...); }
78+
79+
InterfaceProperty& operator = (Orphan<T>&& val) { this->adopt(kj::mv(val)); return *this; }
80+
InterfaceProperty& operator = (typename T::Client& val) { set(val); return *this; }
81+
InterfaceProperty& operator = (typename T::Client&& val) { set(kj::mv(val)); return *this; }
82+
83+
template <typename U>
84+
InterfaceProperty& operator = (U&& val) {
85+
return *this = typename T::Client(kj::mv(val));
86+
}
87+
};
88+
89+
} // namespace altcxx
90+
} // namespace capnp
91+
92+
#endif // CAPNP_ALTCXX_PROPERTY_RPC_H_

0 commit comments

Comments
 (0)