Index: code/trunk/src/libraries/network/synchronisable/Synchronisable.h
===================================================================
--- code/trunk/src/libraries/network/synchronisable/Synchronisable.h	(revision 6417)
+++ code/trunk/src/libraries/network/synchronisable/Synchronisable.h	(revision 7163)
@@ -37,4 +37,5 @@
 #include <map>
 #include <queue>
+#include <set>
 
 #include "util/mbool.h"
@@ -64,4 +65,6 @@
     };
   }
+  
+  typedef uint8_t VariableID;
 
   /**
@@ -71,5 +74,5 @@
    * in an emulated bitset.
    * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
-   * Bit 32 is a bool and defines whether the data is actually stored or is just filled up with 0
+   * Bit 32 is a bool and defines whether the variables are stored in diff mode
    * Byte 5 to 8: objectID_
    * Byte 9 to 12: classID_
@@ -77,35 +80,68 @@
    */
   class _NetworkExport SynchronisableHeader{
+    friend class SynchronisableHeaderLight;
     private:
-      uint8_t *data_;
+      uint8_t* data_;
     public:
       SynchronisableHeader(uint8_t* data)
         { data_ = data; }
       inline static uint32_t getSize()
-        { return 16; }
-      inline uint32_t getDataSize() const
-        { return (*(uint32_t*)data_) & 0x7FFFFFFF; } //only use the first 31 bits
-      inline void setDataSize(uint32_t size)
-        { *(uint32_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint32_t*)(data_) & 0x80000000 ); }
-      inline bool isDataAvailable() const
-        { return ( (*(uint32_t*)data_) & 0x80000000 ) == 0x80000000; }
-      inline void setDataAvailable( bool b)
-        { *(uint32_t*)(data_) = (b << 31) | (*(uint32_t*)(data_) & 0x7FFFFFFF ); }
+        { return 14; }
+      inline uint16_t getDataSize() const
+        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 15 bits
+      inline void setDataSize(uint16_t size)
+        { *(uint16_t*)(data_) = (size & 0x7FFF) | (*(uint16_t*)(data_) & 0x8000 ); }
+      inline bool isDiffed() const
+        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
+      inline void setDiffed( bool b)
+        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
       inline uint32_t getObjectID() const
-        { return *(uint32_t*)(data_+4); }
+        { return *(uint32_t*)(data_+2); }
       inline void setObjectID(uint32_t objectID_)
-        { *(uint32_t*)(data_+4) = objectID_; }
+        { *(uint32_t*)(data_+2) = objectID_; }
       inline uint32_t getClassID() const
-        { return *(uint32_t*)(data_+8); }
+        { return *(uint32_t*)(data_+6); }
       inline void setClassID(uint32_t classID_)
-        { *(uint32_t*)(data_+8) = classID_; }
+        { *(uint32_t*)(data_+6) = classID_; }
       inline uint32_t getCreatorID() const
-        { return *(uint32_t*)(data_+12); }
+        { return *(uint32_t*)(data_+10); }
       inline void setCreatorID(uint32_t creatorID_)
-        { *(uint32_t*)(data_+12) = creatorID_; }
+        { *(uint32_t*)(data_+10) = creatorID_; }
       inline void operator=(SynchronisableHeader& h)
         { memcpy(data_, h.data_, getSize()); }
   };
 
