Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

Committer:
mazgch
Date:
Sat Nov 09 13:31:01 2013 +0000
Revision:
7:9aa830f5811e
Parent:
5:5362073f2689
Child:
8:2435cdff8015
fix parsing problems in pipe

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 0:cb2d45baaca3 1 #pragma once
mazgch 0:cb2d45baaca3 2
mazgch 0:cb2d45baaca3 3 template <class T>
mazgch 0:cb2d45baaca3 4 class Pipe
mazgch 0:cb2d45baaca3 5 {
mazgch 0:cb2d45baaca3 6 private:
mazgch 0:cb2d45baaca3 7 inline int _inc(int i, int n = 1)
mazgch 0:cb2d45baaca3 8 {
mazgch 0:cb2d45baaca3 9 i += n;
mazgch 0:cb2d45baaca3 10 if (i >= s)
mazgch 0:cb2d45baaca3 11 i -= s;
mazgch 0:cb2d45baaca3 12 return i;
mazgch 0:cb2d45baaca3 13 }
mazgch 0:cb2d45baaca3 14 public:
mazgch 5:5362073f2689 15 Pipe(int n, T* p = NULL)
mazgch 0:cb2d45baaca3 16 {
mazgch 5:5362073f2689 17 a = p ? NULL : new T[n];
mazgch 0:cb2d45baaca3 18 r = 0;
mazgch 0:cb2d45baaca3 19 w = 0;
mazgch 5:5362073f2689 20 b = p ? p : a;
mazgch 0:cb2d45baaca3 21 s = n;
mazgch 0:cb2d45baaca3 22 }
mazgch 0:cb2d45baaca3 23 virtual ~Pipe()
mazgch 0:cb2d45baaca3 24 {
mazgch 5:5362073f2689 25 if (a)
mazgch 5:5362073f2689 26 delete [] a;
mazgch 0:cb2d45baaca3 27 }
mazgch 0:cb2d45baaca3 28 // writing thread
mazgch 0:cb2d45baaca3 29 bool writeable(void) // = not full
mazgch 0:cb2d45baaca3 30 {
mazgch 0:cb2d45baaca3 31 int i = _inc(w);
mazgch 0:cb2d45baaca3 32 return (i = r);
mazgch 0:cb2d45baaca3 33 }
mazgch 0:cb2d45baaca3 34 int free(void) // number of elements that can be added
mazgch 0:cb2d45baaca3 35 {
mazgch 0:cb2d45baaca3 36 int t = r - w;
mazgch 0:cb2d45baaca3 37 if (t <= 0)
mazgch 0:cb2d45baaca3 38 t += s;
mazgch 0:cb2d45baaca3 39 return t - 1;
mazgch 0:cb2d45baaca3 40 }
mazgch 0:cb2d45baaca3 41 void putc(T c)
mazgch 0:cb2d45baaca3 42 {
mazgch 0:cb2d45baaca3 43 int i = w;
mazgch 0:cb2d45baaca3 44 int j = i;
mazgch 0:cb2d45baaca3 45 i = _inc(i);
mazgch 0:cb2d45baaca3 46 //assert(i != r);
mazgch 0:cb2d45baaca3 47 b[j] = c;
mazgch 0:cb2d45baaca3 48 w = i;
mazgch 0:cb2d45baaca3 49 }
mazgch 0:cb2d45baaca3 50 int put(T* p, int n)
mazgch 0:cb2d45baaca3 51 {
mazgch 0:cb2d45baaca3 52 int f = free();
mazgch 0:cb2d45baaca3 53 if (f < n)
mazgch 0:cb2d45baaca3 54 n = f;
mazgch 0:cb2d45baaca3 55 if (n)
mazgch 0:cb2d45baaca3 56 {
mazgch 0:cb2d45baaca3 57 int m = s - w;
mazgch 0:cb2d45baaca3 58 if (m > n)
mazgch 0:cb2d45baaca3 59 m = n;
mazgch 0:cb2d45baaca3 60 memcpy(&b[w], &p[0], m);
mazgch 0:cb2d45baaca3 61 int t = n - m;
mazgch 0:cb2d45baaca3 62 if (t)
mazgch 0:cb2d45baaca3 63 {
mazgch 0:cb2d45baaca3 64 memcpy(&b[0], &p[m], t);
mazgch 0:cb2d45baaca3 65 w = t;
mazgch 0:cb2d45baaca3 66 }
mazgch 0:cb2d45baaca3 67 else
mazgch 7:9aa830f5811e 68 w = _inc(w,m);
mazgch 7:9aa830f5811e 69 }
mazgch 0:cb2d45baaca3 70 return n;
mazgch 0:cb2d45baaca3 71 }
mazgch 0:cb2d45baaca3 72 // reading thread
mazgch 0:cb2d45baaca3 73 // --------------------------------------------------------
mazgch 0:cb2d45baaca3 74 //! check if there are any values available
mazgch 0:cb2d45baaca3 75 bool readable(void) // = not empty
mazgch 0:cb2d45baaca3 76 {
mazgch 0:cb2d45baaca3 77 return (r != w);
mazgch 0:cb2d45baaca3 78 }
mazgch 0:cb2d45baaca3 79 //! get the number of values avialable in the buffer
mazgch 2:b6012cd91657 80 virtual int size(void)
mazgch 0:cb2d45baaca3 81 {
mazgch 0:cb2d45baaca3 82 int t = w - r;
mazgch 0:cb2d45baaca3 83 if (t < 0)
mazgch 0:cb2d45baaca3 84 t += s;
mazgch 0:cb2d45baaca3 85 return t;
mazgch 0:cb2d45baaca3 86 }
mazgch 0:cb2d45baaca3 87 //! get a value from buffer (this function will block if no values available)
mazgch 0:cb2d45baaca3 88 T getc(void)
mazgch 0:cb2d45baaca3 89 {
mazgch 0:cb2d45baaca3 90 //while (r == w)
mazgch 0:cb2d45baaca3 91 // /* just wait until w changes*/;
mazgch 0:cb2d45baaca3 92 T t = b[r];
mazgch 0:cb2d45baaca3 93 r = _inc(r);
mazgch 0:cb2d45baaca3 94 return t;
mazgch 0:cb2d45baaca3 95 }
mazgch 0:cb2d45baaca3 96 // get values from buffer (if the buffer has less values, only the values avilable are returned)
mazgch 2:b6012cd91657 97 virtual int get(T* p, int n)
mazgch 0:cb2d45baaca3 98 {
mazgch 0:cb2d45baaca3 99 int f = size();
mazgch 0:cb2d45baaca3 100 if (f < n)
mazgch 0:cb2d45baaca3 101 n = f;
mazgch 0:cb2d45baaca3 102 if (n)
mazgch 0:cb2d45baaca3 103 {
mazgch 0:cb2d45baaca3 104 int m = s - r;
mazgch 0:cb2d45baaca3 105 if (m > n)
mazgch 0:cb2d45baaca3 106 m = n;
mazgch 0:cb2d45baaca3 107 memcpy(&p[0], &b[r], m);
mazgch 0:cb2d45baaca3 108 int t = n - m;
mazgch 0:cb2d45baaca3 109 if (t)
mazgch 0:cb2d45baaca3 110 {
mazgch 0:cb2d45baaca3 111 memcpy(&p[m] ,&b[0], t);
mazgch 0:cb2d45baaca3 112 r = t;
mazgch 0:cb2d45baaca3 113 }
mazgch 0:cb2d45baaca3 114 else
mazgch 7:9aa830f5811e 115 r = _inc(r,m);
mazgch 0:cb2d45baaca3 116 }
mazgch 0:cb2d45baaca3 117 return n;
mazgch 0:cb2d45baaca3 118 }
mazgch 0:cb2d45baaca3 119 // the following functions are useful if you like to inspect or parse the buffer
mazgch 2:b6012cd91657 120 virtual int start(void) { o = r; return size(); } // reset the parsing index and return the number of available elments
mazgch 2:b6012cd91657 121 virtual T next(void) { T t = b[o]; o = _inc(o); return t; } // get the next element and increment
mazgch 2:b6012cd91657 122 virtual void done(void) { r = o; } // commit the index
mazgch 0:cb2d45baaca3 123
mazgch 0:cb2d45baaca3 124 private:
mazgch 0:cb2d45baaca3 125 // buffer
mazgch 0:cb2d45baaca3 126 T* b; //!< buffer
mazgch 5:5362073f2689 127 T* a; //!< allocated buffer
mazgch 0:cb2d45baaca3 128 int s; //!< size of buffer (s - 1) elements can be stored
mazgch 0:cb2d45baaca3 129 volatile int w; //! write index
mazgch 0:cb2d45baaca3 130 volatile int r; //! read index
mazgch 0:cb2d45baaca3 131 int o; //! offest index used by parsing functions
mazgch 0:cb2d45baaca3 132 };