|
| 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