+    /**
+   * @brief: stores information about a Synchronisable (light version)
+   *
+   * This class stores the information about a Synchronisable (objectID_, dataSize)
+   * in an emulated bitset.
+   * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream
+   * Bit 32 is a bool and defines whether the variables are stored in diff mode
+   * Byte 5 to 8: objectID_
+   */
+  class _NetworkExport SynchronisableHeaderLight{
+    private:
+      uint8_t* data_;
+    public:
+      SynchronisableHeaderLight(uint8_t* data)
+        { data_ = data; }
+      inline static uint32_t getSize()
+        { return 6; }
+      inline uint16_t getDataSize() const
+        { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 31 bits
+      inline void setDataSize(uint16_t size)
+        { *(uint16_t*)(data_) = (size & 0x7FFFFFFF) | (*(uint16_t*)(data_) & 0x8000 ); }
+      inline bool isDiffed() const
+        { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }
+      inline void setDiffed( bool b)
+        { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }
+      inline uint32_t getObjectID() const
+        { return *(uint32_t*)(data_+2); }
+      inline void setObjectID(uint32_t objectID_)
+        { *(uint32_t*)(data_+2) = objectID_; }
+      inline void operator=(SynchronisableHeader& h)
+        { memcpy(data_, h.data_, getSize()); }
+  };
 
   /**
@@ -134,8 +170,14 @@
 
     void setSyncMode(uint8_t mode);
+    
+    inline uint32_t getNrOfVariables(){ return this->syncList_.size(); }
+    inline uint32_t getVarSize( VariableID ID )
+    { return this->syncList_[ID]->getSize(state_); }
 
   protected:
     Synchronisable(BaseObject* creator);
     template <class T> void registerVariable(T& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
+    template <class T> void registerVariable(std::set<T>& variable, uint8_t mode=0x1, NetworkCallbackBase *cb=0, bool bidirectional=false);
+    template <class T> void unregisterVariable(T& var);
 
     void setPriority(unsigned int freq){ objectFrequency_ = freq; }
@@ -143,8 +185,7 @@
 
   private:
-    uint32_t getData(uint8_t*& men, int32_t id, uint8_t mode=0x0);
+    uint32_t getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode);
     uint32_t getSize(int32_t id, uint8_t mode=0x0);
     bool updateData(uint8_t*& mem, uint8_t mode=0x0, bool forceCallback=false);
-    bool isMyData(uint8_t* mem);
     bool doSync(int32_t id, uint8_t mode=0x0);
 
@@ -156,6 +197,6 @@
     uint32_t classID_;
 
-    std::vector<SynchronisableVariableBase*> syncList;
-    std::vector<SynchronisableVariableBase*> stringList;
+    std::vector<SynchronisableVariableBase*> syncList_;
+    std::vector<SynchronisableVariableBase*> stringList_;
     uint32_t dataSize_; //size of all variables except strings
     static uint8_t state_; // detemines wheter we are server (default) or client
@@ -171,16 +212,46 @@
     if (bidirectional)
     {
-      syncList.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
-      this->dataSize_ += syncList.back()->getSize(state_);
+      syncList_.push_back(new SynchronisableVariableBidirectional<T>(variable, mode, cb));
+      this->dataSize_ += syncList_.back()->getSize(state_);
     }
     else
     {
-      syncList.push_back(new SynchronisableVariable<T>(variable, mode, cb));
+      syncList_.push_back(new SynchronisableVariable<T>(variable, mode, cb));
       if ( this->state_ == mode )
-        this->dataSize_ += syncList.back()->getSize(state_);
+        this->dataSize_ += syncList_.back()->getSize(state_);
     }
   }
+  
+  template <class T> void Synchronisable::unregisterVariable(T& variable){
+    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
+    while(it!=syncList_.end()){
+      if( ((*it)->getReference()) == &variable ){
+        this->dataSize_ -= (*it)->getSize(Synchronisable::state_);
+        delete (*it);
+        syncList_.erase(it);
+        return;
+      }
+      else
+        it++;
+    }
+    bool unregistered_nonexistent_variable = false;
+    assert(unregistered_nonexistent_variable); //if we reach this point something went wrong:
+    // the variable has not been registered before
+  }
+
+  template <class T> void Synchronisable::registerVariable( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
+  {
+    SynchronisableVariableBase* sv;
+    if (bidirectional)
+      sv = new SynchronisableVariableBidirectional<std::set<T> >(variable, mode, cb);
+    else
+      sv = new SynchronisableVariable<std::set<T> >(variable, mode, cb);
+    syncList_.push_back(sv);
+    stringList_.push_back(sv);
+  }
 
   template <> _NetworkExport void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
+//   template <class T> _NetworkExport void Synchronisable::registerVariable<std::set<T> >( std::set<T>& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional);
+  template <> _NetworkExport void Synchronisable::unregisterVariable( std::string& variable );
 
 
