objwriter.h

changeset 29
b4e09ae24bf1
parent 27
15c06a191f9b
child 32
d11a034aabfd
equal deleted inserted replaced
28:fb46d3d40064 29:b4e09ae24bf1
107 for (unsigned int x = 0; x < sizeof (T); x++) { 107 for (unsigned int x = 0; x < sizeof (T); x++) {
108 buffer[writepos] = CharByte<T> (stuff, x); 108 buffer[writepos] = CharByte<T> (stuff, x);
109 writepos++; 109 writepos++;
110 } 110 }
111 } 111 }
112 112
113 template<class T> T Read() { 113 template<class T> T Read() {
114 T result = buffer[readpos]; 114 T result = buffer[readpos];
115 readpos += sizeof (T); 115 readpos += sizeof (T);
116 return result; 116 return result;
117 }
118
119 private:
120 template <class T> unsigned char CharByte (T a, unsigned int b) {
121 if (b >= sizeof (T))
122 error ("CharByte: tried to get byte %u out of a %u-byte %s\n",
123 b, sizeof (T), typeid(T).name());
124
125 unsigned long p1 = pow<unsigned long> (256, b);
126 unsigned long p2 = pow<unsigned long> (256, b+1);
127 unsigned long r = (a % p2) / p1;
128
129 if (r > 256)
130 error ("result %lu too big!", r);
131
132 unsigned char ur = static_cast<unsigned char> (r);
133 return ur;
117 } 134 }
118 }; 135 };
119 136
120 class ObjWriter { 137 class ObjWriter {
121 public: 138 public:

mercurial