/usr/include/net6/serialise.hpp is in libnet6-1.3-dev 1:1.3.14-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | /* net6 - Library providing IPv4/IPv6 network access
* Copyright (C) 2005, 2006 Armin Burgmeier / 0x539 dev group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _NET6_SERIALISE_HPP_
#define _NET6_SERIALISE_HPP_
#include <string>
#include <sstream>
#include <stdexcept>
/** Generic stuff to de/serialise data types to/from strings.
*/
namespace serialise
{
/** 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
* serialise::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 serialise::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;
};
/** 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;
};
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;
};
#if 0
/** String serialisation does not need any conversions.
*/
template<>
class context<std::string>: public context_base<std::string>
{
public:
typedef std::string data_type;
virtual std::string to_string(const data_type& from) const;
virtual data_type from_string(const std::string& string) const;
};
/** const char* serialisation is only supported in one direction
* (const char* -> string). If you want to get certain data as const char*,
* get it as std::string and call c_str() on it.
*/
template<>
class context<const char*>: public context_base<const char*>
{
public:
typedef const char* data_type;
virtual std::string to_string(const data_type& from) const;
/** This method must not be instanciated. Use context<std::string>
* instead!
*/
virtual data_type from_string(const std::string& from) const;
};
/** char array serialisation is only supported in one direction
* (char[N] -> string). If you want to get certain data as a char array, get
* it as std::string and call c_str() on it.
*/
template<size_t N>
class context<char[N]>: public context_base<char[N]>
{
public:
typedef const char data_type[N];
virtual std::string to_string(const data_type& from) const;
/** This method must not be instanciated. Use context<std::string>
* instead!
*/
virtual data_type from_string(const std::string& from) const;
};
#endif
/** A serialised object.
*/
class data
{
public:
/** Uses the given string as serialised data without converting it.
*/
data(const std::string& serialised);
/** 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 serialised data.
*/
const std::string& serialised() const;
/** Deserialises 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_serialised;
};
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
{
}
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;
}
template<typename data_type>
data::data(const data_type& data, const context_base_to<data_type>& ctx):
m_serialised(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_serialised);
}
template<size_t N>
std::string default_context_to<char[N]>::
to_string(const data_type& from) const
{
return from;
}
} // namespace serialise
#endif // _NET6_SERIALISE_HPP_
|