summaryrefslogtreecommitdiff
path: root/src/CommonAPI/SerializableVariant.h
blob: 29cf6b2d90d43c61e6b7458fbc29f5fcd6a95c42 (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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * 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_SERIALIZABLE_VARIANT_H_
#define COMMONAPI_SERIALIZABLE_VARIANT_H_

#include <memory>
#include <cstdint>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
#include <cassert>

namespace CommonAPI {

class InputStream;
class OutputStream;

class TypeOutputStream;

template<typename _Type>
struct TypeWriter;

class SerializableVariant {
public:
    virtual ~SerializableVariant() {
    }

    virtual uint8_t getValueType() const = 0;

    virtual void readFromInputStream(const uint8_t typeIndex, InputStream& inputStream) = 0;
    virtual void writeToOutputStream(OutputStream& outputStream) const = 0;

    virtual void writeToTypeOutputStream(TypeOutputStream& typeOutputStream) const = 0;
};

template<typename ... _Types>
struct MaxSize;

template<>
struct MaxSize<> {
    static const unsigned int value = 0;
};

template<typename _Type, typename ... _Types>
struct MaxSize<_Type, _Types...> {
    static const unsigned int current_type_size = sizeof(_Type);
    static const unsigned int next_type_size = MaxSize<_Types...>::value;
    static const unsigned int value =
                    current_type_size > next_type_size ?
                                    current_type_size : next_type_size;
};

template<typename _SearchType, typename ... _RestTypes>
struct VariantTypeSelector;

template<typename _SearchType, typename ... _RestTypes>
struct VariantTypeSelector<_SearchType, _SearchType, _RestTypes...> {
    typedef _SearchType type;
};

template<typename ... _Types>
class Variant: public SerializableVariant {
private:
    typedef std::tuple_size<std::tuple<_Types...>> TypesTupleSize;

public:

    static const unsigned int maxSize = MaxSize<_Types...>::value;

    Variant();

    Variant(const Variant& fromVariant);

    Variant(Variant&& fromVariant);

    ~Variant();

    virtual void readFromInputStream(const uint8_t typeIndex, InputStream& inputStream);

    virtual void writeToOutputStream(OutputStream& outputStream) const;

    virtual void writeToTypeOutputStream(TypeOutputStream& typeOutputStream) const;

    Variant& operator=(const Variant& rhs);

    Variant& operator=(Variant&& rhs);

    template<typename _Type>
    typename std::enable_if<!std::is_same<_Type, Variant<_Types...>>::value, Variant<_Types...>&>::type
    operator=(const _Type& value);

    bool operator==(const Variant<_Types...>& rhs) const;

    bool operator!=(const Variant<_Types...>& rhs) const;

    template <typename _Type>
    const bool isType() const;

    template <typename _Type>
    Variant(const _Type& value,
                    typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
                    typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
                    typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0);

    template <typename _Type>
    Variant(_Type && value,
                    typename std::enable_if<!std::is_const<_Type>::value>::type* = 0,
                    typename std::enable_if<!std::is_reference<_Type>::value>::type* = 0,
                    typename std::enable_if<!std::is_same<_Type, Variant>::value>::type* = 0);

    template <typename _Type>
    const typename VariantTypeSelector<_Type, _Types...>::type& get() const;

    inline uint8_t getValueType() const {
    	return valueType_;
    }

private:
    inline bool hasValue() const {
        return valueType_ < TypesTupleSize::value;
    }

    template<typename _U>
    void set( const _U& value, const bool clear);

    template<typename _U>
    void set( _U&& value, const bool clear);

    template<typename _FriendType>
    friend struct TypeWriter;
    template<typename ... _FriendTypes>
    friend struct AssignmentVisitor;
    template<typename _FriendType>
    friend struct TypeEqualsVisitor;
    template<typename ... _FriendTypes>
    friend struct PartialEqualsVisitor;
    template<typename ... _FriendTypes>
    friend struct InputStreamReadVisitor;

    uint8_t valueType_;
    typename std::aligned_storage<maxSize>::type valueStorage_;
};
} // namespace CommonAPI

#include "SerializableVariant.hpp"

#endif // COMMONAPI_SERIALIZABLE_VARIANT_H_