usb: getting string descriptors, minor improvements
[quarnos.git] / manes / ods / object_stream.h
blobc8322d2bae417ee71864c457e41d53d2fbcd2b21
1 #ifndef _OBJECT_STREAM_
2 #define _OBJECT_STREAM_
4 #include "libs/pointer.h"
6 #include "obj_val.h"
7 #include "obj_ref.h"
9 namespace manes {
10 namespace ods {
11 /**
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 {
18 public:
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
33 * to be placed
35 virtual void get_ref(obj_ref *addr) = 0;
37 /** Get obj_ref
38 * @return address of obj_ref
40 virtual obj_ref *get_ref() = 0;
42 /** Add obj_ref
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
49 * to be placed
51 virtual void get_val(obj_val *addr) = 0;
53 /** Get obj_val
54 * @return address of obj_val
56 virtual obj_val *get_val() = 0;
58 /** Add obj_val
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;
67 /**
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.
75 class object_stream {
76 private:
77 p<object_collector> collector;
79 public:
80 /**
81 * Creates stream that puts and gets data from specified
82 * object collector.
84 object_stream(p<object_collector> col) : collector(col) { }
86 /**
87 * Start serialization
88 * This method has to be invoked before serialization begins.
89 * If serialization has already started object_stream will
90 * ignore this call.
92 template<typename T>
93 void start() {
94 collector->set_size(sizeof(T));
97 /**
98 * Add object
99 * This method adds object to the stream.
101 template<typename T>
102 object_stream &operator<<(T &obj) {
103 collector->add_int((char*)&obj, sizeof(T));
104 return *this;
108 * Get object
109 * This method gets object from the stream in the order
110 * they were added to.
112 template<typename T>
113 object_stream &operator>>(T &obj) {
114 collector->get_int((char*)&obj,0);
115 return *this;
119 template<>
120 inline object_stream &object_stream::operator>><obj_val&>(obj_val &obj) {
121 collector->get_val(&obj);
122 return *this;
125 template<>
126 inline object_stream &object_stream::operator>><p<obj_val> >(p<obj_val> &obj) {
127 obj = collector->get_val();
128 return *this;
131 template<>
132 inline object_stream &object_stream::operator>><p<obj_ref> >(p<obj_ref> &obj) {
133 obj = collector->get_ref();
134 return *this;
137 template<>
138 inline object_stream &object_stream::operator<<<obj_val&>(obj_val &obj) {
139 collector->add_val(&obj);
140 return *this;
143 template<>
144 inline object_stream &object_stream::operator<<<p<obj_val> >(p<obj_val> &obj) {
145 collector->add_val(&*obj);
146 return *this;
149 template<>
150 inline object_stream &object_stream::operator<<<p<obj_ref> >(p<obj_ref> &obj) {
151 collector->add_ref(&*obj);
152 return *this;
157 #endif