GwySerializable — Abstract interface for serializable objects
GByteArray * | (*GwySerializeFunc) () |
GObject * | (*GwyDeserializeFunc) () |
GByteArray * | gwy_serializable_serialize () |
GObject * | gwy_serializable_deserialize () |
GObject * | gwy_serializable_duplicate () |
void | gwy_serializable_clone () |
void | gwy_serializable_clone_with_type () |
gsize | gwy_serializable_get_size () |
gsize | gwy_serialize_get_struct_size () |
gsize | gwy_serialize_get_items_size () |
GByteArray * | gwy_serialize_pack_object_struct () |
gboolean | gwy_serialize_unpack_object_struct () |
gsize | gwy_serialize_check_string () |
GByteArray * | gwy_serialize_object_items () |
GwySerializeItem * | gwy_deserialize_object_hash () |
#define | GWY_SERIALIZABLE_GET_IFACE() |
GwySerializeSpec | |
GwySerializeValue | |
GwySerializeItem | |
GwySerializable | |
struct | GwySerializableIface |
GInterface ╰── GwySerializable
GwySerializable requires GObject.
GwySerializable is implemented by GwyContainer, GwySIUnit and GwyStringList.
#include <libgwyddion/gwyddion.h>
GwySerializable is an abstract interface for data-like object that can be
serialized and deserialized. You can serialize any object implementing this
interface with gwy_serializable_serialize()
and the restore (deserialize) it
with gwy_serializable_deserialize()
. It is also posible it duplicate any
such object with gwy_serializable_duplicate()
and transform one object to
copy of another with gwy_serializable_clone()
.
Note all GwySerializable methods work with object `value', whatever it means
for a particular type. Beside that, objects also have their identity which
is unique and can't be subject of cloning. It consists of properties like
memory address or references held by object users. Two important properties
related to identity (and thus NOT retained) are: signals connected to
a particular object and user data set with g_object_set_data()
or
g_object_set_qdata()
.
You can implement serialization and deserialization in your classes with
gwy_serialize_pack_object_struct()
and gwy_serialize_unpack_object_struct()
or with gwy_serialize_object_items()
and gwy_deserialize_object_hash()
.
The former two are useful for struct-like objects (most objects are of this
kind), the latter two for hash-like objects, i.e., objects that can contain
components of arbitrary name and type. Serialized size calculations can be
in most cases performed by gwy_serialize_get_struct_size()
and
gwy_serialize_get_items_size()
helper functions.
GByteArray * (*GwySerializeFunc) (GObject *serializable
,GByteArray *buffer
);
The type of serialization method, see gwy_serializable_serialize()
for
description.
serializable |
An object to serialize. |
|
buffer |
A buffer to append the representation to, may be |
buffer
with serialized object appended.
GObject * (*GwyDeserializeFunc) (const guchar *buffer
,gsize size
,gsize *position
);
The type of deserialization method, see gwy_serializable_deserialize()
for
description.
buffer |
A buffer containing a serialized object. |
|
size |
The size of |
|
position |
The current position in |
A newly created (restored) object.
GByteArray * gwy_serializable_serialize (GObject *serializable
,GByteArray *buffer
);
Serializes an object to byte buffer.
This is a high-level method. Do not use it for implementation of child object serialization (should you ever need to do it manually), it would lead to repeated required buffer size calculations.
serializable |
A GObject that implements GwySerializable interface. |
|
buffer |
A buffer to which the serialized object should be appended,
or |
buffer
or a newly allocated GByteArray with serialized
object appended.
GObject * gwy_serializable_deserialize (const guchar *buffer
,gsize size
,gsize *position
);
Restores a serialized object from byte buffer.
The newly created object has reference count according to its nature, thus a GtkObject will have a floating reference, a GObject will have a refcount of 1, etc.
buffer |
A block of memory of size |
|
size |
The size of |
|
position |
The position of the object in |
A newly created object.
GObject *
gwy_serializable_duplicate (GObject *object
);
Creates a copy of an object.
If the object doesn't support duplication natively, it's brute-force serialized and then deserialized, this may be quite inefficient, namely for large objects.
You can duplicate a NULL
, too, but you are discouraged from doing it.
object |
An object implementing GwySerializable interface. |
The newly created object copy. However if the object is a
singleton, object
itself (with incremented reference count)
can be returned, too.
void gwy_serializable_clone (GObject *source
,GObject *copy
);
Makes an object identical to another object of the same type.
More precisely, source
may be subclass of copy
(the extra information
is lost then).
source |
An object implementing GwySerializable interface. |
|
copy |
An object of the same type as |
void gwy_serializable_clone_with_type (GObject *source
,GObject *copy
,GType type
);
Makes an object identical to another object of the same type, with explicit type checking.
This function is the same as gwy_serializable_clone()
, except it asserts
the object type, which must be type
.
Since 2.52
source |
An object implementing GwySerializable interface. |
|
copy |
An object of the same type as |
|
type |
The type the objects are asserted to be. |
gsize
gwy_serializable_get_size (GObject *serializable
);
Calculates the expected size of serialized object.
serializable |
A GObject that implements GwySerializable interface. |
The expected size of serialized serializable
.
gsize gwy_serialize_get_struct_size (const guchar *object_name
,gsize nspec
,const GwySerializeSpec *spec
);
Calculates serialized object size for struct-like objects.
The component specification is the same as in
gwy_serialize_pack_object_struct()
.
object_name |
The type name of the object. |
|
nspec |
The number of items in |
|
spec |
The components to serialize. |
Serialized object size, it included space for object name and size. The value is exact unless some components are objects that do not return exact size estimate themselves.
gsize gwy_serialize_get_items_size (const guchar *object_name
,gsize nitems
,const GwySerializeItem *items
);
Calculates serialized object size for hash-like objects.
The component specification is the same as in
gwy_serialize_object_items()
.
object_name |
The type name of the object. |
|
nitems |
The number of |
|
items |
The components to serialize. |
Serialized object size, it included space for object name and size. The value is exact unless some components are objects that do not return exact size estimate themselves.
GByteArray * gwy_serialize_pack_object_struct (GByteArray *buffer
,const guchar *object_name
,gsize nspec
,const GwySerializeSpec *spec
);
Appends serialization of object with g_type_name()
object_name
and
components described by spec
to buffer
in gwy-file format.
Here's how a serialization method of a simple object whose state is described by a single real number foo could look (without error checking):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static guchar* my_object_serialize(GObject *obj, guchar *buffer, gsize *size) { MyObject *my_object = MY_OBJECT(obj); GwySerializeSpec spec[] = { { 'd', "foo", &my_object->foo, NULL, }, }; return gwy_serialize_pack_object_struct(buffer, size, "MyObject", G_N_ELEMENTS(spec), spec); } |
buffer |
A buffer to which the serialized components should be appended. |
|
object_name |
The type name of the object. |
|
nspec |
The number of items in |
|
spec |
The components to serialize. |
The buffer with serialization of spec
components appended.
gboolean gwy_serialize_unpack_object_struct (const guchar *buffer
,gsize size
,gsize *position
,const guchar *object_name
,gsize nspec
,GwySerializeSpec *spec
);
Deserializes an object with named components packed into gwy-file format by
gwy_serialize_pack_object_struct()
.
Extra components are ignored (but cause a warning), components of different type than expected cause failure, missing components are not detected.
It is safe to pass pointers to existing non-atomic objects (strings, arrays,
objects) in spec
values, they will be dereferenced and freed as necessary
when an unpacked value is about to replace them.
For the same reason it is an error to pass pointers to unintialized memory
there, always initialize non-atomic spec
values to NULL
pointers, at
least.
Caller is responsible for use/clean-up of these values if deserialization succeeds or not.
Here's how a deserialization method of a simple object whose state is
described by a single real number foo
could look (without error checking):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static GObject* my_object_deserialize(const guchar *buffer, gsize size, gsize *position) { double foo = 1.0; GwySerializeSpec spec[] = { { 'd', "foo", &foo, NULL, }, }; MyObject *my_object; gwy_serialize_unpack_object_struct(buffer, size, position, "MyObject", G_N_ELEMENTS(spec), spec); return my_object_new(foo); } |
buffer |
A memory location containing a serialized object at position
|
|
size |
Current size of |
|
position |
The position of the object in |
|
object_name |
The type name of the object. |
|
nspec |
The number of items in |
|
spec |
The components to deserialize. |
Whether the unpacking succeeded (see description body for definition of success and failure).
gsize gwy_serialize_check_string (const guchar *buffer
,gsize size
,gsize position
,const guchar *compare_to
);
Check whether size
bytes of memory in buffer
can be interpreted as a
NUL-terminated string, and eventually whether it's equal to compare_to
.
When compare_to
is NULL
, the comparsion is not performed.
buffer |
A memory location containing a NUL-terminated string at position
|
|
size |
The size of |
|
position |
The position of the string in |
|
compare_to |
String to compare |
The length of the NUL-terminated string including the NUL character; zero otherwise.
GByteArray * gwy_serialize_object_items (GByteArray *buffer
,const guchar *object_name
,gsize nitems
,const GwySerializeItem *items
);
Serializes an object to buffer in gwy-file format.
More precisely, it appends serialization of object with g_type_name()
object_name
with components described by items
to buffer
.
buffer |
A buffer to which the serialized components should be appended,
or |
|
object_name |
The type of the object. |
|
nitems |
The number of |
|
items |
The components to serialize. |
buffer
or a newly allocated GByteArray with serialization of
items
components appended.
GwySerializeItem * gwy_deserialize_object_hash (const guchar *buffer
,gsize size
,gsize *position
,const guchar *object_name
,gsize *nitems
);
Deserializes an object with arbitrary components from gwy-file format.
This function works like gwy_serialize_unpack_object_struct()
, except that
it does not use any a priori knowledge of what the object contains. So
instead of filling values in supplied GwySerializeSpec's, it constructs
GwySerializeItem's completely from what is found in buffer
. It does
considerably less sanity checks and even allows several components of the
same name.
buffer |
A block of memory of size |
|
size |
The size of |
|
position |
Current position in buffer, will be updated to point after object. |
|
object_name |
The type name of the object. |
|
nitems |
Where the number of deserialized components should be stored. |
A newly allocated array of deserialized components. Note the
name
fields of GwySerializeSpec's point to buffer
and thus are
valid only as long as buffer
is; any arrays or strings are newly
allocated and must be reused or freed by caller.
#define GWY_SERIALIZABLE_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE((obj), GWY_TYPE_SERIALIZABLE, GwySerializableIface))
typedef struct { guchar ctype; const guchar *name; gpointer value; guint32 *array_size; } GwySerializeSpec;
A structure containing information for one object/struct component serialization or deserialization.
This component information is used in gwy_serialize_pack_object_struct()
and gwy_serialize_unpack_object_struct()
suitable for (de)serialization
of struct-like objects.
Following atomic component types (ctype
's) exist:
'b'
for a boolean,
'c'
for a character,
'i'
for a 32bit integer,
'q'
for a 64bit integer,
'd'
for a gdouble,
's'
for a NUL-terminated string,
'o'
for a serializable object.
And array component types:
'C'
for a character array,
'I'
for a 32bit integer array,
'Q'
for a 64bit integer array,
'D'
for a gdouble array,
'S'
for an array of NUL-terminated strings,
'O'
for an array of objects.
Component type, see description body for possible values. |
||
Component name as a NUL-terminated string. |
||
Pointer to component (always add one level of indirection; for
an object, a GObject** pointer should be stored). If it is
|
||
Pointer to array size if component is an array, |
Helper serialization type that can hold any atomic value or pointer to non-atomic value.
Boolean. |
||
Character. |
||
32bit integer. |
||
64bit integer. |
||
Double-precision float. |
||
NUL-terminated string. |
||
Object (serializable). |
||
Array of booleans. Not to be used. |
||
Array of characters. |
||
Array of 32bit integers. |
||
Array of 64bit integers. |
||
Array of double-precision floats. |
||
Array of NUL-terminated strings. |
||
Array of objects (serializable). |
typedef struct { guchar ctype; const guchar *name; GwySerializeValue value; guint32 array_size; } GwySerializeItem;
A structure containing information for one object/struct component serialization or deserialization.
This component information is used in gwy_serialize_object_items()
and
gwy_deserialize_object_hash()
suitable for (de)serialization of hash-like
objects.
Component type, see GwySerializeSpec for details. |
||
Component name as a NUL-terminated string. |
||
GwySerializeValue |
Component value. |
|
Array size if component is an array, unused otherwise. |
typedef struct _GwySerializable GwySerializable;
struct GwySerializableIface { GwySerializeFunc serialize; GwyDeserializeFunc deserialize; void (*clone)(GObject *source, GObject *copy); GObject* (*duplicate)(GObject *object); gsize (*get_size)(GObject *object); };
The methods a serializable objects has to implement.
GwySerializeFunc |
Serialization method (obligatory), see GwySerializeFunc for description. |
|
GwyDeserializeFunc |
Restore method (obligatory), see GwyDeserializeFunc for description. |
|
Clone method (obligatory). Copies complete object `value' to an existing object of the same type. This method is called from copy's class if source and copy classes differ. |
||
Duplication method (optional). Creates a duplicate of an object. When it is absent, duplication is performed by a rather inefficient serialize-and-deserialize fallback. |
||
Serialized size calculation method (optional). Calculates expected serialized object size (including object name and size header). Its purpose is to avoid frequent memory reallocations during serialization of large objects. The returned value may be inexact, a reasonable upper bound is sufficient. When it is absent, serialization is less efficient. |