1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "synchronizeable_classid_list.h" |
---|
18 | #include "converter.h" |
---|
19 | #include <cassert> |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * standard constructor |
---|
24 | * @todo this constructor is not jet implemented - do it |
---|
25 | */ |
---|
26 | SynchronizeableClassIDList::SynchronizeableClassIDList( std::map< std::string, int > * ptrIn, std::map< std::string, int > * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority ) |
---|
27 | { |
---|
28 | this->vPtrIn = ptrIn; |
---|
29 | this->vPtrOut = ptrOut; |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | /** |
---|
34 | * standard deconstructor |
---|
35 | */ |
---|
36 | SynchronizeableClassIDList::~SynchronizeableClassIDList () |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * write var data to byte buffer |
---|
42 | * @param buf pointer to write to |
---|
43 | * @param maxLength writeToBuf will not write more than maxLength bytes |
---|
44 | * @return number bytes written |
---|
45 | */ |
---|
46 | int SynchronizeableClassIDList::writeToBuf( byte * buf, int maxLength ) |
---|
47 | { |
---|
48 | int res = 0; |
---|
49 | int n; |
---|
50 | |
---|
51 | std::map< std::string, int >::const_iterator it; |
---|
52 | |
---|
53 | n = Converter::intToByteArray( vPtrIn->size(), buf, maxLength ); |
---|
54 | assert( n == INTSIZE ); |
---|
55 | res += n; |
---|
56 | |
---|
57 | for ( it = vPtrIn->begin(); it != vPtrIn->end(); it++ ) |
---|
58 | { |
---|
59 | n = Converter::stringToByteArray( it->first, buf+res, maxLength-res ); |
---|
60 | assert( n == (int)it->first.length()+INTSIZE ); |
---|
61 | res += n; |
---|
62 | |
---|
63 | n = Converter::intToByteArray( it->second, buf+res, maxLength-res ); |
---|
64 | assert( n == INTSIZE ); |
---|
65 | res += n; |
---|
66 | } |
---|
67 | |
---|
68 | return res; |
---|
69 | } |
---|
70 | |
---|
71 | int SynchronizeableClassIDList::getSize() |
---|
72 | { |
---|
73 | int res = 0; |
---|
74 | |
---|
75 | std::map< std::string, int >::const_iterator it; |
---|
76 | |
---|
77 | res += INTSIZE; |
---|
78 | |
---|
79 | for ( it = vPtrIn->begin(); it != vPtrIn->end(); it++ ) |
---|
80 | { |
---|
81 | res += it->first.length()+2*INTSIZE; |
---|
82 | } |
---|
83 | |
---|
84 | return res; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * read var data from byte buffer |
---|
89 | * @param buf pointer to read from |
---|
90 | * @param maxLength readFromBuf will not read more than maxLength bytes |
---|
91 | * @return number bytes read |
---|
92 | */ |
---|
93 | int SynchronizeableClassIDList::readFromBuf( byte * buf, int maxLength ) |
---|
94 | { |
---|
95 | setHasChanged( false ); |
---|
96 | |
---|
97 | int res = 0; |
---|
98 | int num; |
---|
99 | |
---|
100 | assert( maxLength >= INTSIZE ); |
---|
101 | int n = Converter::byteArrayToInt( buf, &num ); |
---|
102 | assert( n == INTSIZE ); |
---|
103 | res += n; |
---|
104 | |
---|
105 | int id; |
---|
106 | std::string name; |
---|
107 | |
---|
108 | for ( int i = 0; i < num; i++ ) |
---|
109 | { |
---|
110 | n = Converter::byteArrayToString( buf+res, name, maxLength-res ); |
---|
111 | assert( n > 0 ); |
---|
112 | res += n; |
---|
113 | |
---|
114 | n = Converter::byteArrayToInt( buf+res, &id ); |
---|
115 | assert( n == INTSIZE ); |
---|
116 | res += n; |
---|
117 | |
---|
118 | if ( vPtrOut->find(name) == vPtrOut->end() ) |
---|
119 | setHasChanged( true ); |
---|
120 | else |
---|
121 | { |
---|
122 | if ( (*vPtrOut)[name] != id ) |
---|
123 | { |
---|
124 | setHasChanged( true ); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | (*vPtrOut)[name] = id; |
---|
129 | } |
---|
130 | |
---|
131 | return res; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * print out variable value |
---|
136 | */ |
---|
137 | void SynchronizeableClassIDList::debug( ) |
---|
138 | { |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * reads actual size from buffer. this is used when size is not constant |
---|
145 | * @param buf pointer to data |
---|
146 | * @param maxLength maximal size of data |
---|
147 | * @return same as readFromBuf would return |
---|
148 | */ |
---|
149 | int SynchronizeableClassIDList::getSizeFromBuf( byte * buf, int maxLength ) |
---|
150 | { |
---|
151 | int res = 0; |
---|
152 | int num; |
---|
153 | |
---|
154 | assert( maxLength >= INTSIZE ); |
---|
155 | int n = Converter::byteArrayToInt( buf, &num ); |
---|
156 | assert( n == INTSIZE ); |
---|
157 | res += n; |
---|
158 | |
---|
159 | int id; |
---|
160 | std::string name; |
---|
161 | |
---|
162 | for ( int i = 0; i < num; i++ ) |
---|
163 | { |
---|
164 | n = Converter::byteArrayToString( buf+res, name, maxLength-res ); |
---|
165 | assert( n > 0 ); |
---|
166 | res += n; |
---|
167 | |
---|
168 | n = Converter::byteArrayToInt( buf+res, &id ); |
---|
169 | assert( n == INTSIZE ); |
---|
170 | res += n; |
---|
171 | } |
---|
172 | |
---|
173 | return res; |
---|
174 | } |
---|
175 | |
---|
176 | |
---|