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

Revision 742, 4.9 kB (checked in by ppalmers, 16 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2007 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 3 of the License, or
12  * (at your option) any later version.
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
32 namespace Util {
33
34 // Interfaces
35
36 class IOSSerialize {
37 public:
38     IOSSerialize() {}
39     virtual ~IOSSerialize() {}
40
41     virtual bool write( byte_t value, const char* name = "" ) = 0;
42     virtual bool write( uint16_t value, const char* name = "" ) = 0;
43     virtual bool write( quadlet_t value, const char* name = "" ) = 0;
44     virtual bool write( const char *values, size_t len, const char* name = "" ) = 0;
45 };
46
47 class IISDeserialize {
48 public:
49     IISDeserialize() {}
50     virtual ~IISDeserialize() {}
51
52     virtual bool read( byte_t* value ) = 0;
53     virtual bool read( uint16_t* value ) = 0;
54     virtual bool read( quadlet_t* value ) = 0;
55     // note that the value pointer is not valid outside deserialize()
56     virtual bool read( char** value, size_t length ) = 0;
57     virtual bool peek( byte_t* value ) = 0;
58     virtual bool peek( uint16_t* value, size_t offset )=0;
59     virtual bool skip( size_t length ) = 0;
60     virtual int getNrOfConsumedBytes()  const = 0;
61 };
62
63 // Specialized implementations of previously defined interfaces
64
65 class CoutSerializer: public IOSSerialize {
66 public:
67     CoutSerializer()
68         : IOSSerialize()
69         , m_cnt( 0 )
70         {}
71     virtual ~CoutSerializer() {}
72
73     virtual bool write( byte_t value, const char* name = "" );
74     virtual bool write( uint16_t value, const char* name = "" );
75     virtual bool write( quadlet_t value,  const char* name = "" );
76     virtual bool write( const char *values, size_t len, const char* name = "" );
77
78 private:
79     unsigned int m_cnt;
80     DECLARE_DEBUG_MODULE;
81
82 };
83
84 class StringSerializer: public IOSSerialize {
85 public:
86     StringSerializer()
87         : IOSSerialize()
88         , m_cnt( 0 )
89         {}
90     virtual ~StringSerializer() {}
91
92     virtual bool write( byte_t value, const char* name = "" );
93     virtual bool write( uint16_t value, const char* name = "" );
94     virtual bool write( quadlet_t value,  const char* name = "" );
95     virtual bool write( const char *values, size_t len, const char* name = "" );
96     virtual std::string getString( ) { return m_string;};
97
98 private:
99     unsigned int m_cnt;
100     std::string m_string;
101     DECLARE_DEBUG_MODULE;
102 };
103
104 class BufferSerialize: public IOSSerialize {
105 public:
106     BufferSerialize( unsigned char* buffer, size_t length )
107         : IOSSerialize()
108         , m_buffer( buffer )
109         , m_curPos( m_buffer )
110         , m_length( length )
111         {}
112     virtual ~BufferSerialize() {}
113
114     virtual bool write( byte_t value, const char* name = "" );
115     virtual bool write( uint16_t value, const char* name = "" );
116     virtual bool write( quadlet_t value,  const char* name = "" );
117     virtual bool write( const char *values, size_t len, const char* name = "" );
118
119     int getNrOfProducesBytes() const
120     { return m_curPos - m_buffer; }
121
122 protected:
123     inline bool isCurPosValid() const;
124
125 private:
126     unsigned char* m_buffer;
127     unsigned char* m_curPos;
128     size_t m_length;
129     DECLARE_DEBUG_MODULE;
130 };
131
132 class BufferDeserialize: public IISDeserialize {
133 public:
134     BufferDeserialize( const unsigned char* buffer, size_t length )
135         : IISDeserialize()
136         , m_buffer( const_cast<unsigned char*>( buffer ) )
137         , m_curPos( m_buffer )
138         , m_length( length )
139         {}
140     virtual ~BufferDeserialize() {}
141
142     virtual bool read( byte_t* value );
143     virtual bool read( uint16_t* value );
144     virtual bool read( quadlet_t* value );
145     // note that the value pointer is not valid outside deserialize()
146     virtual bool read( char** value, size_t length );
147     virtual bool peek( byte_t* value );
148     virtual bool peek( uint16_t* value, size_t offset );
149     virtual bool skip( size_t length );
150
151     int getNrOfConsumedBytes()  const
152         { return m_curPos - m_buffer; }
153
154 protected:
155     inline bool isCurPosValid() const;
156
157 private:
158     unsigned char* m_buffer; // start of the buffer
159     unsigned char* m_curPos; // current read pos
160     size_t m_length;         // size of buffer
161     DECLARE_DEBUG_MODULE;
162 };
163
164 }
165 #endif // UTIL_CMD_SERIALIZE_H
166
Note: See TracBrowser for help on using the browser.