1 #ifndef _OBJECT_STREAM_
2 #define _OBJECT_STREAM_
4 #include "libs/pointer.h"
12 * @brief Objects collector
14 * @details This class allows object_stream to store or to get
15 * objects, depending on their type and serialization method.
17 class object_collector
{
19 /** Add plain array of bytes
20 * @param buf address of the array
21 * @param size size of array in bytes
23 virtual void get_int(char *buf
, unsigned int size
) = 0;
25 /** Get plain array of bytes
26 * @param buf address of the array
27 * @param size size of array in bytes
29 virtual void add_int(char *buf
, unsigned int size
) = 0;
31 /** Get obj_ref at specified address
32 * @param addr place in memory where obj_ref is expected
35 virtual void get_ref(obj_ref
*addr
) = 0;
38 * @return address of obj_ref
40 virtual obj_ref
*get_ref() = 0;
43 * @param addr addres of obj_ref
45 virtual void add_ref(obj_ref
*addr
) = 0;
47 /** Get obj_val at specified address
48 * @param addr place in memory where obj_val is expected
51 virtual void get_val(obj_val
*addr
) = 0;
54 * @return address of obj_val
56 virtual obj_val
*get_val() = 0;
59 * @param addr addres of obj_val
61 virtual void add_val(obj_val
*addr
) = 0;
63 /** Set size of the data structure */
64 virtual void set_size(int size
) = 0;
68 * @brief Stream of objects
70 * @details Instances of this class are used to provide obj_vals
71 * with a possibility to manually deserialize themselves. In
72 * fact object_stream is a wrapper of object_collector class
73 * which provides more low-level interface.
77 p
<object_collector
> collector
;
81 * Creates stream that puts and gets data from specified
84 object_stream(p
<object_collector
> col
) : collector(col
) { }
88 * This method has to be invoked before serialization begins.
89 * If serialization has already started object_stream will
94 collector
->set_size(sizeof(T
));
99 * This method adds object to the stream.
102 object_stream
&operator<<(T
&obj
) {
103 collector
->add_int((char*)&obj
, sizeof(T
));
109 * This method gets object from the stream in the order
110 * they were added to.
113 object_stream
&operator>>(T
&obj
) {
114 collector
->get_int((char*)&obj
,0);
120 inline object_stream
&object_stream::operator>><obj_val
&>(obj_val
&obj
) {
121 collector
->get_val(&obj
);
126 inline object_stream
&object_stream::operator>><p
<obj_val
> >(p
<obj_val
> &obj
) {
127 obj
= collector
->get_val();
132 inline object_stream
&object_stream::operator>><p
<obj_ref
> >(p
<obj_ref
> &obj
) {
133 obj
= collector
->get_ref();
138 inline object_stream
&object_stream::operator<<<obj_val
&>(obj_val
&obj
) {
139 collector
->add_val(&obj
);
144 inline object_stream
&object_stream::operator<<<p
<obj_val
> >(p
<obj_val
> &obj
) {
145 collector
->add_val(&*obj
);
150 inline object_stream
&object_stream::operator<<<p
<obj_ref
> >(p
<obj_ref
> &obj
) {
151 collector
->add_ref(&*obj
);