home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / datapacket.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  5.3 KB  |  231 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library 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 GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef DATAPACKET_H
  24. #define DATAPACKET_H
  25.  
  26. #include "arts_export.h"
  27. #include "buffer.h"
  28.  
  29. /*
  30.  * BC - Status (2002-03-08): GenericDataChannel, DataPacket types
  31.  *
  32.  * These classes must be kept binary compatible, as the do interact with
  33.  * generated code. So you MUST KNOW WHAT YOU ARE DOING, once you start
  34.  * using the provided d pointers for extensions.
  35.  */
  36.  
  37. namespace Arts {
  38. class GenericDataChannelPrivate;
  39. class GenericDataPacket;
  40. /*
  41.  * The GenericDataChannel interface is to be implemented by the flowsystem
  42.  */
  43. class ARTS_EXPORT GenericDataChannel {
  44. private:
  45.     GenericDataChannelPrivate *d;    // unused
  46.  
  47. protected:
  48.     friend class GenericDataPacket;
  49.  
  50.     /*
  51.      * this is used internally by DataPacket
  52.      */
  53.     virtual void processedPacket(GenericDataPacket *packet) = 0;
  54.  
  55.     /*
  56.      * used internally by DataPacket
  57.      */
  58.     virtual void sendPacket(GenericDataPacket *packet) = 0;
  59.  
  60. public:
  61.     /*
  62.      * used to set pull delivery mode
  63.      */
  64.     virtual void setPull(int packets, int capacity) = 0;
  65.     virtual void endPull() = 0;
  66.  
  67.     GenericDataChannel() : d(0)
  68.     {
  69.     }
  70. };
  71.  
  72. /*
  73.  * DataPackets are the heard of asynchronous streaming (MCOP has synchronous
  74.  * and asynchronous streams). They are used
  75.  *
  76.  *  - in the interface async streams expose to C++ implementations of MCOP
  77.  *    interfaces (they directly deal with datapackets)
  78.  *
  79.  *  - from the FlowSystem implemenentations
  80.  */
  81.  
  82. /**
  83.  * The GenericDataPacket class provides the interface the flow system can
  84.  * use to deal with data packets.
  85.  */
  86. class GenericDataPacketPrivate;
  87.  
  88. class ARTS_EXPORT GenericDataPacket {
  89. private:
  90.     GenericDataPacketPrivate *d;
  91.     static long _staticDataPacketCount;
  92.  
  93. public:
  94.     /**
  95.      * the amount of active data packets (memory leak debugging only)
  96.      */
  97.     static long _dataPacketCount() { return _staticDataPacketCount; }
  98.  
  99.     /**
  100.      * the channel this datapacket belongs to
  101.      */
  102.     GenericDataChannel *channel;
  103.  
  104.     /**
  105.      * ensureCapactity ensures that there is room for at least capacity
  106.      * Elements in the packet. This is implemented destructive - that
  107.      * means: you may not find your old contents in the packet after
  108.      * calling ensureCapacity
  109.      */
  110.     virtual void ensureCapacity(int capacity) = 0;
  111.  
  112.     /**
  113.      * read/write write the contents of the packet. Read will also
  114.      * automatically ensure that capacity is adapted before reading.
  115.      */
  116.     virtual void read(Buffer& stream) = 0;
  117.     virtual void write(Buffer& stream) = 0;
  118.  
  119.     /** 
  120.      * having size here (and not in the derived concrete DataPackets) is so
  121.      * that we can see whether the sender can't supply more data (and starts
  122.      * sending zero size packets
  123.      */
  124.     int size;
  125.  
  126.     /**
  127.      * useCount is to be set from sendPacket
  128.      */
  129.     int useCount;
  130.  
  131.     inline void send()
  132.     {
  133.         channel->sendPacket(this);
  134.     }
  135.     inline void processed()
  136.     {
  137.         useCount--;
  138.         if(useCount == 0)
  139.         {
  140.             if(channel)
  141.                 channel->processedPacket(this);
  142.             else
  143.                 delete this;
  144.         }
  145.     }
  146.  
  147.     virtual ~GenericDataPacket()
  148.     {
  149.         _staticDataPacketCount--;
  150.     }
  151.  
  152. protected:
  153.     GenericDataPacket(GenericDataChannel *channel)
  154.         :d(0),channel(channel),useCount(0)
  155.     {
  156.         _staticDataPacketCount++;
  157.     }
  158. };
  159.  
  160. /**
  161.  * The DataPacket<T> interface is what C++ implementations of MCOP interfaces
  162.  * will need to use.
  163.  */
  164. template<class T>
  165. class DataPacket : public GenericDataPacket {
  166. public:
  167.     T *contents;
  168.  
  169. protected:    
  170.     DataPacket(GenericDataChannel *channel)
  171.         : GenericDataPacket(channel) {}
  172.     ~DataPacket() {}
  173. };
  174.  
  175. /**
  176.  * The RawDataPacket<T> interface handles raw class T arrays of data
  177.  */
  178. template<class T>
  179. class RawDataPacket : public DataPacket<T> {
  180. protected:
  181.     int capacity;
  182.     void ensureCapacity(int newCapacity)
  183.     {
  184.         if(newCapacity > capacity)
  185.         {
  186.             delete[] this->contents;
  187.             capacity = newCapacity;
  188.             this->contents = new T[capacity];
  189.         }
  190.     }
  191.     RawDataPacket(int capacity, GenericDataChannel *channel)
  192.         :DataPacket<T>(channel), capacity(capacity)
  193.     {
  194.         this->size = capacity;
  195.         this->contents = new T[capacity];
  196.     }
  197.     ~RawDataPacket()
  198.     {
  199.         delete[] this->contents;
  200.     }
  201. };
  202.  
  203. /**
  204.  * FloatDataPacket finally is one concrete DataPacket (which contains the
  205.  * information how to marshal a datapacket of type float)
  206.  */
  207. class ARTS_EXPORT FloatDataPacket : public RawDataPacket<float> {
  208. public:
  209.     FloatDataPacket(int capacity, GenericDataChannel *channel)
  210.             : RawDataPacket<float>(capacity, channel)
  211.     {
  212.         //
  213.     }
  214.     void read(Buffer& stream);
  215.     void write(Buffer& stream);
  216. };
  217.  
  218. class ARTS_EXPORT ByteDataPacket : public RawDataPacket<mcopbyte> {
  219. public:
  220.     ByteDataPacket(int capacity, GenericDataChannel *channel)
  221.             : RawDataPacket<mcopbyte>(capacity, channel)
  222.     {
  223.         //
  224.     }
  225.     void read(Buffer& stream);
  226.     void write(Buffer& stream);
  227. };
  228.  
  229. }
  230. #endif
  231.