00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _HLATYPES_FIXEDARRAY_HH
00018 #define _HLATYPES_FIXEDARRAY_HH
00019
00020 #include <stdexcept>
00021
00022 namespace libhla {
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00044 template<class M, int N, bool hasVariable = M::m_isVariable>
00045 struct HLAfixedArray;
00046
00047 template<class M, int N, bool hasVariable>
00048 std::ostream& PrintBuffer(std::ostream& stream, HLAfixedArray<M,N,hasVariable>& buffer)
00049 { return __print_buffer(stream, (void*)&buffer, buffer.__sizeof()); }
00050
00051
00052
00053 template<class M, int N>
00054 struct HLAfixedArray<M, N, false>
00055 {
00056 static const size_t size()
00057 { return N; }
00058
00059 static const size_t offset(long i)
00060 { return i*(M::__sizeof() + __padding(M::__sizeof(), M::m_octetBoundary)); }
00061
00062 M& operator[](long i) const
00063 {
00064 if (i < 0 || i >= (long)size())
00065 throw std::out_of_range("HLAfixedArray: index out of range");
00066 return *(M*)((char*)this + offset(i));
00067 }
00068
00069 static const size_t emptysizeof()
00070 { return __sizeof(); }
00071
00072
00073 static const size_t __sizeof()
00074 { return offset(N-1) + M::__sizeof(); }
00075
00076 void copy(void* source)
00077 {
00078 size_t offs = 0;
00079
00080 for (size_t i = 0; i < N; i++) {
00081 ((M*)((char*)this + offs))->copy((char*)source + offs);
00082 offs += M::__sizeof() + __padding(M::__sizeof(), M::m_octetBoundary);
00083 }
00084 }
00085
00086 static const size_t m_octetBoundary = M::m_octetBoundary;
00087 static const bool m_isVariable = false;
00088 };
00089
00090
00091 template<class M, int N>
00092 struct HLAfixedArray<M, N, true>
00093 {
00094 static const size_t size()
00095 { return N; }
00096
00097 const size_t offset(long i) const
00098 {
00099 size_t offs = 0;
00100
00101 for (long j=0; j<i; j++) {
00102 offs += ((M*)((char*)this + offs))->__sizeof();
00103 offs += __padding(offs, M::m_octetBoundary);
00104 }
00105 return offs;
00106 }
00107
00108 M& operator[](long i) const
00109 {
00110 if (i >= size())
00111 throw std::out_of_range("HLAfixedArray: index out of range");
00112 return *(M*)((char*)this + offset(i));
00113 }
00114
00115 static const size_t emptysizeof()
00116 {
00117 size_t size = N*M::emptysizeof();
00118
00119 if(N > 1)
00120 size += (N-1)*__padding(M::emptysizeof(), M::m_octetBoundary);
00121 return size;
00122 }
00123
00124 const size_t __sizeof() const
00125 {
00126 size_t offs = offset(N-1);
00127 return offs + ((M*)((char*)this + offs))->__sizeof();
00128 }
00129
00130 void copy(void* source)
00131 {
00132 size_t offsD = 0;
00133 size_t offsS = 0;
00134
00135 for (size_t i = 0; i < N; i++) {
00136 ((M*)((char*)this + offsD))->copy((char*)source + offsS);
00137
00138 offsD += ((M*)((char*)this + offsD))->__sizeof();
00139 offsD += __padding(offsD, M::m_octetBoundary);
00140 offsS += ((M*)((char*)source + offsS))->__sizeof();
00141 offsS += __padding(offsS, M::m_octetBoundary);
00142 }
00143 }
00144
00145 static const size_t m_octetBoundary = M::m_octetBoundary;
00146 static const bool m_isVariable = true;
00147 };
00148
00149 }
00150
00151 #endif // _HLATYPES_FIXEDARRAY_HH
00152
00153
00154