summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusTypeOutputStream.hpp
blob: a2e96e521120481bb8b8392ff228452925a1b7c9 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (C) 2014-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef COMMONAPI_DBUS_DBUSTYPEOUTPUTSTREAM_H_
#define COMMONAPI_DBUS_DBUSTYPEOUTPUTSTREAM_H_

#include <CommonAPI/TypeOutputStream.hpp>

namespace CommonAPI {
namespace DBus {

class DBusTypeOutputStream: public TypeOutputStream<DBusTypeOutputStream> {
public:
    DBusTypeOutputStream() : signature_("") {}

    TypeOutputStream &writeType(const bool &, const EmptyDeployment *) {
        signature_.append("b");
        return (*this);
    }

    TypeOutputStream &writeType(const int8_t &, const EmptyDeployment *) {
        signature_.append("y");
        return (*this);
    }

    TypeOutputStream &writeType(const int16_t &, const EmptyDeployment *) {
        signature_.append("n");
        return (*this);
    }

    TypeOutputStream &writeType(const int32_t &, const EmptyDeployment *) {
        signature_.append("i");
        return (*this);
    }

    TypeOutputStream &writeType(const  int64_t &, const EmptyDeployment *) {
        signature_.append("x");
        return (*this);
    }

    TypeOutputStream &writeType(const uint8_t &, const EmptyDeployment *) {
        signature_.append("y");
        return (*this);
    }

    TypeOutputStream &writeType(const uint16_t &, const EmptyDeployment *) {
        signature_.append("q");
        return (*this);
    }

    TypeOutputStream &writeType(const uint32_t &, const EmptyDeployment *) {
        signature_.append("u");
        return (*this);
    }

    TypeOutputStream &writeType(const uint64_t &, const EmptyDeployment *) {
        signature_.append("t");
        return (*this);
    }

    TypeOutputStream &writeType(const float &, const EmptyDeployment *) {
        signature_.append("d");
        return (*this);
    }

    TypeOutputStream &writeType(const double &, const EmptyDeployment *) {
        signature_.append("d");
        return (*this);
    }

    TypeOutputStream &writeType(const std::string &, const StringDeployment *_depl) {
        if ((_depl != nullptr) && _depl->isObjectPath_) {
            signature_.append("o");
        } else {
            signature_.append("s");
        }
        return (*this);
    }

    TypeOutputStream &writeType(const std::string &, const CommonAPI::EmptyDeployment * = nullptr) {
        signature_.append("s");
        return (*this);
    }

    TypeOutputStream &writeType() {
        signature_.append("ay");
        return (*this);
    }

    TypeOutputStream &writeType(const Version &, const EmptyDeployment *) {
        signature_.append("(uu)");
        return (*this);
    }

    TypeOutputStream &writeVersionType() {
        signature_.append("(uu)");
        return (*this);
    }

    template<typename Deployment_, typename... Types_>
    TypeOutputStream &writeType(const Struct<Types_...> &_value, const Deployment_ * _depl = nullptr) {
        (void)_value;
        signature_.append("(");
        const auto itsSize(std::tuple_size<std::tuple<Types_...>>::value);
        StructTypeWriter<Deployment_, itsSize-1, DBusTypeOutputStream, Struct<Types_...>>{}
            (*this, _value, _depl);

        signature_.append(")");
        return (*this);
    }

    template<typename Deployment_, class PolymorphicStruct_>
    TypeOutputStream &writeType(const std::shared_ptr<PolymorphicStruct_> &_value, const Deployment_ * _depl = nullptr) {
        (void)_value;
        signature_.append("(");
        _value->template writeType<>((*this), _depl);
        signature_.append(")");
        return (*this);
    }

    template<typename... Types_>
    TypeOutputStream &writeType(const Variant<Types_...> &_value, const EmptyDeployment *_depl) {
        (void)_value;
        (void)_depl;
        signature_.append("(yv)");
        return (*this);
    }

    template<typename Deployment_, typename... Types_>
    TypeOutputStream &writeType(const Variant<Types_...> &_value, const Deployment_ *_depl) {
        (void)_value;
        if (_depl != nullptr && _depl->isDBus_) {
            signature_.append("v");
        } else {
            signature_.append("(yv)");
        }

        return (*this);
    }

    template<typename ElementType_>
    TypeOutputStream &writeType(const std::vector<ElementType_> &_value, const EmptyDeployment * _depl = nullptr) {
        (void)_value;
        signature_.append("a");
        ElementType_ dummyElement;
        writeType(dummyElement, _depl);
        return (*this);
    }

    template<typename Deployment_, typename ElementType_>
    TypeOutputStream &writeType(const std::vector<ElementType_> &_value, const Deployment_ * _depl = nullptr) {
        (void)_value;
        signature_.append("a");
        ElementType_ dummyElement;
        writeType(dummyElement, (_depl ? _depl->elementDepl_ : nullptr)); 
        return (*this);
    }

    template<typename KeyType_, typename ValueType_, typename HasherType_>
    TypeOutputStream &writeType(const std::unordered_map<KeyType_, ValueType_, HasherType_> &_value, const EmptyDeployment *_depl = nullptr) {
        (void)_value;
        signature_.append("a{");
        KeyType_ dummyKey;
        writeType(dummyKey, _depl);
        ValueType_ dummyValue;
        writeType(dummyValue, _depl);
        signature_.append("}");
        return (*this);
    }

    template<typename Deployment_, typename KeyType_, typename ValueType_, typename HasherType_>
    TypeOutputStream &writeType(const std::unordered_map<KeyType_, ValueType_, HasherType_> &_value, const Deployment_ *_depl = nullptr) {
        (void)_value;
        signature_.append("a{");
        KeyType_ dummyKey;
        writeType(dummyKey, (_depl ? _depl->key_ : nullptr)); 
        ValueType_ dummyValue;
        writeType(dummyValue, (_depl ? _depl->value_ : nullptr));
        signature_.append("}");
        return (*this);
    }

    inline std::string getSignature() {
        return std::move(signature_);
    }

private:
    std::string signature_;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_DBUSTYPEOUTPUTSTREAM_HPP_