aboutsummaryrefslogtreecommitdiff
path: root/paludis/serialise-impl.hh
blob: 89baf3d589b42d035a413a4df46da04541850689 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PALUDIS_SERIALISE_IMPL_HH
#define PALUDIS_GUARD_PALUDIS_SERIALISE_IMPL_HH 1

#include <paludis/serialise.hh>
#include <paludis/util/remove_shared_ptr.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/options.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/package_id-fwd.hh>
#include <paludis/dep_spec-fwd.hh>
#include <tr1/type_traits>
#include <list>
#include <ostream>
#include <istream>

namespace paludis
{
    template <
        typename Flags_,
        typename Flag_>
    struct SerialiserFlagsInclude
    {
        static const bool value =
            std::tr1::is_same<typename Flags_::Flag1, Flag_>::value ||
            std::tr1::is_same<typename Flags_::Flag2, Flag_>::value ||
            std::tr1::is_same<typename Flags_::Flag3, Flag_>::value;
    };

    template <
        bool is_container_,
        bool might_be_null_,
        typename T_>
    struct SerialiserObjectWriterHandler;

    template <>
    struct PALUDIS_VISIBLE SerialiserObjectWriterHandler<false, false, bool>
    {
        static void write(Serialiser & s, const bool t);
    };

    template <>
    struct PALUDIS_VISIBLE SerialiserObjectWriterHandler<false, false, int>
    {
        static void write(Serialiser & s, const int t);
    };

    template <>
    struct PALUDIS_VISIBLE SerialiserObjectWriterHandler<false, false, std::string>
    {
        static void write(Serialiser & s, const std::string & t);
    };

    template <>
    struct PALUDIS_VISIBLE SerialiserObjectWriterHandler<false, false, const PackageID>
    {
        static void write(Serialiser & s, const PackageID & t);
    };

    template <
        typename T_>
    struct SerialiserObjectWriterHandler<false, false, Options<T_> >
    {
        static void write(Serialiser & s, const Options<T_> & t)
        {
            std::stringstream ss;
            for (T_ i(static_cast<T_>(0)), i_end(t.highest_bit()) ;
                    i != i_end ; i = static_cast<T_>(static_cast<int>(i) + 1))
            {
                if (! t[i])
                    continue;
                if (! ss.str().empty())
                    ss << ",";
                ss << i;
            }

            s.raw_stream() << "\"";
            s.escape_write(ss.str());
            s.raw_stream() << "\";";
        }
    };

    template <
        typename T_>
    struct SerialiserObjectWriterHandler<false, false, const T_>
    {
        static void write(Serialiser & s, const T_ & t)
        {
            SerialiserObjectWriterHandler<false, false, T_>::write(s, t);
        }
    };

    template <
        typename T_>
    struct SerialiserObjectWriterHandler<false, false, T_>
    {
        static void write(Serialiser & s, const T_ & t)
        {
            t.serialise(s);
        }
    };

    template <
        bool is_container_,
        typename T_>
    struct SerialiserObjectWriterHandler<is_container_, true, T_>
    {
        static void write(Serialiser & s, const T_ & t)
        {
            if (t)
                SerialiserObjectWriterHandler<is_container_, false, typename RemoveSharedPtr<T_>::Type>::write(
                        s, *t);
            else
                s.raw_stream() << "null;";
        }
    };

    template <typename T_>
    struct SerialiserConstIteratorType
    {
        typedef typename T_::ConstIterator Type;
    };

    template <typename T_>
    struct SerialiserConstIteratorType<std::list<T_> >
    {
        typedef typename std::list<T_>::const_iterator Type;
    };

    template <
        typename T_>
    struct SerialiserObjectWriterHandler<true, false, T_>
    {
        static void write(Serialiser & s, const T_ & t)
        {
            s.raw_stream() << "c(";
            unsigned n(0);
            for (typename SerialiserConstIteratorType<T_>::Type i(t.begin()), i_end(t.end()) ;
                    i != i_end ; ++i)
            {
                typedef typename std::iterator_traits<
                    typename SerialiserConstIteratorType<T_>::Type>::value_type ItemValueType;
                typedef typename std::tr1::remove_reference<ItemValueType>::type ItemType;

                s.raw_stream() << ++n << "=";
                SerialiserObjectWriterHandler<
                    false,
                    ! std::tr1::is_same<ItemType, typename RemoveSharedPtr<ItemType>::Type>::value,
                    ItemType
                        >::write(s, *i);
            }

            s.raw_stream() << "count=";
            SerialiserObjectWriterHandler<false, false, int>::write(s, n);

            s.raw_stream() << ");";
        }
    };

    template <
        typename Flags_,
        typename T_>
    SerialiserObjectWriter &
    SerialiserObjectWriter::member(
            const Flags_ &,
            const std::string & item_name,
            const T_ & t)
    {
        _serialiser.raw_stream() << item_name << "=";

        SerialiserObjectWriterHandler<
            SerialiserFlagsInclude<Flags_, serialise::container>::value,
            SerialiserFlagsInclude<Flags_, serialise::might_be_null>::value,
            T_
            >::write(_serialiser, t);

        return *this;
    }

    template <typename T_>
    struct DeserialisatorHandler;

    template <>
    struct DeserialisatorHandler<bool>
    {
        static bool handle(Deserialisation & v)
        {
            return destringify<bool>(v.string_value());
        }
    };

    template <>
    struct DeserialisatorHandler<int>
    {
        static int handle(Deserialisation & v)
        {
            return destringify<int>(v.string_value());
        }
    };

    template <>
    struct DeserialisatorHandler<std::string>
    {
        static std::string handle(Deserialisation & v)
        {
            return v.string_value();
        }
    };

    template <>
    struct PALUDIS_VISIBLE DeserialisatorHandler<std::tr1::shared_ptr<const PackageID> >
    {
        static std::tr1::shared_ptr<const PackageID> handle(Deserialisation & v);
    };

    template <typename T_>
    struct PALUDIS_VISIBLE DeserialisatorHandler<std::tr1::shared_ptr<T_> >
    {
        static std::tr1::shared_ptr<T_> handle(Deserialisation & v)
        {
            if (v.null())
                return make_null_shared_ptr();
            else
                return T_::deserialise(v);
        }
    };

    template <typename T_>
    struct DeserialisatorHandler<Options<T_> >
    {
        static Options<T_> handle(Deserialisation & v)
        {
            Options<T_> result;
            std::list<std::string> tokens;
            tokenise<delim_kind::AnyOfTag, delim_mode::BoundaryTag>(v.string_value(),
                    ",", "", std::back_inserter(tokens));

            for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                    t != t_end ; ++t)
                result += destringify<T_>(*t);

            return result;
        }
    };

    template <typename T_>
    struct DeserialisatorHandler
    {
        static T_ handle(Deserialisation & v)
        {
            return T_::deserialise(v);
        }
    };

    template <typename T_>
    T_
    Deserialisator::member(const std::string & key_name)
    {
        return DeserialisatorHandler<T_>::handle(*find_remove_member(key_name));
    }

    template <typename T_>
    std::tr1::shared_ptr<T_> deserialise(
            const Environment * const env,
            const std::string & str,
            const std::string & class_name)
    {
        Deserialiser d(env, str);
        Deserialisation dd(class_name, d);
        return T_::deserialise(dd);
    }
}

#endif