Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/Synchronisable.cc @ 238

Last change on this file since 238 was 238, checked in by dumenim, 16 years ago

changed Synchronise.cc, compiles now

File size: 2.0 KB
Line 
1//
2// C++ Implementation: synchronisable
3//
4// Description:
5//
6//
7// Author:  Oliver Scheuss, (C) 2007
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11
12#include "network/Synchronisable.h"
13
14namespace network {
15
16Synchronisable::Synchronisable()
17{
18  registerAllVariables();
19}
20
21
22Synchronisable::~Synchronisable()
23{
24 
25}
26
27void Synchronisable::registerVar(const void *var, int size){
28  synchronisableVariable temp={size, var};
29  syncList.push_back(temp);
30}
31syncData Synchronisable::getData(){
32  std::list<synchronisableVariable>::iterator i;
33  int totalsize=0;
34  //figure out size of data to be allocated
35  for(i=syncList.begin(); i!=syncList.end(); i++){
36    // increase size (size of variable and size of size of variable ;)
37    totalsize+=sizeof(int)+i->size;
38  }
39  syncData retVal;
40  retVal.objectID=this->objectID;
41  retVal.classID=this->classID;
42  retVal.length=totalsize;
43  // allocate memory
44  retVal.data = (unsigned char *)malloc(totalsize);
45  // copy to location
46  //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
47  int n=0;
48  for(i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){
49        //CHANGED: i->size TO (const void*)(&(i->size)) memcpy WANTS A CONST VOID* SO CONVERT INT TO CONST VOID* 
50    std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
51    n+=sizeof(int);
52    //CHANGED: i->var TO (const void*)(&(i->var)) SINCE var IS A POINTER, NO & BEFORE i
53    std::memcpy(retVal.data+n, (const void*)(i->var), i->size);
54    n+=i->size;
55  }
56  return retVal;
57}
58bool Synchronisable::updateData(syncData vars){
59  unsigned char *data=vars.data;
60  std::list<synchronisableVariable>::iterator i;
61  for(i=syncList.begin(); i!=syncList.end(); i++){
62    if((int)*data==i->size){
63      data+=sizeof(int);
64      //CHANGED: THIS FROM i->var TO (void*)i->var SINCE var IS A CONST VOID* AND memcpy NEEDS A VOID* AS FIRST ARGUMENT
65      memcpy((void*)i->var, data, i->size);
66      data+=i->size;
67    } else
68      return false; //there was some problem with registerVar
69  }
70}
71
72}
Note: See TracBrowser for help on using the repository browser.