00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Fennel_AttributeAccessorImpl_Included
00025 #define Fennel_AttributeAccessorImpl_Included
00026
00027 #include "fennel/tuple/AttributeAccessor.h"
00028 #include "fennel/tuple/TupleAccessor.h"
00029 #include "fennel/tuple/TupleData.h"
00030
00031 #ifdef __MSVC__
00032 #include <winsock2.h>
00033 #else
00034 #include <netinet/in.h>
00035 #endif
00036
00037 FENNEL_BEGIN_NAMESPACE
00038
00043 class FENNEL_TUPLE_EXPORT AttributeAccessorImpl
00044 : public AttributeAccessor
00045 {
00046 public:
00047 explicit AttributeAccessorImpl();
00048
00060 bool unmarshalNullableValue(
00061 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00062 {
00063 if (tupleAccessor.getBitFields()[iNullBit]) {
00064 value.pData = NULL;
00065 return true;
00066 } else {
00067 return false;
00068 }
00069 }
00070
00071 void marshalValueData(
00072 PBuffer pDestData,
00073 TupleDatum const &value) const
00074 {
00075 memcpy(pDestData,value.pData,value.cbData);
00076 }
00077 };
00078
00082 class FENNEL_TUPLE_EXPORT FixedWidthAccessor
00083 : public AttributeAccessorImpl
00084 {
00085 public:
00086 void unmarshalValue(
00087 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00088 {
00089 value.pData = tupleAccessor.getCurrentTupleBuf() + iFixedOffset;
00090 }
00091 };
00092
00097 class FENNEL_TUPLE_EXPORT FixedWidthNetworkAccessor16
00098 : public FixedWidthAccessor
00099 {
00100 public:
00101 void unmarshalValue(
00102 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00103 {
00104 assert(value.cbData == sizeof(uint16_t));
00105 FixedWidthAccessor::unmarshalValue(tupleAccessor,value);
00106 value.data16 = ntohs(*reinterpret_cast<uint16_t const *>(value.pData));
00107 value.pData = reinterpret_cast<PConstBuffer>(&(value.data16));
00108 }
00109
00110 void marshalValueData(
00111 PBuffer pDestData,
00112 TupleDatum const &value) const
00113 {
00114 assert(value.cbData == sizeof(uint16_t));
00115 *reinterpret_cast<uint16_t *>(pDestData) =
00116 htons(*reinterpret_cast<uint16_t const *>(value.pData));
00117 }
00118 };
00119
00124 class FENNEL_TUPLE_EXPORT FixedWidthNetworkAccessor32
00125 : public FixedWidthAccessor
00126 {
00127 public:
00128 void unmarshalValue(
00129 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00130 {
00131 assert(value.cbData == sizeof(uint32_t));
00132 FixedWidthAccessor::unmarshalValue(tupleAccessor,value);
00133 value.data32 = ntohl(*reinterpret_cast<uint32_t const *>(value.pData));
00134 value.pData = reinterpret_cast<PConstBuffer>(&(value.data32));
00135 }
00136
00137 void marshalValueData(
00138 PBuffer pDestData,
00139 TupleDatum const &value) const
00140 {
00141 assert(value.cbData == sizeof(uint32_t));
00142 *reinterpret_cast<uint32_t *>(pDestData) =
00143 htonl(*reinterpret_cast<uint32_t const *>(value.pData));
00144 }
00145 };
00146
00151 class FENNEL_TUPLE_EXPORT FixedWidthNetworkAccessor64
00152 : public FixedWidthAccessor
00153 {
00154 public:
00155 void unmarshalValue(
00156 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00157 {
00158 assert(value.cbData == sizeof(uint64_t));
00159 FixedWidthAccessor::unmarshalValue(tupleAccessor,value);
00160 value.data64 = ntohll(*reinterpret_cast<uint64_t const *>(value.pData));
00161 value.pData = reinterpret_cast<PConstBuffer>(&(value.data64));
00162 }
00163
00164 void marshalValueData(
00165 PBuffer pDestData,
00166 TupleDatum const &value) const
00167 {
00168 assert(value.cbData == sizeof(uint64_t));
00169 *reinterpret_cast<uint64_t *>(pDestData) =
00170 htonll(*reinterpret_cast<uint64_t const *>(value.pData));
00171 }
00172 };
00173
00179 template<bool network>
00180 class FixedOffsetVarWidthAccessor
00181 : public AttributeAccessorImpl
00182 {
00183 public:
00184 void unmarshalValue(
00185 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00186 {
00187 value.pData = tupleAccessor.getCurrentTupleBuf() + iFixedOffset;
00188 TupleAccessor::StoredValueOffset const *pEndOffset =
00189 tupleAccessor.referenceIndirectOffset(iEndIndirectOffset);
00190 uint16_t iEndOffset = *pEndOffset;
00191 if (network) {
00192 iEndOffset = ntohs(iEndOffset);
00193 }
00194 value.cbData = iEndOffset - iFixedOffset;
00195 assert(value.cbData <= cbStorage);
00196 }
00197 };
00198
00203 template<bool network>
00204 class VarOffsetAccessor
00205 : public AttributeAccessorImpl
00206 {
00207 public:
00208 void unmarshalValue(
00209 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00210 {
00211 TupleAccessor::StoredValueOffset const *pEndOffset =
00212 tupleAccessor.referenceIndirectOffset(iEndIndirectOffset);
00213 uint iOffset = pEndOffset[-1];
00214 uint iEndOffset = pEndOffset[0];
00215 if (network) {
00216 iOffset = ntohs(iOffset);
00217 iEndOffset = ntohs(iEndOffset);
00218 }
00219 value.pData = tupleAccessor.getCurrentTupleBuf() + iOffset;
00220 value.cbData = iEndOffset - iOffset;
00221 assert(value.cbData <= cbStorage);
00222 }
00223 };
00224
00228 class FENNEL_TUPLE_EXPORT BitAccessor
00229 : public AttributeAccessorImpl
00230 {
00231 public:
00232 void unmarshalValue(
00233 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00234 {
00235 if (tupleAccessor.getBitFields()[iValueBit]) {
00236 value.pData = reinterpret_cast<PConstBuffer>(
00237 &(TupleAccessor::BOOL_TRUE));
00238 } else {
00239 value.pData = reinterpret_cast<PConstBuffer>(
00240 &(TupleAccessor::BOOL_FALSE));
00241 }
00242 }
00243 };
00244
00245 template <class Accessor>
00246 class NullableAccessor : public Accessor
00247 {
00248 public:
00249 void unmarshalValue(
00250 TupleAccessor const &tupleAccessor,TupleDatum &value) const
00251 {
00252 if (Accessor::unmarshalNullableValue(tupleAccessor,value)) {
00253 return;
00254 }
00255 return Accessor::unmarshalValue(tupleAccessor,value);
00256 }
00257 };
00258
00259 FENNEL_END_NAMESPACE
00260
00261 #endif
00262
00263