root/trunk/libffado/src/libutil/cmd_serialize.h

Revision 1568, 4.9 kB (checked in by holin, 15 years ago)

gcc 4.4 fixes (r1566, r1567, DICE) to trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef UTIL_CMD_SERIALIZE_H
25 #define UTIL_CMD_SERIALIZE_H
26
27 #include "debugmodule/debugmodule.h"
28
29 #include <libraw1394/raw1394.h> // byte_t and quadlet_t declaration
30 #include <string>
31 #include <stdint.h>
32
33 namespace Util {
34         namespace Cmd {
35
36 // Interfaces
37
38 class IOSSerialize {
39 public:
40     IOSSerialize() {}
41     virtual ~IOSSerialize() {}
42
43     virtual bool write( byte_t value, const char* name = "" ) = 0;
44     virtual bool write( uint16_t value, const char* name = "" ) = 0;
45     virtual bool write( quadlet_t value, const char* name = "" ) = 0;
46     virtual bool write( const char *values, size_t len, const char* name = "" ) = 0;
47 };
48
49 class IISDeserialize {
50 public:
51     IISDeserialize() {}
52     virtual ~IISDeserialize() {}
53
54     virtual bool read( byte_t* value ) = 0;
55     virtual bool read( uint16_t* value ) = 0;
56     virtual bool read( quadlet_t* value ) = 0;
57     // note that the value pointer is not valid outside deserialize()
58     virtual bool read( char** value, size_t length ) = 0;
59     virtual bool peek( byte_t* value ) = 0;
60     virtual bool peek( uint16_t* value, size_t offset )=0;
61     virtual bool skip( size_t length ) = 0;
62     virtual int getNrOfConsumedBytes()  const = 0;
63 };
64
65 // Specialized implementations of previously defined interfaces
66
67 class CoutSerializer: public IOSSerialize {
68 public:
69     CoutSerializer()
70         : IOSSerialize()
71         , m_cnt( 0 )
72         {}
73     virtual ~CoutSerializer() {}
74
75     virtual bool write( byte_t value, const char* name = "" );
76     virtual bool write( uint16_t value, const char* name = "" );
77     virtual bool write( quadlet_t value,  const char* name = "" );
78     virtual bool write( const char *values, size_t len, const char* name = "" );
79
80 private:
81     unsigned int m_cnt;
82     DECLARE_DEBUG_MODULE;
83
84 };
85
86 class StringSerializer: public IOSSerialize {
87 public:
88     StringSerializer()
89         : IOSSerialize()
90         , m_cnt( 0 )
91         {}
92     virtual ~StringSerializer() {}
93
94     virtual bool write( byte_t value, const char* name = "" );
95     virtual bool write( uint16_t value, const char* name = "" );
96     virtual bool write( quadlet_t value,  const char* name = "" );
97     virtual bool write( const char *values, size_t len, const char* name = "" );
98     virtual std::string getString( ) { return m_string;};
99
100 private:
101     unsigned int m_cnt;
102     std::string m_string;
103     DECLARE_DEBUG_MODULE;
104 };
105
106 class BufferSerialize: public IOSSerialize {
107 public:
108     BufferSerialize( unsigned char* buffer, size_t length )
109         : IOSSerialize()
110         , m_buffer( buffer )
111         , m_curPos( m_buffer )
112         , m_length( length )
113         {}
114     virtual ~BufferSerialize() {}
115
116     virtual bool write( byte_t value, const char* name = "" );
117     virtual bool write( uint16_t value, const char* name = "" );
118     virtual bool write( quadlet_t value,  const char* name = "" );
119     virtual bool write( const char *values, size_t len, const char* name = "" );
120
121     int getNrOfProducesBytes() const
122     { return m_curPos - m_buffer; }
123
124 protected:
125     inline bool isCurPosValid() const;
126
127 private:
128     unsigned char* m_buffer;
129     unsigned char* m_curPos;
130     size_t m_length;
131     DECLARE_DEBUG_MODULE;
132 };
133
134 class BufferDeserialize: public IISDeserialize {
135 public:
136     BufferDeserialize( const unsigned char* buffer, size_t length )
137         : IISDeserialize()
138         , m_buffer( const_cast<unsigned char*>( buffer ) )
139         , m_curPos( m_buffer )
140         , m_length( length )
141         {}
142     virtual ~BufferDeserialize() {}
143
144     virtual bool read( byte_t* value );
145     virtual bool read( uint16_t* value );
146     virtual bool read( quadlet_t* value );
147     // note that the value pointer is not valid outside deserialize()
148     virtual bool read( char** value, size_t length );
149     virtual bool peek( byte_t* value );
150     virtual bool peek( uint16_t* value, size_t offset );
151     virtual bool skip( size_t length );
152
153     int getNrOfConsumedBytes()  const
154         { return m_curPos - m_buffer; }
155
156 protected:
157     inline bool isCurPosValid() const;
158
159 private:
160     unsigned char* m_buffer; // start of the buffer
161     unsigned char* m_curPos; // current read pos
162     size_t m_length;         // size of buffer
163     DECLARE_DEBUG_MODULE;
164 };
165
166         }
167 }
168 #endif // UTIL_CMD_SERIALIZE_H
169
Note: See TracBrowser for help on using the browser.