UCommon
protocols.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
31 #ifndef _UCOMMON_PROTOCOLS_H_
32 #define _UCOMMON_PROTOCOLS_H_
33 
34 #ifndef _UCOMMON_CPR_H_
35 #include <ucommon/cpr.h>
36 #endif
37 
38 namespace ucommon {
39 
40 class String;
41 class StringPager;
42 
43 class __EXPORT MemoryProtocol
44 {
45 protected:
46  friend class MemoryRedirect;
47 
55  virtual void *_alloc(size_t size) = 0;
56 
60  virtual void fault(void) const;
61 
62 public:
63  virtual ~MemoryProtocol();
64 
70  inline void *alloc(size_t size)
71  {return _alloc(size);}
72 
80  void *zalloc(size_t size);
81 
88  char *dup(const char *string);
89 
96  void *dup(void *memory, size_t size);
97 };
98 
104 class __EXPORT MemoryRedirect : public MemoryProtocol
105 {
106 private:
107  MemoryProtocol *target;
108 
109 public:
110  MemoryRedirect(MemoryProtocol *protocol);
111 
112  virtual void *_alloc(size_t size);
113 };
114 
122 class __EXPORT LockingProtocol
123 {
124 protected:
125  virtual void _lock(void);
126  virtual void _unlock(void);
127 
128 public:
129  virtual ~LockingProtocol();
130 };
131 
138 class __EXPORT PrintProtocol
139 {
140 public:
141  virtual ~PrintProtocol();
142 
146  virtual const char *_print(void) const = 0;
147 };
148 
157 class __EXPORT InputProtocol
158 {
159 public:
160  virtual ~InputProtocol();
161 
167  virtual int _input(int code) = 0;
168 };
169 
175 class __EXPORT CharacterProtocol
176 {
177 protected:
178  const char *eol;
179  int back;
180 
182 
187  virtual int _getch(void) = 0;
188 
194  virtual int _putch(int code) = 0;
195 
200  inline void putback(int code)
201  {back = code;}
202 
210  inline void seteol(const char *string)
211  {eol = string;}
212 
213 public:
214  virtual ~CharacterProtocol();
215 
220  inline int getchar(void)
221  {return _getch();}
222 
228  inline int putchar(int code)
229  {return _putch(code);}
230 
231  size_t print(const PrintProtocol& format);
232 
233  size_t input(InputProtocol& format);
234 
245  size_t getline(char *string, size_t size);
246 
256  size_t getline(String& buffer);
257 
264  size_t putline(const char *string);
265 
266  size_t putchars(const char *string, size_t count = 0);
267 
274  size_t load(StringPager *list);
275 
281  size_t save(const StringPager *list);
282 };
283 
292 class __EXPORT BufferProtocol : public CharacterProtocol
293 {
294 public:
295  typedef enum {RDONLY, WRONLY, RDWR} mode_t;
296 
297 private:
298  char *buffer;
299  char *input, *output;
300  size_t bufsize, bufpos, insize, outsize;
301  bool end;
302 
303 protected:
304  const char *format;
305 
309  BufferProtocol();
310 
316  BufferProtocol(size_t size, mode_t access = RDWR);
317 
321  virtual ~BufferProtocol();
322 
326  virtual void fault(void) const;
327 
334  void allocate(size_t size, mode_t access = RDWR);
335 
339  void release(void);
340 
348  char *request(size_t size);
349 
356  char *gather(size_t size);
357 
365  virtual size_t _push(const char *address, size_t size) = 0;
366 
374  virtual size_t _pull(char *address, size_t size) = 0;
375 
380  virtual int _err(void) const = 0;
381 
385  virtual void _clear(void) = 0;
386 
390  virtual bool _blocking(void);
391 
395  virtual bool _pending(void);
396 
400  virtual bool _flush(void);
401 
402  virtual int _getch(void);
403 
404  virtual int _putch(int ch);
405 
411  inline size_t input_pending(void) const
412  {return bufpos;}
413 
418  inline size_t output_waiting(void) const
419  {return outsize;}
420 
421 public:
422  const char *endl(void) const
423  {return eol;}
424 
432  size_t put(const void *address, size_t count);
433 
440  size_t get(void *address, size_t count);
441 
448  size_t printf(const char *format, ...) __PRINTF(2, 3);
449 
454  inline bool flush(void)
455  {return _flush();}
456 
460  void purge(void);
461 
465  void reset(void);
466 
471  bool eof(void);
472 
477  inline operator bool() const
478  {return buffer != NULL;}
479 
484  inline bool operator!() const
485  {return buffer == NULL;}
486 
491  inline bool is_open(void) const
492  {return buffer != NULL;}
493 
498  inline bool is_input(void) const
499  {return input != NULL;}
500 
505  inline bool is_output(void) const
506  {return output != NULL;}
507 
512  inline bool is_pending(void)
513  {return _pending();}
514 
518  inline void seteof(void)
519  {end = true;}
520 
521  inline int err(void) const
522  {return _err();}
523 
524  template<typename T> inline size_t write(const T& data)
525  {return put(&data, sizeof(T));}
526 
527  template<typename T> inline size_t read(T& data)
528  {return get(&data, sizeof(T));}
529 
530  template<typename T> inline size_t write(const T* data, unsigned count)
531  {return put(data, sizeof(T) * count) / sizeof(T);}
532 
533  template<typename T> inline size_t read(T* data, unsigned count)
534  {return get(data, sizeof(T) * count) / sizeof(T);}
535 
536 };
537 
545 class __EXPORT ObjectProtocol
546 {
547 public:
551  virtual void retain(void) = 0;
552 
556  virtual void release(void) = 0;
557 
561  virtual ~ObjectProtocol();
562 
566  ObjectProtocol *copy(void);
567 
571  inline void operator++(void)
572  {retain();};
573 
577  inline void operator--(void)
578  {release();};
579 };
580 
584 class __EXPORT KeyProtocol
585 {
586 protected:
587  virtual int keytype(void) const = 0;
588 
592  virtual size_t keysize(void) const = 0;
593 
597  virtual const void *keydata(void) const = 0;
598 
599  virtual bool equal(const KeyProtocol& compare) const;
600 
601  inline bool operator!=(const KeyProtocol& compare) const {
602  return !equal(compare);
603  }
604 
605  virtual ~KeyProtocol();
606 };
607 
612 class __EXPORT _character_operators
613 {
614 private:
615  inline _character_operators() {}
616 
617 public:
618  static CharacterProtocol& print(CharacterProtocol& p, const char *s);
619 
620  static CharacterProtocol& print(CharacterProtocol& p, const char& ch);
621 
622  static CharacterProtocol& input(CharacterProtocol& p, char& ch);
623 
624  static CharacterProtocol& input(CharacterProtocol& p, String& str);
625 
626  static CharacterProtocol& print(CharacterProtocol& p, const long& value);
627 
628  static CharacterProtocol& input(CharacterProtocol& p, long& value);
629 
630  static CharacterProtocol& print(CharacterProtocol& p, const double& value);
631 
632  static CharacterProtocol& input(CharacterProtocol& p, double& value);
633 };
634 
635 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char *s)
636  {return _character_operators::print(p, s);}
637 
638 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char& ch)
639  {return _character_operators::print(p, ch);}
640 
641 inline CharacterProtocol& operator>> (CharacterProtocol& p, char& ch)
642  {return _character_operators::input(p, ch);}
643 
644 inline CharacterProtocol& operator>> (CharacterProtocol& p, String& str)
645  {return _character_operators::input(p, str);}
646 
647 inline CharacterProtocol& operator<< (CharacterProtocol& p, const PrintProtocol& format)
648  {p.print(format); return p;}
649 
650 inline CharacterProtocol& operator>> (CharacterProtocol& p, InputProtocol& format)
651  {p.input(format); return p;}
652 
653 inline CharacterProtocol& operator<< (CharacterProtocol& p, const StringPager& list)
654  {p.save(&list); return p;}
655 
656 inline CharacterProtocol& operator>> (CharacterProtocol& p, StringPager& list)
657  {p.load(&list); return p;}
658 
659 inline CharacterProtocol& operator<< (CharacterProtocol& p, const long& value)
660  {return _character_operators::print(p, value);}
661 
662 inline CharacterProtocol& operator>> (CharacterProtocol& p, long& value)
663  {return _character_operators::input(p, value);}
664 
665 inline CharacterProtocol& operator<< (CharacterProtocol& p, const double& value)
666  {return _character_operators::print(p, value);}
667 
668 inline CharacterProtocol& operator>> (CharacterProtocol& p, double& value)
669  {return _character_operators::input(p, value);}
670 
671 } // namespace ucommon
672 
673 #endif
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:271
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
Common locking protocol.
Definition: protocols.h:122
size_t input_pending(void) const
Get current input position.
Definition: protocols.h:411
void putback(int code)
Write to back buffer.
Definition: protocols.h:200
void operator++(void)
Increase retention operator.
Definition: protocols.h:571
At least with gcc, linking of stream operators was broken.
Definition: protocols.h:612
bool is_pending(void)
See if pending input.
Definition: protocols.h:512
void operator--(void)
Decrease retention operator.
Definition: protocols.h:577
bool is_input(void) const
See if input active.
Definition: protocols.h:498
void seteof(void)
Set eof flag.
Definition: protocols.h:518
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
A copy-on-write string class that operates by reference count.
Definition: string.h:83
Used for forming stream output.
Definition: protocols.h:138
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:324
A redirection base class for the memory protocol.
Definition: protocols.h:104
A common base class for all managed objects.
Definition: protocols.h:545
bool operator!() const
See if buffer closed.
Definition: protocols.h:484
int putchar(int code)
Put the next character.
Definition: protocols.h:228
Common buffer protocol class.
Definition: protocols.h:292
int getchar(void)
Get the next character.
Definition: protocols.h:220
Common namespace for all ucommon objects.
Definition: access.h:47
Key data protocol used for things like maps and ordered lists.
Definition: protocols.h:584
void retain(ObjectProtocol *object)
Convenience function to access object retention.
Definition: object.h:492
size_t output_waiting(void) const
Get current output position.
Definition: protocols.h:418
bool is_open(void) const
See if buffer open.
Definition: protocols.h:491
String pager for storing lists of NULL terminated strings.
Definition: memory.h:410
Used for processing input.
Definition: protocols.h:157
void seteol(const char *string)
Set end of line marker.
Definition: protocols.h:210
bool is_output(void) const
See if output active.
Definition: protocols.h:505
Runtime functions.
Data keys parsed from a keyfile.
Definition: keydata.h:58
Common character processing protocol.
Definition: protocols.h:175