00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifndef SIMPLE_PROPERTIES_HPP 00018 #define SIMPLE_PROPERTIES_HPP 00019 00020 #include <ndb_global.h> 00021 #include <NdbOut.hpp> 00022 00036 class SimpleProperties { 00037 public: 00041 enum ValueType { 00042 Uint32Value = 0, 00043 StringValue = 1, 00044 BinaryValue = 2, 00045 InvalidValue = 3 00046 }; 00047 00051 struct SP2StructMapping { 00052 Uint16 Key; 00053 Uint32 Offset; 00054 ValueType Type; 00055 Uint32 minValue; 00056 Uint32 maxValue; 00057 Uint32 Length_Offset; // Offset used for looking up length of 00058 // data if Type = BinaryValue 00059 }; 00060 00064 enum UnpackStatus { 00065 Eof = 0, // Success, end of SimpleProperties object reached 00066 Break = 1, // Success 00067 TypeMismatch = 2, 00068 ValueTooLow = 3, 00069 ValueTooHigh = 4, 00070 UnknownKey = 5, 00071 OutOfMemory = 6 // Only used when packing 00072 }; 00073 00077 class Reader; 00078 static UnpackStatus unpack(class Reader & it, 00079 void * dst, 00080 const SP2StructMapping[], Uint32 mapSz, 00081 bool ignoreMinMax, 00082 bool ignoreUnknownKeys); 00083 00084 class Writer; 00085 static UnpackStatus pack(class Writer &, 00086 const void * src, 00087 const SP2StructMapping[], Uint32 mapSz, 00088 bool ignoreMinMax); 00089 00093 class Reader { 00094 public: 00095 virtual ~Reader() {} 00096 00101 bool first(); 00102 00107 bool next(); 00108 00112 bool valid() const; 00113 00118 Uint16 getKey() const; 00119 00124 Uint16 getValueLen() const; 00125 00130 ValueType getValueType() const; 00131 00136 Uint32 getUint32() const; 00137 char * getString(char * dst) const; 00138 00142 void printAll(NdbOut& ndbout); 00143 00144 private: 00145 bool readValue(); 00146 00147 Uint16 m_key; 00148 Uint16 m_itemLen; 00149 union { 00150 Uint32 m_ui32_value; 00151 Uint32 m_strLen; // Including 0-byte in words 00152 }; 00153 ValueType m_type; 00154 protected: 00155 Reader(); 00156 virtual void reset() = 0; 00157 00158 virtual bool step(Uint32 len) = 0; 00159 virtual bool getWord(Uint32 * dst) = 0; 00160 virtual bool peekWord(Uint32 * dst) const = 0; 00161 virtual bool peekWords(Uint32 * dst, Uint32 len) const = 0; 00162 }; 00163 00167 class Writer { 00168 public: 00169 Writer() {} 00170 virtual ~Writer() {} 00171 00172 bool first(); 00173 bool add(Uint16 key, Uint32 value); 00174 bool add(Uint16 key, const char * value); 00175 bool add(Uint16 key, const void* value, int len); 00176 protected: 00177 virtual bool reset() = 0; 00178 virtual bool putWord(Uint32 val) = 0; 00179 virtual bool putWords(const Uint32 * src, Uint32 len) = 0; 00180 private: 00181 bool add(const char* value, int len); 00182 }; 00183 }; 00184 00188 class SimplePropertiesLinearReader : public SimpleProperties::Reader { 00189 public: 00190 SimplePropertiesLinearReader(const Uint32 * src, Uint32 len); 00191 virtual ~SimplePropertiesLinearReader() {} 00192 00193 virtual void reset(); 00194 virtual bool step(Uint32 len); 00195 virtual bool getWord(Uint32 * dst); 00196 virtual bool peekWord(Uint32 * dst) const ; 00197 virtual bool peekWords(Uint32 * dst, Uint32 len) const; 00198 private: 00199 Uint32 m_len; 00200 Uint32 m_pos; 00201 const Uint32 * m_src; 00202 }; 00203 00207 class LinearWriter : public SimpleProperties::Writer { 00208 public: 00209 LinearWriter(Uint32 * src, Uint32 len); 00210 virtual ~LinearWriter() {} 00211 00212 virtual bool reset(); 00213 virtual bool putWord(Uint32 val); 00214 virtual bool putWords(const Uint32 * src, Uint32 len); 00215 Uint32 getWordsUsed() const; 00216 private: 00217 Uint32 m_len; 00218 Uint32 m_pos; 00219 Uint32 * m_src; 00220 }; 00221 00225 class UtilBufferWriter : public SimpleProperties::Writer { 00226 public: 00227 UtilBufferWriter(class UtilBuffer & buf); 00228 virtual ~UtilBufferWriter() {} 00229 00230 virtual bool reset(); 00231 virtual bool putWord(Uint32 val); 00232 virtual bool putWords(const Uint32 * src, Uint32 len); 00233 Uint32 getWordsUsed() const; 00234 private: 00235 class UtilBuffer & m_buf; 00236 }; 00237 00244 class SimplePropertiesSectionReader : public SimpleProperties::Reader { 00245 public: 00246 SimplePropertiesSectionReader(struct SegmentedSectionPtr &, 00247 class SectionSegmentPool &); 00248 virtual ~SimplePropertiesSectionReader() {} 00249 00250 00251 virtual void reset(); 00252 virtual bool step(Uint32 len); 00253 virtual bool getWord(Uint32 * dst); 00254 virtual bool peekWord(Uint32 * dst) const ; 00255 virtual bool peekWords(Uint32 * dst, Uint32 len) const; 00256 Uint32 getSize() const; 00257 bool getWords(Uint32 * dst, Uint32 len); 00258 00259 private: 00260 Uint32 m_pos; 00261 Uint32 m_len; 00262 class SectionSegmentPool & m_pool; 00263 struct SectionSegment * m_head; 00264 struct SectionSegment * m_currentSegment; 00265 }; 00266 00267 inline 00268 Uint32 SimplePropertiesSectionReader::getSize() const 00269 { 00270 return m_len; 00271 } 00272 00279 class SimplePropertiesSectionWriter : public SimpleProperties::Writer { 00280 public: 00281 SimplePropertiesSectionWriter(class SectionSegmentPool &); 00282 virtual ~SimplePropertiesSectionWriter() {} 00283 00284 virtual bool reset(); 00285 virtual bool putWord(Uint32 val); 00286 virtual bool putWords(const Uint32 * src, Uint32 len); 00287 00291 void getPtr(struct SegmentedSectionPtr & dst); 00292 00293 private: 00294 Int32 m_pos; 00295 Uint32 m_sz; 00296 class SectionSegmentPool & m_pool; 00297 struct SectionSegment * m_head; 00298 Uint32 m_prevPtrI; // Prev to m_currentSegment 00299 struct SectionSegment * m_currentSegment; 00300 }; 00301 00302 #endif
1.4.7

