#include <micro_api.hpp>
Public Types | |
typedef SerializableType | serializable_type |
The base Serializable interface associated with this object. | |
Public Member Functions | |
micro_api (const std::string &serclass) | |
Constructs an object with the given serializer_class(). | |
~micro_api () | |
Does nothing. | |
micro_api () | |
Constructs an object with the same serializer_class() as s11nlite::serializer_class(). | |
std::string | serializer_class () const |
Returns the current serializer class name. | |
void | serializer_class (const std::string &s) |
Sets the current serializer class name. | |
bool | buffer (const serializable_type &src) |
Serializes src to an internal buffer, which may be fetched and cleared with buffer() and clear_buffer(), respectively. | |
std::string | buffer () const |
Returns this object's buffered data, if any has been set using buffer(serializable_type). | |
void | clear_buffer () |
Clears any buffered data saved using buffer(serializable_type). | |
bool | save (const serializable_type &src, const std::string &dest) const |
Saves src to dest, returning true on success and false on error. | |
bool | save (const serializable_type &src, std::ostream &dest) const |
Overload taking an ostream. | |
serializable_type * | load (const std::string &src) const |
Loads a serializable_type from src, returning 0 on failure and a valid pointer on success (which the caller owns). | |
serializable_type * | load (std::istream &src) const |
An overload which takes an istream instead of a string. | |
serializable_type * | load_buffer () const |
Loads a Serializable from the buffer() content, if any. |
It is intended to be used for saving and loading sets of like-typed Serializables. For apps which do lots of loading and saving of homogeonous Serializables, micro_api can cut down on the amount of typing (intentionally ambiguous) needed:
s11nlite::micro_api<MyT> micro; MyT my; ... populate my ... micro.save( my, "somefile" ); ... MyT * your = micro.load( "somefile" );
Unlike the s11nlite interface, micro_api explicitely hides all node-level details. It does allow the user to set an output format (serializer class), as it may be desirable to save different collections of objects in different formats. For loading it uses s11nlite, so it supports any formats supported there.
Templatized on:
Any functions in this class might throw if their s11n[lite] counterparts do.
Multi-threading:
Using a single micro_api instance from multiple threads is not encouraged, but it just might work. Even so, some operations must be locked from the client side to avoid undesired racing, such as operation sequences like (micro.buffer(Object); string s=micro.buffer()) to ensure that the buffer is not hammered by a parallel call to micro.buffer(XXX) just before (s=micro.buffer()) is called.
Definition at line 55 of file micro_api.hpp.
typedef SerializableType s11nlite::micro_api< SerializableType >::serializable_type |
The base Serializable interface associated with this object.
Definition at line 62 of file micro_api.hpp.
s11nlite::micro_api< SerializableType >::micro_api | ( | const std::string & | serclass | ) | [inline, explicit] |
Constructs an object with the given serializer_class().
Definition at line 93 of file micro_api.hpp.
s11nlite::micro_api< SerializableType >::~micro_api | ( | ) | [inline] |
s11nlite::micro_api< SerializableType >::micro_api | ( | ) | [inline] |
Constructs an object with the same serializer_class() as s11nlite::serializer_class().
Definition at line 107 of file micro_api.hpp.
std::string s11nlite::micro_api< SerializableType >::serializer_class | ( | ) | const [inline] |
void s11nlite::micro_api< SerializableType >::serializer_class | ( | const std::string & | s | ) | [inline] |
bool s11nlite::micro_api< SerializableType >::buffer | ( | const serializable_type & | src | ) | [inline] |
Serializes src to an internal buffer, which may be fetched and cleared with buffer() and clear_buffer(), respectively.
Definition at line 134 of file micro_api.hpp.
std::string s11nlite::micro_api< SerializableType >::buffer | ( | ) | const [inline] |
Returns this object's buffered data, if any has been set using buffer(serializable_type).
The buffer can be deserialized by wrapping it in an istringstream.
Note that only Serializers which can write to streams can be used here (e.g., not sqlite3_serializer).
Definition at line 154 of file micro_api.hpp.
void s11nlite::micro_api< SerializableType >::clear_buffer | ( | ) | [inline] |
Clears any buffered data saved using buffer(serializable_type).
Definition at line 165 of file micro_api.hpp.
bool s11nlite::micro_api< SerializableType >::save | ( | const serializable_type & | src, | |
const std::string & | dest | |||
) | const [inline] |
Saves src to dest, returning true on success and false on error.
If the underlying call(s) to serialize throw then this function pass on the exception.
Note that dest does not get parsed to see if it's a URL. Instead it goes directly to the Serializer. This distinction is important for Serializers which treat filenames and streams differently (e.g., mysql_serializer and sqlite3_serializer). If you want to get the built-in URL support for your filename strings, use s11n::io::get_ostream(). Likewise, for loading, use s11n::io::get_istream().
Definition at line 187 of file micro_api.hpp.
bool s11nlite::micro_api< SerializableType >::save | ( | const serializable_type & | src, | |
std::ostream & | dest | |||
) | const [inline] |
serializable_type* s11nlite::micro_api< SerializableType >::load | ( | const std::string & | src | ) | const [inline] |
Loads a serializable_type from src, returning 0 on failure and a valid pointer on success (which the caller owns).
If the underlying call(s) to serialize throw then this function passes on the exception. In that case the object which was allocated for the process (if any) is destroyed, cleaned up by the s11n_traits::cleanup_functor mechanism, which is believed to be relatively safe from memory leaks.
Definition at line 214 of file micro_api.hpp.
serializable_type* s11nlite::micro_api< SerializableType >::load | ( | std::istream & | src | ) | const [inline] |
An overload which takes an istream instead of a string.
Definition at line 222 of file micro_api.hpp.
serializable_type* s11nlite::micro_api< SerializableType >::load_buffer | ( | ) | const [inline] |
Loads a Serializable from the buffer() content, if any.
The caller owns the returned pointer, which may be 0.
Definition at line 231 of file micro_api.hpp.