summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusTypeOutputStream.hpp
blob: 074d76a41aa19eb3bc80c832cd323edb387bf955 (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
// 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 &_type) {
        signature_.append("b");
        return (*this);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    template<class _PolymorphicStruct>
    TypeOutputStream &writeType(const std::shared_ptr<_PolymorphicStruct> &_value) {
        signature_.append("(");
        _value->template writeType<>((*this));
        signature_.append(")");
        return (*this);
    }

    template<typename... _Types>
    TypeOutputStream &writeType(const Variant<_Types...> &_value) {
        signature_.append("(yv)");
        return (*this);
    }

    template<typename _Deployment, typename... _Types>
    TypeOutputStream &writeType(const Variant<_Types...> &_value, const _Deployment *_depl) {
    	if (_depl != nullptr && _depl->isDBus_) {
    		signature_.append("v");
    	} else {
    		signature_.append("(yv)");
    	}
		TypeOutputStreamWriteVisitor<DBusTypeOutputStream> typeVisitor(*this);
		ApplyVoidVisitor<TypeOutputStreamWriteVisitor<DBusTypeOutputStream>,
				Variant<_Types...>, _Types...>::visit(typeVisitor, _value);
        return (*this);
    }

	template<typename _ElementType>
	TypeOutputStream &writeType(const std::vector<_ElementType> &_value) {
		signature_.append("a");
		_ElementType dummyElement;
		writeType(dummyElement);
		return (*this);
	}

	template<typename _KeyType, typename _ValueType, typename _HasherType>
	TypeOutputStream &writeType(const std::unordered_map<_KeyType, _ValueType, _HasherType> &_value) {
		signature_.append("a{");
		_KeyType dummyKey;
		writeType(dummyKey);
		_ValueType dummyValue;
		writeType(dummyValue);
		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_