Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #ifndef IROHA_SHARED_MODEL_TRIVIAL_PROTO_HPP
19 : #define IROHA_SHARED_MODEL_TRIVIAL_PROTO_HPP
20 :
21 : #include "utils/reference_holder.hpp"
22 :
23 : namespace shared_model {
24 : namespace proto {
25 : /**
26 : * Simple generic class for handling proto objects
27 : * @tparam Iface is interface to inherit from
28 : * @tparam Proto is protobuf container
29 : */
30 : template <typename Iface, typename Proto>
31 : class TrivialProto final : public Iface {
32 : public:
33 : /**
34 : * @tparm ProtoLoader generic param so it can be handled
35 : * in the load for the boost::variant
36 : */
37 : template <typename ProtoLoader>
38 : explicit TrivialProto(ProtoLoader &&ref)
39 1740 : : proto_(std::forward<ProtoLoader>(ref)) {}
40 :
41 : protected:
42 : typename Iface::ModelType *clone() const override {
43 0 : return new TrivialProto(Proto(*proto_));
44 0 : }
45 :
46 : private:
47 : detail::ReferenceHolder<Proto> proto_;
48 : };
49 :
50 : /**
51 : * Simple generic class for handling proto objects
52 : * @tparam Iface is interface to inherit from
53 : * @tparam Proto is protobuf container
54 : * @tparam Impl is implementation of Iface
55 : */
56 : template <typename Iface, typename Proto, typename Impl>
57 : class CopyableProto : public Iface {
58 : public:
59 : /**
60 : * @tparm ProtoLoader generic param so it can be handled
61 : * in the load for the boost::variant
62 : */
63 : template <typename ProtoLoader>
64 : explicit CopyableProto(ProtoLoader &&ref)
65 82881 : : proto_(std::forward<ProtoLoader>(ref)) {}
66 :
67 : using TransportType = Proto;
68 :
69 : const Proto &getTransport() const {
70 4 : return *proto_;
71 : }
72 :
73 : protected:
74 : typename Iface::ModelType *clone() const override final {
75 971 : return new Impl(Proto(*proto_));
76 0 : }
77 : detail::ReferenceHolder<Proto> proto_;
78 : };
79 : } // namespace proto
80 : } // namespace shared_model
81 :
82 : #endif // IROHA_SHARED_MODEL_TRIVIAL_PROTO_HPP
|