home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example6 / DataBlock_STL.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  3.2 KB  |  139 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. #ifndef __DATABLOCK_STL_H__
  12. #define __DATABLOCK_STL_H__
  13.  
  14. #include "DataBlock_GenericSTLVirtual.h"
  15.  
  16. /**
  17.  * This DataBlock derived class scans the registered area for changes and if needed creates a packet detailing those changes<br>
  18.  * This DataBlock does not do any lag compensation or prediction. It just informs each replica of a change when one is made.
  19.  */
  20. template <class T> class DataBlock_STL : public DataBlock_GenericSTLVirtual
  21. {
  22. public:
  23.     /**
  24.      * The ctor performs some basic initialisation
  25.      */
  26.     DataBlock_STL(){}
  27.  
  28.     /**
  29.      * The dtor makes sure everything is tidy
  30.      */
  31.     virtual ~DataBlock_STL(){}
  32.  
  33.     /**
  34.      * This registers the template class area with this data block type and returns a new pointer
  35.      * \param data the start of the memory to check
  36.      * \return Returns a pointer that can be used to attach to a ReplicaObject
  37.      */
  38.     static DataBlock_STL *Register(T *const data)
  39.     {
  40.         DataBlock_STL<T> *object = new DataBlock_STL<T>();
  41.  
  42.         object->mData = data;
  43.  
  44.         return object;
  45.     }
  46.  
  47.     /**
  48.      * This registers the memory area with this data block type. This is used when the memory is in datablocks that are allocated as part of a bigger class.
  49.      * \param data the start of the memory to check
  50.      * \param size the size of the data block
  51.      */
  52.     void RegisterInClassPointer(T *const data)
  53.     {
  54.         mData = (void *) data;
  55.     }
  56.  
  57.     bool IsAttached(T *const data)
  58.     {
  59.         if (!data)
  60.         {
  61.             return false;
  62.         }
  63.         if (mData == data)
  64.         {
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69.  
  70. protected:
  71.     /**
  72.      * This gets the number of elements in the STL container
  73.      * \return The number of elements in the STL container
  74.      */
  75.     int GetNumberOfElements(void)
  76.     {
  77.         T *theData = (T *) mData;
  78.         return (int) theData->size();
  79.     }
  80.  
  81.     void SetNumberOfElements(const int numElements)
  82.     {
  83.         T *theData = (T *) mData;
  84.         theData->resize(numElements);
  85.     }
  86.  
  87.     /**
  88.      * Gets the element size
  89.      * \return The element size
  90.      */
  91.     int GetElementSize(void) const
  92.     {
  93.         return sizeof(T::value_type);
  94.     }
  95.  
  96.     void *GetElementPointerAt(const int offset)
  97.     {
  98.         int copyOffset = offset;
  99.         T::iterator st,en;
  100.         T *theData = (T *) mData;
  101.  
  102.         st = theData->begin();
  103.         en = theData->end();
  104.  
  105.         // Count up...
  106.         while (copyOffset > 0)
  107.         {
  108.             st++;
  109.             copyOffset--;
  110.         }
  111.  
  112.         T::value_type *data = &(*st);
  113.  
  114.         return data;
  115.     }
  116.  
  117.     void SetElementDataAt(const void *data,const int offset)
  118.     {
  119.         T *theData = (T *) mData;
  120.  
  121.         T::value_type tempData;
  122.         tempData = ((T::value_type *)data)[0];
  123.  
  124.         ((T::value_type *)GetElementPointerAt(offset))[0] = tempData;
  125.     }
  126.  
  127. private:
  128. };
  129.  
  130. /* _RO_DO_REGISTERBLOCK_STL_VAR macro starts here */
  131. #define _RO_DO_REGISTERBLOCK_STL_VAR(x,y)    \
  132.     {\
  133.         DataBlock_STL< ##y > *datablock = 0;    \
  134.         datablock = DataBlock_STL< ##y >::Register(&(basepoint->x));    \
  135.         _RO_DO_SETDATABLOCKVARIABLENAME(x);
  136. /* _RO_DO_REGISTERBLOCK_NDATA_VAR macro ends here */
  137.  
  138. #endif
  139.