00001 #ifndef __CONFIG_VALUES_HPP 00002 #define __CONFIG_VALUES_HPP 00003 00004 #include <ndb_types.h> 00005 #include <UtilBuffer.hpp> 00006 00007 class ConfigValues { 00008 friend class ConfigValuesFactory; 00009 ConfigValues(Uint32 sz, Uint32 data); 00010 00011 public: 00012 ~ConfigValues(); 00013 00014 enum ValueType { 00015 InvalidType = 0, 00016 IntType = 1, 00017 StringType = 2, 00018 SectionType = 3, 00019 Int64Type = 4 00020 }; 00021 00022 struct Entry { 00023 Uint32 m_key; 00024 ValueType m_type; 00025 union { 00026 Uint32 m_int; 00027 Uint64 m_int64; 00028 const char * m_string; 00029 }; 00030 }; 00031 00032 class ConstIterator { 00033 friend class ConfigValuesFactory; 00034 const ConfigValues & m_cfg; 00035 public: 00036 Uint32 m_currentSection; 00037 ConstIterator(const ConfigValues&c) : m_cfg(c) { m_currentSection = 0;} 00038 00039 bool openSection(Uint32 key, Uint32 no); 00040 bool closeSection(); 00041 00042 bool get(Uint32 key, Entry *) const; 00043 00044 bool get(Uint32 key, Uint32 * value) const; 00045 bool get(Uint32 key, Uint64 * value) const; 00046 bool get(Uint32 key, const char ** value) const; 00047 bool getTypeOf(Uint32 key, ValueType * type) const; 00048 00049 Uint32 get(Uint32 key, Uint32 notFound) const; 00050 Uint64 get64(Uint32 key, Uint64 notFound) const; 00051 const char * get(Uint32 key, const char * notFound) const; 00052 ValueType getTypeOf(Uint32 key) const; 00053 }; 00054 00055 class Iterator : public ConstIterator { 00056 ConfigValues & m_cfg; 00057 public: 00058 Iterator(ConfigValues&c) : ConstIterator(c), m_cfg(c) {} 00059 Iterator(ConfigValues&c, const ConstIterator& i):ConstIterator(c),m_cfg(c){ 00060 m_currentSection = i.m_currentSection; 00061 } 00062 00063 bool set(Uint32 key, Uint32 value); 00064 bool set(Uint32 key, Uint64 value); 00065 bool set(Uint32 key, const char * value); 00066 }; 00067 00068 Uint32 getPackedSize() const; // get size in bytes needed to pack 00069 Uint32 pack(UtilBuffer&) const; 00070 Uint32 pack(void * dst, Uint32 len) const;// pack into dst(of len %d); 00071 00072 private: 00073 friend class Iterator; 00074 friend class ConstIterator; 00075 00076 bool getByPos(Uint32 pos, Entry *) const; 00077 Uint64 * get64(Uint32 index) const; 00078 char ** getString(Uint32 index) const; 00079 00080 Uint32 m_size; 00081 Uint32 m_dataSize; 00082 Uint32 m_stringCount; 00083 Uint32 m_int64Count; 00084 00085 Uint32 m_values[1]; 00086 void * m_data[1]; 00087 }; 00088 00089 class ConfigValuesFactory { 00090 Uint32 m_currentSection; 00091 public: 00092 Uint32 m_sectionCounter; 00093 Uint32 m_freeKeys; 00094 Uint32 m_freeData; 00095 00096 public: 00097 ConfigValuesFactory(Uint32 keys = 50, Uint32 data = 10); // Initial 00098 ConfigValuesFactory(ConfigValues * m_cfg); // 00099 ~ConfigValuesFactory(); 00100 00101 ConfigValues * m_cfg; 00102 ConfigValues * getConfigValues(); 00103 00104 bool openSection(Uint32 key, Uint32 no); 00105 bool put(const ConfigValues::Entry & ); 00106 bool put(Uint32 key, Uint32 value); 00107 bool put64(Uint32 key, Uint64 value); 00108 bool put(Uint32 key, const char * value); 00109 bool closeSection(); 00110 00111 void expand(Uint32 freeKeys, Uint32 freeData); 00112 void shrink(); 00113 00114 bool unpack(const UtilBuffer&); 00115 bool unpack(const void * src, Uint32 len); 00116 00117 static ConfigValues * extractCurrentSection(const ConfigValues::ConstIterator &); 00118 00119 private: 00120 static ConfigValues * create(Uint32 keys, Uint32 data); 00121 void put(const ConfigValues & src); 00122 }; 00123 00124 inline 00125 bool 00126 ConfigValues::ConstIterator::get(Uint32 key, Uint32 * value) const { 00127 Entry tmp; 00128 if(get(key, &tmp) && tmp.m_type == IntType){ 00129 * value = tmp.m_int; 00130 return true; 00131 } 00132 return false; 00133 } 00134 00135 inline 00136 bool 00137 ConfigValues::ConstIterator::get(Uint32 key, Uint64 * value) const { 00138 Entry tmp; 00139 if(get(key, &tmp) && tmp.m_type == Int64Type){ 00140 * value = tmp.m_int64; 00141 return true; 00142 } 00143 return false; 00144 } 00145 00146 inline 00147 bool 00148 ConfigValues::ConstIterator::get(Uint32 key, const char ** value) const { 00149 Entry tmp; 00150 if(get(key, &tmp) && tmp.m_type == StringType){ 00151 * value = tmp.m_string; 00152 return true; 00153 } 00154 return false; 00155 } 00156 00157 inline 00158 bool 00159 ConfigValues::ConstIterator::getTypeOf(Uint32 key, ValueType * type) const{ 00160 Entry tmp; 00161 if(get(key, &tmp)){ 00162 * type = tmp.m_type; 00163 return true; 00164 } 00165 return false; 00166 } 00167 00168 inline 00169 Uint32 00170 ConfigValues::ConstIterator::get(Uint32 key, Uint32 notFound) const { 00171 Entry tmp; 00172 if(get(key, &tmp) && tmp.m_type == IntType){ 00173 return tmp.m_int; 00174 } 00175 return notFound; 00176 } 00177 00178 inline 00179 Uint64 00180 ConfigValues::ConstIterator::get64(Uint32 key, Uint64 notFound) const { 00181 Entry tmp; 00182 if(get(key, &tmp) && tmp.m_type == Int64Type){ 00183 return tmp.m_int64; 00184 } 00185 return notFound; 00186 } 00187 00188 inline 00189 const char * 00190 ConfigValues::ConstIterator::get(Uint32 key, const char * notFound) const { 00191 Entry tmp; 00192 if(get(key, &tmp) && tmp.m_type == StringType){ 00193 return tmp.m_string; 00194 } 00195 return notFound; 00196 } 00197 00198 inline 00199 ConfigValues::ValueType 00200 ConfigValues::ConstIterator::getTypeOf(Uint32 key) const{ 00201 Entry tmp; 00202 if(get(key, &tmp)){ 00203 return tmp.m_type; 00204 } 00205 return ConfigValues::InvalidType; 00206 } 00207 00208 inline 00209 bool 00210 ConfigValuesFactory::put(Uint32 key, Uint32 val){ 00211 ConfigValues::Entry tmp; 00212 tmp.m_key = key; 00213 tmp.m_type = ConfigValues::IntType; 00214 tmp.m_int = val; 00215 return put(tmp); 00216 } 00217 00218 inline 00219 bool 00220 ConfigValuesFactory::put64(Uint32 key, Uint64 val){ 00221 ConfigValues::Entry tmp; 00222 tmp.m_key = key; 00223 tmp.m_type = ConfigValues::Int64Type; 00224 tmp.m_int64 = val; 00225 return put(tmp); 00226 } 00227 00228 inline 00229 bool 00230 ConfigValuesFactory::put(Uint32 key, const char * val){ 00231 ConfigValues::Entry tmp; 00232 tmp.m_key = key; 00233 tmp.m_type = ConfigValues::StringType; 00234 tmp.m_string = val; 00235 return put(tmp); 00236 } 00237 00238 inline 00239 Uint32 00240 ConfigValues::pack(UtilBuffer& buf) const { 00241 Uint32 len = getPackedSize(); 00242 void * tmp = buf.append(len); 00243 if(tmp == 0){ 00244 return 0; 00245 } 00246 return pack(tmp, len); 00247 } 00248 00249 inline 00250 bool 00251 ConfigValuesFactory::unpack(const UtilBuffer& buf){ 00252 return unpack(buf.get_data(), buf.length()); 00253 } 00254 00255 #endif
1.4.7

