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
|
/* Gobby - GTK-based collaborative text editor
* Copyright (C) 2008-2014 Armin Burgmeier <armin@arbur.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _GOBBY_SERIALIZE_HPP_
#define _GOBBY_SERIALIZE_HPP_
#include <string>
#include <sstream>
#include <stdexcept>
namespace Gobby
{
/** Generic stuff to de/serialize data types to/from strings.
*/
namespace serialize
{
/** Error that is thrown if conversion from a string fails. For example, if
* "t3" should be converted to int.
*/
class conversion_error: public std::runtime_error
{
public:
conversion_error(const std::string& message);
};
/** @brief Several built-in type names.
*/
template<typename data_type> struct type_name {};
template<> struct type_name<int> { static const char* name; };
template<> struct type_name<long> { static const char* name; };
template<> struct type_name<short> { static const char* name; };
template<> struct type_name<char> { static const char* name; };
template<> struct type_name<unsigned int> { static const char* name; };
template<> struct type_name<unsigned long> { static const char* name; };
template<> struct type_name<unsigned short> { static const char* name; };
template<> struct type_name<unsigned char> { static const char* name; };
template<> struct type_name<float> { static const char* name; };
template<> struct type_name<double> { static const char* name; };
template<> struct type_name<long double> { static const char* name; };
template<> struct type_name<bool> { static const char* name; };
/** Abstract base context type to convert something to a string.
*/
template<typename data_type>
class context_base_to
{
public:
virtual ~context_base_to() {}
/** @brief Converts the given data type to a string.
*/
virtual std::string to_string(const data_type& from) const = 0;
};
/** Abstract base context type to convert a string to another type.
*/
template<typename data_type>
class context_base_from
{
public:
virtual ~context_base_from() {}
/** @brief Converts a string to a data type. Might throw
* serialize::conversion_error.
*/
virtual data_type from_string(const std::string& from) const = 0;
};
/** Default context to convert something literally to a string.
*/
template<typename data_type>
class default_context_to: public context_base_to<data_type>
{
public:
/** @brief Converts the given data type to a string.
*/
virtual std::string to_string(const data_type& from) const;
protected:
/** Method derived classes may overload to alter the conversion.
*/
virtual void on_stream_setup(std::stringstream& stream) const;
};
/** Default context to convert a string literally to a type.
*/
template<typename data_type>
class default_context_from: public context_base_from<data_type>
{
public:
/** @brief Converts the given string to the type specified
* as template parameter.
*
* May throw serialize::conversion_error().
*/
virtual data_type from_string(const std::string& from) const;
protected:
/** Method derived classes may overload to alter the conversion.
*/
virtual void on_stream_setup(std::stringstream& stream) const;
};
#if 0
/** Context that uses hexadecimal representation for numerical types.
*/
template<typename data_type>
class hex_context_to: public default_context_to<data_type>
{
protected:
virtual void on_stream_setup(std::stringstream& stream) const;
};
/** Context that uses hexadecimal representation for numerical types.
*/
template<typename data_type>
class hex_context_from: public default_context_from<data_type>
{
public:
virtual void on_stream_setup(std::stringstream& stream) const;
};
#endif
template<>
class default_context_to<std::string>: public context_base_to<std::string>
{
public:
typedef std::string data_type;
virtual std::string to_string(const data_type& from) const;
};
template<>
class default_context_from<std::string>: public context_base_from<std::string>
{
public:
typedef std::string data_type;
virtual data_type from_string(const std::string& from) const;
};
template<>
class default_context_to<const char*>: public context_base_to<const char*>
{
public:
typedef const char* data_type;
virtual std::string to_string(const data_type& from) const;
};
template<std::size_t N>
class default_context_to<char[N]>: public context_base_to<char[N]>
{
public:
typedef char data_type[N];
virtual std::string to_string(const data_type& from) const;
};
/** A serialized object.
*/
class data
{
public:
/** Uses the given string as serialized data without converting it.
*/
data(const std::string& serialized);
/** Serialises the given object with the given context. A default
* context is used if no one is given.
*/
template<typename type>
data(const type& data,
const context_base_to<type>& ctx = default_context_to<type>());
/** Returns the serialized data.
*/
const std::string& serialized() const;
/** Deserializes the object with the given context. A default context
* is used of no one is given.
*/
template<typename type>
type as(const context_base_from<type>& ctx =
default_context_from<type>()) const;
protected:
std::string m_serialized;
};
template<typename data_type>
std::string default_context_to<data_type>::
to_string(const data_type& from) const
{
std::stringstream stream;
on_stream_setup(stream);
stream << from;
return stream.str();
}
template<typename data_type>
data_type default_context_from<data_type>::
from_string(const std::string& from) const
{
std::stringstream stream(from);
on_stream_setup(stream);
data_type data;
stream >> data;
if(stream.bad() )
{
throw conversion_error(
"Could not convert \"" + from + "\" to " +
type_name<data_type>::name
);
}
return data;
}
template<typename data_type>
void default_context_to<data_type>::
on_stream_setup(std::stringstream& stream) const
{
}
template<typename data_type>
void default_context_from<data_type>::
on_stream_setup(std::stringstream& stream) const
{
}
#if 0
template<typename data_type>
void hex_context_to<data_type>::
on_stream_setup(std::stringstream& stream) const
{
stream << std::hex;
}
template<typename data_type>
void hex_context_from<data_type>::
on_stream_setup(std::stringstream& stream) const
{
stream >> std::hex;
}
#endif
template<typename data_type>
data::data(const data_type& data, const context_base_to<data_type>& ctx):
m_serialized(ctx.to_string(data) )
{
}
template<typename data_type>
data_type data::as(const context_base_from<data_type>& ctx) const
{
return ctx.from_string(m_serialized);
}
template<size_t N>
std::string default_context_to<char[N]>::
to_string(const data_type& from) const
{
return from;
}
} // namespace serialize
} // namespace Gobby
#endif // _GOBBY_SERIALIZE_HPP_
|