ucommon
ucommon/protocols.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 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 class StringPager;
00041 
00042 class __EXPORT MemoryProtocol
00043 {
00044 protected:
00045     friend class MemoryRedirect;
00046 
00054     virtual void *_alloc(size_t size) = 0;
00055 
00059     virtual void fault(void) const;
00060 
00061 public:
00062     virtual ~MemoryProtocol();
00063 
00069     inline void *alloc(size_t size)
00070         {return _alloc(size);}
00071 
00079     void *zalloc(size_t size);
00080 
00087     char *dup(const char *string);
00088 
00095     void *dup(void *memory, size_t size);
00096 };
00097 
00103 class __EXPORT MemoryRedirect : public MemoryProtocol
00104 {
00105 private:
00106     MemoryProtocol *target;
00107 
00108 public:
00109     MemoryRedirect(MemoryProtocol *protocol);
00110 
00111     virtual void *_alloc(size_t size);
00112 };
00113 
00121 class __EXPORT LockingProtocol
00122 {
00123 protected:
00124     virtual void _lock(void);
00125     virtual void _unlock(void);
00126 
00127 public:
00128     virtual ~LockingProtocol();
00129 };
00130 
00137 class __EXPORT PrintProtocol
00138 {
00139 public:
00140     virtual ~PrintProtocol();
00141 
00145     virtual const char *_print(void) const = 0;
00146 };
00147 
00156 class __EXPORT InputProtocol
00157 {
00158 public:
00159     virtual ~InputProtocol();
00160 
00166     virtual int _input(int code) = 0;
00167 };
00168 
00174 class __EXPORT CharacterProtocol
00175 {
00176 protected:
00177     const char *eol;
00178     int back;
00179 
00180     CharacterProtocol();
00181 
00186     virtual int _getch(void) = 0;
00187 
00193     virtual int _putch(int code) = 0;
00194 
00199     inline void putback(int code)
00200         {back = code;}
00201 
00209     inline void seteol(const char *string)
00210         {eol = string;}
00211 
00212 public:
00213     virtual ~CharacterProtocol();
00214 
00219     inline int getchar(void)
00220         {return _getch();}
00221 
00227     inline int putchar(int code)
00228         {return _putch(code);}
00229 
00230     size_t print(const PrintProtocol& format);
00231 
00232     size_t input(InputProtocol& format);
00233 
00244     size_t getline(char *string, size_t size);
00245 
00255     size_t getline(String& buffer);
00256 
00263     size_t putline(const char *string);
00264 
00265     size_t putchars(const char *string, size_t count = 0);
00266 
00273     size_t load(StringPager *list);
00274 
00280     size_t save(const StringPager *list);
00281 };
00282 
00291 class __EXPORT BufferProtocol : public CharacterProtocol
00292 {
00293 public:
00294     typedef enum {RDONLY, WRONLY, RDWR} mode_t;
00295 
00296 private:
00297     char *buffer;
00298     char *input, *output;
00299     size_t bufsize, bufpos, insize, outsize;
00300     bool end;
00301 
00302 protected:
00303     const char *format;
00304 
00308     BufferProtocol();
00309 
00315     BufferProtocol(size_t size, mode_t access = RDWR);
00316 
00320     virtual ~BufferProtocol();
00321 
00325     virtual void fault(void) const;
00326 
00333     void allocate(size_t size, mode_t access = RDWR);
00334 
00338     void release(void);
00339 
00347     char *request(size_t size);
00348 
00355     char *gather(size_t size);
00356 
00364     virtual size_t _push(const char *address, size_t size) = 0;
00365 
00373     virtual size_t _pull(char *address, size_t size) = 0;
00374 
00379     virtual int _err(void) const = 0;
00380 
00384     virtual void _clear(void) = 0;
00385 
00389     virtual bool _blocking(void);
00390 
00394     virtual bool _pending(void);
00395 
00399     virtual bool _flush(void);
00400 
00401     virtual int _getch(void);
00402 
00403     virtual int _putch(int ch);
00404 
00410     inline size_t input_pending(void)
00411         {return bufpos;}
00412 
00417     inline size_t output_waiting(void)
00418         {return outsize;}
00419 
00420 public:
00421     const char *endl(void)
00422         {return eol;}
00423 
00431     size_t put(const void *address, size_t count);
00432 
00439     size_t get(void *address, size_t count);
00440 
00447     size_t printf(const char *format, ...) __PRINTF(2, 3);
00448 
00453     inline bool flush(void)
00454         {return _flush();}
00455 
00459     void purge(void);
00460 
00464     void reset(void);
00465 
00470     bool eof(void);
00471 
00476     inline operator bool()
00477         {return buffer != NULL;}
00478 
00483     inline bool operator!()
00484         {return buffer == NULL;}
00485 
00490     inline bool is_open(void)
00491         {return buffer != NULL;}
00492 
00497     inline bool is_input(void)
00498         {return input != NULL;}
00499 
00504     inline bool is_output(void)
00505         {return output != NULL;}
00506 
00511     inline bool is_pending(void)
00512         {return _pending();}
00513 
00517     inline void seteof(void)
00518         {end = true;}
00519 
00520     inline int err(void)
00521         {return _err();}
00522 
00523     template<typename T> inline size_t write(const T& data)
00524         {return put(&data, sizeof(T));}
00525 
00526     template<typename T> inline size_t read(T& data)
00527         {return get(&data, sizeof(T));}
00528 
00529     template<typename T> inline size_t write(const T* data, unsigned count)
00530         {return put(data, sizeof(T) * count) / sizeof(T);}
00531 
00532     template<typename T> inline size_t read(T* data, unsigned count)
00533         {return get(data, sizeof(T) * count) / sizeof(T);}
00534 
00535 };
00536 
00544 class __EXPORT ObjectProtocol
00545 {
00546 public:
00550     virtual void retain(void) = 0;
00551 
00555     virtual void release(void) = 0;
00556 
00560     virtual ~ObjectProtocol();
00561 
00565     ObjectProtocol *copy(void);
00566 
00570     inline void operator++(void)
00571         {retain();};
00572 
00576     inline void operator--(void)
00577         {release();};
00578 };
00579 
00583 class __EXPORT KeyProtocol
00584 {
00585 protected:
00586     virtual int keytype(void) const = 0;
00587 
00591     virtual size_t keysize(void) const = 0;
00592 
00596     virtual void *keydata(void) const = 0;
00597 
00598     virtual bool equal(const KeyProtocol& compare) const;
00599 
00600     inline bool operator!=(const KeyProtocol& compare) const {
00601         return !equal(compare);
00602     }
00603 
00604     virtual ~KeyProtocol();
00605 };
00606 
00611 class __EXPORT _character_operators
00612 {
00613 private:
00614     inline _character_operators() {}
00615 
00616 public:
00617     static CharacterProtocol& print(CharacterProtocol& p, const char *s);
00618 
00619     static CharacterProtocol& print(CharacterProtocol& p, const char& ch);
00620 
00621     static CharacterProtocol& input(CharacterProtocol& p, char& ch);
00622 
00623     static CharacterProtocol& input(CharacterProtocol& p, String& str);
00624 
00625     static CharacterProtocol& print(CharacterProtocol& p, const long& value);
00626 
00627     static CharacterProtocol& input(CharacterProtocol& p, long& value);
00628 
00629     static CharacterProtocol& print(CharacterProtocol& p, const double& value);
00630 
00631     static CharacterProtocol& input(CharacterProtocol& p, double& value);
00632 };
00633 
00634 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char *s)
00635     {return _character_operators::print(p, s);}
00636 
00637 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char& ch)
00638     {return _character_operators::print(p, ch);}
00639 
00640 inline CharacterProtocol& operator>> (CharacterProtocol& p, char& ch)
00641     {return _character_operators::input(p, ch);}
00642 
00643 inline CharacterProtocol& operator>> (CharacterProtocol& p, String& str)
00644     {return _character_operators::input(p, str);}
00645 
00646 inline CharacterProtocol& operator<< (CharacterProtocol& p, const PrintProtocol& format)
00647     {p.print(format); return p;}
00648 
00649 inline CharacterProtocol& operator>> (CharacterProtocol& p, InputProtocol& format)
00650     {p.input(format); return p;}
00651 
00652 inline CharacterProtocol& operator<< (CharacterProtocol& p, const StringPager& list)
00653     {p.save(&list); return p;}
00654 
00655 inline CharacterProtocol& operator>> (CharacterProtocol& p, StringPager& list)
00656     {p.load(&list); return p;}
00657 
00658 inline CharacterProtocol& operator<< (CharacterProtocol& p, const long& value)
00659     {return _character_operators::print(p, value);}
00660 
00661 inline CharacterProtocol& operator>> (CharacterProtocol& p, long& value)
00662     {return _character_operators::input(p, value);}
00663 
00664 inline CharacterProtocol& operator<< (CharacterProtocol& p, const double& value)
00665     {return _character_operators::print(p, value);}
00666 
00667 inline CharacterProtocol& operator>> (CharacterProtocol& p, double& value)
00668     {return _character_operators::input(p, value);}
00669 
00670 } // namespace ucommon
00671 
00672 #endif