1 |
/* avc_serialize.h |
---|
2 |
* Copyright (C) 2005,07 by Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FreeBoB. |
---|
5 |
* |
---|
6 |
* FreeBoB is free software; you can redistribute it and/or modify |
---|
7 |
* it under the terms of the GNU General Public License as published by |
---|
8 |
* the Free Software Foundation; either version 2 of the License, or |
---|
9 |
* (at your option) any later version. |
---|
10 |
* FreeBoB is distributed in the hope that it will be useful, |
---|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 |
* GNU General Public License for more details. |
---|
14 |
* |
---|
15 |
* You should have received a copy of the GNU General Public License |
---|
16 |
* along with FreeBoB; if not, write to the Free Software |
---|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
18 |
* MA 02111-1307 USA. |
---|
19 |
*/ |
---|
20 |
|
---|
21 |
#ifndef Serialize_h |
---|
22 |
#define Serialize_h |
---|
23 |
|
---|
24 |
#include "debugmodule/debugmodule.h" |
---|
25 |
|
---|
26 |
#include <libraw1394/raw1394.h> // byte_t and quadlet_t declaration |
---|
27 |
#include <string> |
---|
28 |
|
---|
29 |
// Interfaces |
---|
30 |
|
---|
31 |
class IOSSerialize { |
---|
32 |
public: |
---|
33 |
IOSSerialize() {} |
---|
34 |
virtual ~IOSSerialize() {} |
---|
35 |
|
---|
36 |
virtual bool write( byte_t value, const char* name = "" ) = 0; |
---|
37 |
virtual bool write( quadlet_t value, const char* name = "" ) = 0; |
---|
38 |
}; |
---|
39 |
|
---|
40 |
class IISDeserialize { |
---|
41 |
public: |
---|
42 |
IISDeserialize() {} |
---|
43 |
virtual ~IISDeserialize() {} |
---|
44 |
|
---|
45 |
virtual bool read( byte_t* value ) = 0; |
---|
46 |
virtual bool read( quadlet_t* value ) = 0; |
---|
47 |
virtual bool read( char** value, size_t length ) = 0; |
---|
48 |
virtual bool peek( byte_t* value ) = 0; |
---|
49 |
}; |
---|
50 |
|
---|
51 |
// Specialized implementations of previously defined interfaces |
---|
52 |
|
---|
53 |
class CoutSerializer: public IOSSerialize { |
---|
54 |
public: |
---|
55 |
CoutSerializer() |
---|
56 |
: IOSSerialize() |
---|
57 |
, m_cnt( 0 ) |
---|
58 |
{} |
---|
59 |
virtual ~CoutSerializer() {} |
---|
60 |
|
---|
61 |
virtual bool write( byte_t value, const char* name = "" ); |
---|
62 |
virtual bool write( quadlet_t value, const char* name = "" ); |
---|
63 |
|
---|
64 |
private: |
---|
65 |
unsigned int m_cnt; |
---|
66 |
DECLARE_DEBUG_MODULE; |
---|
67 |
|
---|
68 |
}; |
---|
69 |
|
---|
70 |
class StringSerializer: public IOSSerialize { |
---|
71 |
public: |
---|
72 |
StringSerializer() |
---|
73 |
: IOSSerialize() |
---|
74 |
, m_cnt( 0 ) |
---|
75 |
{} |
---|
76 |
virtual ~StringSerializer() {} |
---|
77 |
|
---|
78 |
virtual bool write( byte_t value, const char* name = "" ); |
---|
79 |
virtual bool write( quadlet_t value, const char* name = "" ); |
---|
80 |
virtual std::string getString( ) { return m_string;}; |
---|
81 |
|
---|
82 |
private: |
---|
83 |
unsigned int m_cnt; |
---|
84 |
std::string m_string; |
---|
85 |
|
---|
86 |
}; |
---|
87 |
|
---|
88 |
class BufferSerialize: public IOSSerialize { |
---|
89 |
public: |
---|
90 |
BufferSerialize( unsigned char* buffer, size_t length ) |
---|
91 |
: IOSSerialize() |
---|
92 |
, m_buffer( buffer ) |
---|
93 |
, m_curPos( m_buffer ) |
---|
94 |
, m_length( length ) |
---|
95 |
{} |
---|
96 |
virtual ~BufferSerialize() {} |
---|
97 |
|
---|
98 |
virtual bool write( byte_t value, const char* name = "" ); |
---|
99 |
virtual bool write( quadlet_t value, const char* name = "" ); |
---|
100 |
|
---|
101 |
int getNrOfProducesBytes() const |
---|
102 |
{ return m_curPos - m_buffer; } |
---|
103 |
|
---|
104 |
protected: |
---|
105 |
inline bool isCurPosValid() const; |
---|
106 |
|
---|
107 |
private: |
---|
108 |
unsigned char* m_buffer; |
---|
109 |
unsigned char* m_curPos; |
---|
110 |
size_t m_length; |
---|
111 |
}; |
---|
112 |
|
---|
113 |
class BufferDeserialize: public IISDeserialize { |
---|
114 |
public: |
---|
115 |
BufferDeserialize( const unsigned char* buffer, size_t length ) |
---|
116 |
: IISDeserialize() |
---|
117 |
, m_buffer( const_cast<unsigned char*>( buffer ) ) |
---|
118 |
, m_curPos( m_buffer ) |
---|
119 |
, m_length( length ) |
---|
120 |
{} |
---|
121 |
virtual ~BufferDeserialize() {} |
---|
122 |
|
---|
123 |
virtual bool read( byte_t* value ); |
---|
124 |
virtual bool read( quadlet_t* value ); |
---|
125 |
virtual bool read( char** value, size_t length ); |
---|
126 |
virtual bool peek( byte_t* value ); |
---|
127 |
|
---|
128 |
int getNrOfConsumedBytes() const |
---|
129 |
{ return m_curPos - m_buffer; } |
---|
130 |
|
---|
131 |
protected: |
---|
132 |
inline bool isCurPosValid() const; |
---|
133 |
|
---|
134 |
private: |
---|
135 |
unsigned char* m_buffer; // start of the buffer |
---|
136 |
unsigned char* m_curPos; // current read pos |
---|
137 |
size_t m_length; // size of buffer |
---|
138 |
}; |
---|
139 |
|
---|
140 |
#endif // Serialize_h |
---|
141 |
|
---|