Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

Revision:
0:cb2d45baaca3
Child:
2:b6012cd91657
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pipe.h	Sun Oct 20 15:12:52 2013 +0000
@@ -0,0 +1,130 @@
+#pragma once 
+
+template <class T>
+class Pipe
+{
+private:
+    inline int _inc(int i, int n = 1)
+    {
+        i += n;
+        if (i >= s)
+            i -= s;
+        return i;
+    }
+public:
+    Pipe(int n)
+    {
+        r = 0;
+        w = 0;
+        n ++; // we add one more element to be able to identify empty from full
+        b = new T[n];
+        s = n;
+    }    
+    virtual ~Pipe()
+    {
+        delete [] b;
+    }
+    // writing thread
+    bool writeable(void) // = not full
+    {
+        int i = _inc(w);
+        return (i = r);
+    }
+    int free(void)      // number of elements that can be added
+    {
+        int t = r - w;
+        if (t <= 0)
+            t += s;
+        return t - 1;
+    }
+    void putc(T c)
+    {
+        int i = w;
+        int j = i;
+        i = _inc(i);
+        //assert(i != r);
+        b[j] = c;
+        w = i;   
+    }
+    int put(T* p, int n)
+    {
+        int f = free();
+        if (f < n)
+            n = f;
+        if (n)
+        {
+            int m = s - w;
+            if (m > n)
+                m = n;
+            memcpy(&b[w], &p[0], m);
+            int t = n - m;
+            if (t)
+            {
+                memcpy(&b[0], &p[m], t);
+                w = t;
+            }
+            else 
+                w += m; 
+        }
+        return n;
+    }
+    // reading thread 
+    // --------------------------------------------------------
+    //! check if there are any values available
+    bool readable(void) // = not empty
+    {
+        return (r != w);
+    }
+    //! get the number of values avialable in the buffer 
+    int size(void)
+    {
+        int t = w - r;
+        if (t < 0)
+            t += s;
+        return t;
+    }
+    //! get a value from buffer (this function will block if no values available)
+    T getc(void)
+    {
+        //while (r == w)
+        //    /* just wait until w changes*/;
+        T t = b[r];
+        r = _inc(r);
+        return t;
+    }
+    // get values from buffer (if the buffer has less values, only the values avilable are returned)
+    int get(T* p, int n)
+    {
+        int f = size();
+        if (f < n)
+            n = f;
+        if (n)
+        {
+            int m = s - r;
+            if (m > n)
+                m = n;
+            memcpy(&p[0], &b[r], m);
+            int t = n - m;
+            if (t)
+            {
+                memcpy(&p[m] ,&b[0], t);
+                r = t;
+            }
+            else 
+                r += m;
+        }
+        return n;
+    }
+    // the following functions are useful if you like to inspect or parse the buffer
+    int start() { o = r; return size(); }               // reset the parsing index and return the number of available elments
+    T next()    { T t = b[o]; o = _inc(o); return t; }  // get the next element and increment
+    void done() { r = o; }                              // commit the index 
+    
+private:
+    // buffer
+    T*            b; //!< buffer
+    int           s; //!< size of buffer (s - 1) elements can be stored
+    volatile int  w; //! write index 
+    volatile int  r; //! read index 
+    int           o; //! offest index used by parsing functions  
+};