2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #ifndef __STG_SGCONFIG_READER_H__
22 #define __STG_SGCONFIG_READER_H__
33 virtual ~BaseReader() {}
35 virtual ssize_t read(TransportProto& proto) = 0;
36 virtual bool done() const = 0;
40 class Reader : public BaseReader
43 Reader(size_t size = sizeof(T)) : m_size(size), m_done(0) {}
45 virtual ssize_t read(TransportProto& proto)
47 char* pos = static_cast<void*>(&m_dest);
49 ssize_t res = proto.read(pos, m_size - m_done);
60 virtual bool done() const { return m_done == m_size; }
62 T get() const { return ntoh(m_dest); }
72 class Reader<std::vector<Reader*> > : public BaseReader
75 Reader(const std::vector<Reader*>& readers) : m_size(readers.size()), m_done(0) {}
77 virtual ssize_t read(TransportProto& proto)
81 size_t res = m_dest[m_done]->read(proto);
88 if (m_dest[m_done].done())
93 virtual bool done() const { return m_done == m_size; }
95 const T& get() const { return m_dest; }
105 class Reader<std::vector<char> > : public BaseReader
108 Reader(size_t size ) : m_dest(size), m_size(size), m_done(0) {}
110 virtual ssize_t read(TransportProto& proto)
112 char* pos = static_cast<void*>(m_dest.data());
114 ssize_t res = proto.read(pos, m_size - m_done);
125 virtual bool done() const { return m_done == m_size; }
127 const std::vector<char>& get() const { return m_dest; }
130 std::vector<char> m_dest;
137 class Reader<std::string>
140 Reader() : m_dest(Reader<std::string>::initDest()) {}
142 virtual ssize_t read(TransportProto& proto)
146 size_t res = m_dest[m_done]->read(proto);
153 if (m_dest[m_done].done())
158 virtual bool done() const { return m_done == m_size; }
160 const T& get() const { return m_dest; }