]>
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 "stringtools.hh"
38 #include "serializer.hh"
39 #include "deserializer.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
);
64 * To get a number value which may have been parsed as either an integer or
65 * double, use these instead.
68 bool getNumber(long&);
69 bool getNumber(double&);
71 virtual bool isNull();
76 class wrapper
: public serializable
80 wrapper(const T
& var
) : variable(var
) {}
82 void serialize(serializer
& out
) const;
83 void deserialize(deserializer
& in
);
92 class null
: public serializable
96 void serialize(serializer
& out
) const;
97 void deserialize(deserializer
& in
);
104 typedef wrapper
<long> wrapped_integer
;
105 typedef wrapper
<double> wrapped_real
;
106 typedef wrapper
<bool> wrapped_boolean
;
107 typedef wrapper
<std::string
> wrapped_string
;
108 typedef wrapper
<std::wstring
> wrapped_wstring
;
109 typedef wrapper
<std::vector
<serializable_ptr
> > wrapped_array
;
110 typedef wrapper
<std::map
<std::string
,serializable_ptr
> > wrapped_dictionary
;
115 inline void wrapper
<T
>::serialize(serializer
& out
) const
122 wrapper
<std::vector
<serializable_ptr
> >::serialize(serializer
& out
) const
126 std::vector
<serializable_ptr
>::const_iterator i
;
127 for (i
= variable
.begin(); i
< variable
.end(); i
++)
129 (*i
)->serialize(out
);
137 wrapper
<std::map
<std::string
,serializable_ptr
> >::serialize(serializer
& out
) const
141 std::map
<std::string
,serializable_ptr
>::const_iterator i
;
142 for (i
= variable
.begin(); i
!= variable
.end(); i
++)
144 out
.push((*i
).first
);
145 (*i
).second
->serialize(out
);
151 inline void null::serialize(serializer
& out
) const
158 inline void wrapper
<T
>::deserialize(deserializer
& in
)
164 inline void wrapper
<std::vector
<serializable_ptr
> >::deserialize(deserializer
& in
)
166 serializable_ptr obj
;
170 while (obj
= in
.deserialize())
172 variable
.push_back(serializable_ptr(obj
));
180 wrapper
<std::map
<std::string
,serializable_ptr
> >::deserialize(deserializer
& in
)
182 serializable_ptr obj
;
186 while (obj
= in
.deserialize())
191 variable
[key
] = in
.deserialize();
198 inline void null::deserialize(deserializer
& in
)
205 inline void wrapper
<T
>::print() const
207 std::cout
<< std::boolalpha
<< typeid(T
).name() << "(" << variable
<< ")";
211 inline void wrapper
<std::wstring
>::print() const
213 std::wcout
<< variable
;
217 inline void wrapper
<std::vector
<serializable_ptr
> >::print() const
219 std::cout
<< "array";
223 inline void wrapper
<std::map
<std::string
,serializable_ptr
> >::print() const
225 std::cout
<< "dictionary";
228 inline void null::print() const
235 inline bool wrapper
<T
>::get(T
& value
)
241 inline bool null::isNull()
249 #endif // _SERIALIZABLE_HH_
251 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.051127 seconds and 5 git commands to generate.