UCommon
protocols.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ 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 Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
30 #ifndef _UCOMMON_PROTOCOLS_H_
31 #define _UCOMMON_PROTOCOLS_H_
32 
33 #ifndef _UCOMMON_CPR_H_
34 #include <ucommon/cpr.h>
35 #endif
36 
37 NAMESPACE_UCOMMON
38 
39 class string;
40 
41 class __EXPORT MemoryProtocol
42 {
43 protected:
44  friend class MemoryRedirect;
52  virtual void *_alloc(size_t size) = 0;
53 
54 public:
55  virtual ~MemoryProtocol();
56 
62  inline void *alloc(size_t size)
63  {return _alloc(size);};
64 
72  void *zalloc(size_t size);
73 
80  char *dup(const char *string);
81 
88  void *dup(void *memory, size_t size);
89 };
90 
96 class __EXPORT MemoryRedirect : public MemoryProtocol
97 {
98 private:
99  MemoryProtocol *target;
100 
101 public:
102  MemoryRedirect(MemoryProtocol *protocol);
103 
104  virtual void *_alloc(size_t size);
105 };
106 
114 class __EXPORT LockingProtocol
115 {
116 protected:
117  virtual void _lock(void);
118  virtual void _unlock(void);
119 
120 public:
121  virtual ~LockingProtocol();
122 };
123 
129 class __EXPORT CharacterProtocol
130 {
131 protected:
136  virtual int _getch(void) = 0;
137 
143  virtual int _putch(int code) = 0;
144 
145 public:
146  virtual ~CharacterProtocol();
147 
152  inline int get(void)
153  {return _getch();};
154 
160  inline int put(int code)
161  {return _putch(code);};
162 };
163 
172 class __EXPORT BufferProtocol : public CharacterProtocol
173 {
174 public:
175  typedef enum {BUF_RD, BUF_WR, BUF_RDWR} type_t;
176 
177 private:
178  const char *eol;
179  char *buffer;
180  char *input, *output;
181  size_t bufsize, bufpos, insize, outsize;
182  bool end;
183 
184 protected:
185  const char *format;
186 
190  BufferProtocol();
191 
197  BufferProtocol(size_t size, type_t access = BUF_RDWR);
198 
202  virtual ~BufferProtocol();
203 
211  inline void seteol(const char *string)
212  {eol = string;};
213 
220  void allocate(size_t size, type_t access = BUF_RDWR);
221 
225  void release(void);
226 
234  char *request(size_t size);
235 
242  char *gather(size_t size);
243 
251  virtual size_t _push(const char *address, size_t size) = 0;
252 
260  virtual size_t _pull(char *address, size_t size) = 0;
261 
266  virtual int _err(void) const = 0;
267 
271  virtual void _clear(void) = 0;
272 
276  virtual bool _blocking(void);
277 
281  virtual bool _pending(void);
282 
286  virtual bool _flush(void);
287 
293  inline size_t unread(void)
294  {return bufpos;};
295 
300  inline size_t unsaved(void)
301  {return outsize;};
302 
303 public:
311  size_t get(char *address, size_t count);
312 
321  size_t put(const char *address, size_t count = 0);
322 
327  int _getch(void);
328 
333  int _putch(int ch);
334 
341  size_t printf(const char *format, ...) __PRINTF(2, 3);
342 
347  inline bool flush(void)
348  {return _flush();}
349 
353  void purge(void);
354 
358  void reset(void);
359 
370  size_t getline(char *string, size_t size);
371 
381  size_t getline(string& buffer);
382 
389  size_t putline(const char *string);
390 
395  bool eof(void);
396 
401  inline operator bool()
402  {return buffer != NULL;}
403 
408  inline bool operator!()
409  {return buffer == NULL;}
410 
415  inline bool is_open(void)
416  {return buffer != NULL;}
417 
422  inline bool is_input(void)
423  {return input != NULL;}
424 
429  inline bool is_output(void)
430  {return output != NULL;}
431 
436  inline bool is_pending(void)
437  {return _pending();}
438 
442  inline void seteof(void)
443  {end = true;}
444 
445  inline int err(void)
446  {return _err();}
447 };
448 
449 END_NAMESPACE
450 
451 #endif