00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _HLATYPES_VARIABLEARRAY_HH
00018 #define _HLATYPES_VARIABLEARRAY_HH
00019
00020 #include <HLAbuffer.hh>
00021 #include <HLAbasicType.hh>
00022
00023 namespace libhla {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00047 template<class M, bool hasVariable = M::m_isVariable>
00048 struct HLAvariableArray;
00049
00050 template<class M, bool hasVariable>
00051 std::ostream& PrintBuffer(std::ostream& stream, HLAvariableArray<M,hasVariable>& buffer)
00052 { return __print_buffer(stream, (void*)&buffer, buffer.__sizeof()); }
00053
00054
00055 template<class M>
00056 struct HLAvariableArray<M, false>
00057 {
00059
00060
00061
00062 HLAinteger32BE& size() const
00063 { return (HLAinteger32BE&)*this; }
00064
00066 void set_size(long i)
00067 {
00068 if (i == size())
00069 return;
00070
00071
00072 __HLAbuffer::shake(this, i,
00073 (i-size())*(long)(M::__sizeof() + __padding(M::__sizeof(), M::m_octetBoundary)));
00074 }
00075
00076 static const size_t offset(long i)
00077 { return emptysizeof() + i*(M::__sizeof() + __padding(M::__sizeof(), M::m_octetBoundary)); }
00078
00079 M& operator[](long i) const
00080 {
00081 if (i >= size())
00082 throw std::out_of_range("HLAvariableArray: index out of range");
00083 return *(M*)((char*)this + offset(i));
00084 }
00085
00086 static const size_t emptysizeof()
00087 { return HLAinteger32BE::__sizeof() + __padding(HLAinteger32BE::__sizeof(), M::m_octetBoundary); }
00088
00089
00090 const size_t __sizeof() const
00091 {
00092 if (size() > 0)
00093 return offset(size()-1) + M::__sizeof();
00094 else
00095 return emptysizeof();
00096 }
00097
00098 void copy(void* source)
00099 {
00100 int N = *(HLAinteger32BE*)source;
00101 int toCopy;
00102
00103 __HLAbuffer& buffer = __HLAbuffer::__buffer(this);
00104 if(source == buffer.mShakeThat) {
00105 *(HLAinteger32BE*)this = buffer.mShakeValue;
00106 toCopy = std::min(N, buffer.mShakeValue);
00107 }
00108 else {
00109 *(HLAinteger32BE*)this = N;
00110 toCopy = N;
00111 }
00112
00113 size_t offs = emptysizeof();
00114
00115 for (int i = 0; i < toCopy; i++) {
00116 ((M*)((char*)this + offs))->copy((char*)source + offs);
00117 offs += M::__sizeof() + __padding(M::__sizeof(), M::m_octetBoundary);
00118 }
00119 }
00120
00121 static const size_t m_octetBoundary =
00122 MAX(HLAinteger32BE::m_octetBoundary, M::m_octetBoundary);
00123 static const bool m_isVariable = true;
00124 };
00125
00126
00127
00128 template<class M>
00129 struct HLAvariableArray<M, true>
00130 {
00132
00133
00134
00135 HLAinteger32BE& size() const
00136 { return (HLAinteger32BE&)*this; }
00137
00139 void set_size(long i)
00140 {
00141 if (i == size())
00142 return;
00143
00144
00145 __HLAbuffer::shake(this, i,
00146 (i-size())*(long)(M::emptysizeof() + __padding(M::emptysizeof(), M::m_octetBoundary)));
00147 }
00148
00149 const size_t offset(long i) const
00150 {
00151 size_t offs = emptysizeof();
00152
00153 for (long j=0; j<i; j++) {
00154 offs += ((M*)((char*)this + offs))->__sizeof();
00155 offs += __padding(offs, M::m_octetBoundary);
00156 }
00157 return offs;
00158 }
00159
00160 M& operator[](long i) const
00161 {
00162 if (i >= size())
00163 throw std::out_of_range("HLAvariableArray: index out of range");
00164 return *(M*)((char*)this + offset(i));
00165 }
00166
00167 static const size_t emptysizeof()
00168 { return HLAinteger32BE::__sizeof() + __padding(HLAinteger32BE::__sizeof(), M::m_octetBoundary); }
00169
00170
00171 const size_t __sizeof() const
00172 {
00173 if (size() > 0) {
00174 size_t offs = offset(size()-1);
00175 return offs + ((M*)((char*)this + offs))->__sizeof();
00176 }
00177 else
00178 return emptysizeof();
00179 }
00180
00181 void copy(void* source)
00182 {
00183 int N = *(HLAinteger32BE*)source;
00184 int toCopy;
00185
00186 __HLAbuffer& buffer = __HLAbuffer::__buffer(this);
00187 if(source == buffer.mShakeThat) {
00188 *(HLAinteger32BE*)this = buffer.mShakeValue;
00189 toCopy = std::min(N, buffer.mShakeValue);
00190 }
00191 else {
00192 *(HLAinteger32BE*)this = N;
00193 toCopy = N;
00194 }
00195
00196 size_t offsD = emptysizeof();
00197 size_t offsS = emptysizeof();
00198
00199 for (int i = 0; i < toCopy; i++) {
00200 ((M*)((char*)this + offsD))->copy((char*)source + offsS);
00201
00202 offsD += ((M*)((char*)this + offsD))->__sizeof();
00203 offsD += __padding(offsD, M::m_octetBoundary);
00204 offsS += ((M*)((char*)source + offsS))->__sizeof();
00205 offsS += __padding(offsS, M::m_octetBoundary);
00206 }
00207 }
00208
00209 static const size_t m_octetBoundary =
00210 MAX(HLAinteger32BE::m_octetBoundary, M::m_octetBoundary);
00211 static const bool m_isVariable = true;
00212 };
00213
00214
00215
00216
00217 struct HLAASCIIstring : public HLAvariableArray<HLAASCIIchar>
00218 {
00219 HLAASCIIstring& operator = (const std::string& it)
00220 {
00221 __HLAbuffer& buffer = __HLAbuffer::__buffer(this);
00222 size_t offset = (char*)this - buffer.mBegin;
00223
00224 set_size(it.size());
00225
00226 memcpy(buffer.mBegin + offset + emptysizeof(), it.data(), it.size());
00227
00228 return *this;
00229 }
00230
00231 operator std::string() const
00232 {
00233 return std::string((char*)this + emptysizeof(), size());
00234 }
00235 };
00236
00237 typedef HLAvariableArray<HLAunicodeChar> HLAunicodeString;
00238 typedef HLAvariableArray<HLAbyte> HLAopaqueData;
00239
00240 }
00241
00242 #endif // _HLATYPES_VARIABLEARRAY_HH
00243
00244
00245