ucommon
ucommon/protocols.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00030 #ifndef _UCOMMON_PROTOCOLS_H_
00031 #define _UCOMMON_PROTOCOLS_H_
00032 
00033 #ifndef _UCOMMON_CPR_H_
00034 #include <ucommon/cpr.h>
00035 #endif
00036 
00037 NAMESPACE_UCOMMON
00038 
00039 class string;
00040 
00041 class __EXPORT MemoryProtocol
00042 {
00043 protected:
00044     friend class MemoryRedirect;
00052     virtual void *_alloc(size_t size) = 0;
00053 
00054 public:
00055     virtual ~MemoryProtocol();
00056 
00062     inline void *alloc(size_t size)
00063         {return _alloc(size);};
00064 
00072     void *zalloc(size_t size);
00073 
00080     char *dup(const char *string);
00081 
00088     void *dup(void *memory, size_t size);
00089 };
00090 
00096 class __EXPORT MemoryRedirect : public MemoryProtocol
00097 {
00098 private:
00099     MemoryProtocol *target;
00100 
00101 public:
00102     MemoryRedirect(MemoryProtocol *protocol);
00103 
00104     virtual void *_alloc(size_t size);
00105 };
00106 
00114 class __EXPORT LockingProtocol
00115 {
00116 protected:
00117     virtual void _lock(void);
00118     virtual void _unlock(void);
00119 
00120 public:
00121     virtual ~LockingProtocol();
00122 };
00123 
00129 class __EXPORT CharacterProtocol
00130 {
00131 protected:
00136     virtual int _getch(void) = 0;
00137 
00143     virtual int _putch(int code) = 0;
00144 
00145 public:
00146     virtual ~CharacterProtocol();
00147 
00152     inline int get(void)
00153         {return _getch();};
00154 
00160     inline int put(int code)
00161         {return _putch(code);};
00162 };
00163 
00172 class __EXPORT BufferProtocol : public CharacterProtocol
00173 {
00174 public:
00175     typedef enum {BUF_RD, BUF_WR, BUF_RDWR} type_t;
00176 
00177 private:
00178     const char *eol;
00179     char *buffer;
00180     char *input, *output;
00181     size_t bufsize, bufpos, insize, outsize;
00182     bool end;
00183 
00184 protected:
00185     const char *format;
00186 
00190     BufferProtocol();
00191 
00197     BufferProtocol(size_t size, type_t access = BUF_RDWR);
00198 
00202     virtual ~BufferProtocol();
00203 
00211     inline void seteol(const char *string)
00212         {eol = string;};
00213 
00220     void allocate(size_t size, type_t access = BUF_RDWR);
00221 
00225     void release(void);
00226 
00234     char *request(size_t size);
00235 
00242     char *gather(size_t size);
00243 
00251     virtual size_t _push(const char *address, size_t size) = 0;
00252 
00260     virtual size_t _pull(char *address, size_t size) = 0;
00261 
00266     virtual int _err(void) const = 0;
00267 
00271     virtual void _clear(void) = 0;
00272 
00276     virtual bool _blocking(void);
00277 
00281     virtual bool _pending(void);
00282 
00286     virtual bool _flush(void);
00287 
00293     inline size_t unread(void)
00294         {return bufpos;};
00295 
00300     inline size_t unsaved(void)
00301         {return outsize;};
00302 
00303 public:
00311     size_t get(char *address, size_t count);
00312 
00321     size_t put(const char *address, size_t count = 0);
00322 
00327     int _getch(void);
00328 
00333     int _putch(int ch);
00334 
00341     size_t printf(const char *format, ...) __PRINTF(2, 3);
00342 
00347     inline bool flush(void)
00348         {return _flush();}
00349 
00353     void purge(void);
00354 
00358     void reset(void);
00359 
00370     size_t getline(char *string, size_t size);
00371 
00381     size_t getline(string& buffer);
00382 
00389     size_t putline(const char *string);
00390 
00395     bool eof(void);
00396 
00401     inline operator bool()
00402         {return buffer != NULL;}
00403 
00408     inline bool operator!()
00409         {return buffer == NULL;}
00410 
00415     inline bool is_open(void)
00416         {return buffer != NULL;}
00417 
00422     inline bool is_input(void)
00423         {return input != NULL;}
00424 
00429     inline bool is_output(void)
00430         {return output != NULL;}
00431 
00436     inline bool is_pending(void)
00437         {return _pending();}
00438 
00442     inline void seteof(void)
00443         {end = true;}
00444 
00445     inline int err(void)
00446         {return _err();}
00447 };
00448 
00449 END_NAMESPACE
00450 
00451 #endif