]>
Dogcows Code - chaz/yoink/blob - src/serializable.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #ifndef _SERIALIZABLE_HH_
30 #define _SERIALIZABLE_HH_
37 #include "serializer.hh"
38 #include "deserializer.hh"
39 #include "stringtools.hh"
48 virtual ~serializable();
50 virtual void serialize(serializer
& out
) const = 0;
51 virtual void deserialize(deserializer
& in
) = 0;
53 virtual void print() const = 0;
55 virtual bool get(long& value
);
56 virtual bool get(double& value
);
57 virtual bool get(bool& value
);
58 virtual bool get(std::string
& value
);
59 virtual bool get(std::wstring
& value
);
60 virtual bool get(std::vector
<serializable_ptr
>& value
);
61 virtual bool get(std::map
<std::string
,serializable_ptr
>& value
);
62 virtual bool isNull();
67 class wrapper
: public serializable
71 wrapper(const T
& var
) : variable(var
) {}
73 void serialize(serializer
& out
) const;
74 void deserialize(deserializer
& in
);
84 class null
: public serializable
88 void serialize(serializer
& out
) const;
89 void deserialize(deserializer
& in
);
96 typedef wrapper
<long> wrapped_integer
;
97 typedef wrapper
<double> wrapped_real
;
98 typedef wrapper
<bool> wrapped_boolean
;
99 typedef wrapper
<std::string
> wrapped_string
;
100 typedef wrapper
<std::wstring
> wrapped_wstring
;
101 typedef wrapper
<std::vector
<serializable_ptr
> > wrapped_array
;
102 typedef wrapper
<std::map
<std::string
,serializable_ptr
> > wrapped_dictionary
;
107 inline void wrapper
<T
>::serialize(serializer
& out
) const
114 wrapper
<std::vector
<serializable_ptr
> >::serialize(serializer
& out
) const
118 std::vector
<serializable_ptr
>::const_iterator i
;
119 for (i
= variable
.begin(); i
< variable
.end(); i
++)
121 (*i
)->serialize(out
);
129 wrapper
<std::map
<std::string
,serializable_ptr
> >::serialize(serializer
& out
) const
133 std::map
<std::string
,serializable_ptr
>::const_iterator i
;
134 for (i
= variable
.begin(); i
!= variable
.end(); i
++)
136 out
.push((*i
).first
);
137 (*i
).second
->serialize(out
);
143 inline void null::serialize(serializer
& out
) const
150 inline void wrapper
<T
>::deserialize(deserializer
& in
)
156 inline void wrapper
<std::vector
<serializable_ptr
> >::deserialize(deserializer
& in
)
158 serializable_ptr obj
;
162 while (obj
= in
.deserialize())
164 variable
.push_back(serializable_ptr(obj
));
172 wrapper
<std::map
<std::string
,serializable_ptr
> >::deserialize(deserializer
& in
)
174 serializable_ptr obj
;
178 while (obj
= in
.deserialize())
183 variable
[key
] = in
.deserialize();
190 inline void null::deserialize(deserializer
& in
)
197 inline void wrapper
<T
>::print() const
199 std::cout
<< std::boolalpha
<< typeid(T
).name() << "(" << variable
<< ")";
203 inline void wrapper
<std::wstring
>::print() const
205 std::wcout
<< variable
;
209 inline void wrapper
<std::vector
<serializable_ptr
> >::print() const
211 std::cout
<< "array";
215 inline void wrapper
<std::map
<std::string
,serializable_ptr
> >::print() const
217 std::cout
<< "dictionary";
220 inline void null::print() const
227 inline bool wrapper
<T
>::get(T
& value
)
233 inline bool null::isNull()
241 #endif // _SERIALIZABLE_HH_
This page took 0.049333 seconds and 5 git commands to generate.