3 * @author Reinhard Pfau \<Reinhard.Pfau@intra2net.com\>
5 * @copyright © Copyright 2007-2008 by Intra2net AG
11 #ifndef __I2N_CONFIGDATA_HPP__
12 #define __I2N_CONFIGDATA_HPP__
26 * key-value data storage with some additional features.
27 * This class is meant to hold data from config files.
31 * - the keys are remembered in the order they are put into the list
32 * - each key can hold multiple values
33 * - the values for each key are remembered in the order they were added
34 * - if a single value for a key is requested than the last value is returned.
36 * @tparam KeyType the type for the keys
37 * @tparam ValueType the type for the values.
47 typedef std::vector<KeyType> KeyListType;
48 typedef std::vector<ValueType> ValueListType;
49 typedef std::map<KeyType, ValueListType> StorageType;
54 * @brief adaptor class for accessing values within the list for a given key.
56 * This is returned by KeyValueList::operator[](const std::string&) for
57 * more convenient access.
59 * This class is designed to be used in transient contexts.
63 // make parent class my friend since the constructors are private!
64 friend class KeyValueData;
69 * cast operator for ValueType.
70 * @return the (last) value for the key
72 operator ValueType () const
74 return m_kv.getValue(m_key);
75 } // eo operator ValueType() const
79 * cast operator for ValueListType.
80 * @return the value list for the key.
82 operator ValueListType () const
84 return m_kv.getValues(m_key);
85 } // eo operator ValueListType() const
89 * performs setValue().
90 * @param value the value which should be set for the key.
91 * @return reference to this (for chaining).
93 ValueAdaptor& operator = (const ValueType& value)
95 m_kv.setValue(m_key,value);
97 } // eo operator =(const ValueType&)
101 * performs setValues()
102 * @param values the new values (/value list) which should be set for the key.
103 * @return reference to this (for chaining).
105 ValueAdaptor& operator = (const ValueListType& values)
107 m_kv.setValues(m_key, values);
109 } // eo operator =(const ValueListType&);
113 * performs addValue().
114 * @param value the value which should be added to the value list for the key,
115 * @return reference to this (for chaining).
117 ValueAdaptor& operator += (const ValueType& value)
119 m_kv.addValue(m_key,value);
121 } // eo operator +=(const ValueType&)
125 * performs addValues().
126 * @param values the new values (/value list) which should be added to the value list for the key.
127 * @return reference to this (for chaining).
129 ValueAdaptor& operator += (const ValueListType& values)
131 m_kv.addValues(m_key, values);
133 } // eo operator +=(const ValueListType)
138 ValueAdaptor(KeyValueData& kv, const KeyType& key)
144 ValueAdaptor(const ValueAdaptor& other)
150 ValueAdaptor& operator =(const ValueAdaptor&);
155 }; // eo class ValueAdaptor
167 } // eo ~KeyValueData
172 * set a value for a key.
173 * This erases an existing value list for that key and set's a new (single) value.
174 * @param key the key.
175 * @param value the value.
177 KeyValueData& setValue(const KeyType& key, const ValueType& value)
179 typename StorageType::iterator it= m_storage.find(key);
180 if (it == m_storage.end())
182 m_key_list.push_back(key);
183 m_storage[key].push_back(value);
188 it->second.push_back(value);
191 } // eo setValue(const KeyType&,const ValueType&)
196 * set a new value list for a key.
197 * Erases the key when the new value list is empty.
198 * Else the value list for the key is replaced with the new values.
200 * @param values the new value list.
202 KeyValueData& setValues(const KeyType& key, const ValueListType& values)
204 typename StorageType::iterator it= m_storage.find(key);
205 if (it == m_storage.end())
207 m_key_list.push_back(key);
208 m_storage[key]= values;
222 } // eo setValues(const KeyType&,const ValueListType&)
227 * adds a new value to the value list of a key.
230 * @param value the new value
232 KeyValueData& addValue(const KeyType& key, const ValueType& value)
234 typename StorageType::iterator it= m_storage.find(key);
235 if (it == m_storage.end())
237 m_key_list.push_back(key);
238 m_storage[key].push_back(value);
242 it->second.push_back(value);
245 } // eo addValue(const KeyType&,const ValueType&)
250 * adds new values to the value list of a key.
253 * @param values the new value list
255 KeyValueData& addValues(const KeyType& key, const ValueListType& values)
257 typename StorageType::iterator it= m_storage.find(key);
258 if (it == m_storage.end())
260 m_key_list.push_back(key);
261 m_storage[key]= values;
265 std::copy(values.begin(), values.end(), std::back_inserter(it->second));
268 } // eo addValues(const KeyType&,const ValueListType&)
273 * @brief removes a value from a value list of a key.
274 * @param key the key.
275 * @param value the value which should be removed (once).
276 * @return self reference.
277 * @note only the first occurance of the value is removed!
279 KeyValueData& removeValue(const KeyType& key, const ValueType& value)
281 typename StorageType::iterator it= m_storage.find(key);
282 if (it != m_storage.end())
284 for( typename ValueListType::iterator it_val= it->second.begin();
285 it_val != it->second.end();
288 if ( *it_val == value)
290 it->second.erase(it_val);
294 if (it->second.empty())
304 * retrieve the (single) value for a key.
305 * If the value list of the key has multiple values, the last one returned.
307 * @param[out] value the resulting value if found
308 * @return @a true iff the key has a value (i,e, the key exits).
310 bool getValue(const KeyType& key, ValueType& value) const
312 typename StorageType::const_iterator it= m_storage.find(key);
313 if (it == m_storage.end())
319 value= it->second.back();
322 } // eo getValue(const KeyType&, ValueType&)
327 * retrieve the value list for a key.
329 * @param[out] values the resulting value list if found
330 * @return @a true iff the key has values (i,e, the key exits).
332 bool getValues(const KeyType& key, ValueListType& values) const
334 typename StorageType::const_iterator it= m_storage.find(key);
335 if (it == m_storage.end())
344 } // eo getValues(const KeyType&, ValueListType&)
349 * retrieve the (single) value for a key.
350 * If the value list of the key has multiple values, the last one returned.
352 * @return the value of the key (default contructed value if key not found)
354 ValueType getValue(const KeyType& key) const
357 getValue( key, value);
359 } // eo getValue(const KeyType&)
364 * retrieve the value list for a key.
366 * @return the value of the key (empty if key not found)
368 ValueListType getValues(const KeyType& key) const
370 ValueListType values;
371 getValues( key, values);
373 } // eo getValues(const KeyType&)
378 * retrieve the (single) value for a key.
379 * If the value list of the key has multiple values, the last one returned.
380 * If the key wasn't found (i.e. no value) then the default value is returned.
382 * @param default_value the value which is returned if the key wasn't found.
383 * @return the value of the key (default_value if key not found)
385 ValueType getValueOrDefault(const KeyType& key, const ValueType& default_value) const
388 if (getValue( key, value))
392 return default_value;
393 } // eo getValueOrDefault(const KeyType&,const ValueType&)
398 * removes a key (and it's values) from the list.
399 * @param key the key.
401 void deleteKey(const KeyType& key)
403 typename StorageType::iterator it= m_storage.find(key);
404 if (it == m_storage.end())
409 typename KeyListType::iterator itk= std::find(m_key_list.begin(), m_key_list.end(), key);
410 if (itk != m_key_list.end())
412 m_key_list.erase(itk);
414 } // eo deleteKey(const KeyType&)
419 * returns if a key exists in the list.
420 * @param key the key which should be looked for.
421 * @return @a true if the key is in the list.
423 bool hasKey(const KeyType& key) const
425 return (m_storage.find(key) != m_storage.end());
426 } // eo hasKey(const KeyType&)
431 * delivers the list of keys.
432 * @param[out] result_list the key list
434 void getKeys(KeyListType& result_list) const
436 result_list= m_key_list;
437 } // eo getKeys(KeyListType&) const
442 * deliviers the list of keys.
443 * @return the list of keys.
445 KeyListType getKeys() const
448 } // eo getKeys() const
453 * returns if the list is empty (i.e. has no keys)
454 * @return @a true iff the list has no keys (and values).
458 return m_key_list.empty();
464 * @brief index operator for more readable access.
465 * @param key the key which should be indexed.
466 * @return an instance of ValueAdaptor initialized with the key.
468 ValueAdaptor operator [] (const KeyType& key)
470 return ValueAdaptor(*this, key);
471 } // eo operator[](const KeyType&)
475 * @brief returns if the current iunstance is equal to another one (of the same type).
476 * @param rhs the orher instance to compare with.
477 * @return @a true iff the two instances are equal.
479 bool operator==( const KeyValueData& rhs)
481 return m_key_list == rhs.m_key_list and m_storage == rhs.m_storage;
482 } // eo operator==(const KeyValueData&)
486 * @brief clears the list.
488 * Well, it just deletes all entries.
498 * @brief exchanges the internal data with another instance.
501 void swap(KeyValueData& other)
505 m_storage.swap( other.m_storage );
506 m_key_list.swap( other.m_key_list );
508 } // eo swap(KeyValueData&)
514 * returns a reference to the last value of the value list for a given key; creating it if the key doesn't
516 * This method is only for derived classes to optimize accessing of the values.
517 * @param key the key for which the reference is needed.
518 * @return reference to the last value of the value list for the key.
520 ValueType& getValueRef(const KeyType& key)
522 typename KeyListType::iterator it=
523 std::find( m_key_list.begin(), m_key_list.end(), key );
524 if (it == m_key_list.end())
526 m_key_list.push_back(key);
528 if (m_storage[key].empty())
531 m_storage[key].push_back( v );
533 return m_storage[key].back();
534 } // eo getValueRef(const KeyType&)
538 * @brief returns a reference to the last value of the value list for a given key.
539 * @param key the key for which the reference is needed.
540 * @return reference to the last value of the value list for the key or to internal
541 * value if no values are existing.
543 * This method is only for derived classes to optimize accessing of the values.
545 * @note The method returns a reference to an internal value if no approbiate value
546 * exists! It's up to the derived class to deal with that.
548 const ValueType& getConstValueRef(const KeyType& key) const
550 static ValueType empty_value;
551 typename StorageType::const_iterator storage_it= m_storage.find(key);
552 if (storage_it == m_storage.end()
553 or storage_it->second.empty() )
557 return storage_it->second.back();
558 } // eo getConstValueRef(const KeyType&)
562 * @brief returns a reference to the value list for a given key.
563 * @param key the key for which the reference is needed.
564 * @return reference to the value list for the key.
566 * This method is only for derived classes to optimize accessing of the values.
568 * @note The method returns a reference to an internal value if no approbiate value
569 * exists! It's up to the derived class to deal with that.
571 const ValueListType& getConstValuesRef(const KeyType& key) const
573 static ValueListType empty_value;
574 typename StorageType::const_iterator storage_it= m_storage.find(key);
575 if ( storage_it == m_storage.end() )
579 return storage_it->second;
580 } // eo getConstValuesRef(const KeyType&)
586 StorageType m_storage;
587 KeyListType m_key_list;
589 }; // eo KeyValueData
595 * grouped key value list.
596 * This (template) class provides storage for groups whose values are key-value lists.
597 * Basically it's a key-value list, wohse values are another key-value list.
599 * If used with std::string for all key types, it provides a storage suitable for holding they
600 * (for example) the content of INI style config files.
602 * @param GroupKeyType type for the group keys.
603 * @param KeyType type for the keys (within a group)
604 * @param ValueType type for the values (within a group)
611 class GroupedKeyValueData
612 : public KeyValueData<GroupKeyType, KeyValueData<KeyType,ValueType> >
614 typedef KeyValueData<GroupKeyType, KeyValueData<KeyType,ValueType> > inherited;
618 typedef KeyValueData<KeyType,ValueType> GroupDataType;
622 GroupedKeyValueData()
627 ~GroupedKeyValueData()
633 ** overridden methods:
637 * add (new) group data to a group.
638 * This differs from KeyValueData::addValue in a way that it merges the group data into
639 * an existing group instead of appending a new data group.
640 * @param group_key the key of the group.
641 * @param value the new data which should be added to the group.
643 void addValue(const GroupKeyType& group_key, const GroupDataType& value)
649 GroupDataType& group_data= getValueRef(group_key);
651 typename GroupDataType::KeyListType keys = value.getKeys();
652 for(typename GroupDataType::KeyListType::const_iterator it_key= keys.begin();
653 it_key != keys.end();
656 typename GroupDataType::ValueListType values= value.getValues(*it_key);
657 if (values.empty()) continue;
658 group_data.addValues( *it_key, value.getValues(*it_key) );
660 } // eo addValue(const GroupKey&,const GroupData&)
664 * set new group values.
665 * This differs from KeyValueData::setValues in a way that it merges all values to a single
667 * @param group_key the key of the group.
668 * @param values the list of data grups which are merged together to the new group value.
670 void setValues(const GroupKeyType& group_key, const typename inherited::ValueListType& values)
674 inherited::deleteKey(group_key);
677 // note: since (the overridden method) addValue() already does what we need,
678 // we just clear the data and iterate over the new values list:
679 getValueRef(group_key).clear();
680 for(typename inherited::ValueListType::const_iterator it= values.begin();
684 addValue(group_key, *it);
686 if ( getValueRef(group_key).empty())
688 deleteKey(group_key);
690 } // eo setValues(const GroupKeyType&,const ValueListType&)
694 * add new group values.
695 * This differs from KeyValueList::addValues in a way that the new values are merged into
696 * a single data groupo.
700 void addValues(const GroupKeyType& group_key, const typename inherited::ValueListType& values)
706 // like in setValues(); we just use our new addValue() to do the job:
707 for(typename inherited::ValueListType::const_iterator it= values.begin();
711 addValue(group_key, *it);
713 } // eo addValues(const GroupKeyType&,const ValueListType&)
718 ** additional methods:
722 GroupDataType& operator[](const GroupKeyType& key)
724 return getValueRef(key);
725 } // eo operator [](const groupKey&)
728 const GroupDataType& operator[](const GroupKeyType& key) const
730 return getConstValueRef(key);
731 } // eo operator [](const groupKey&) const
735 * check if a group exists (alias for hasKey(const GroupKey&) const).
736 * @param group_key the key og the group which is looked for.
737 * @return @a true iff the group exists.
739 bool hasGroup(const GroupKeyType& group_key) const
741 return inherited::hasKey(group_key);
742 } // eo hasGroup(const GroupKeyType&) const
746 * check if a key within a group exists.
747 * @param group_key the group which should be looked in.
748 * @param key the key which should be tested.
749 * @return @a true iff the group exists and the key exists within that group.
751 bool hasKey(const GroupKeyType& group_key, const KeyType& key) const
753 if (!hasGroup(group_key))
757 return getConstValueRef(group_key).hasKey(key);
758 } // eo hasKey(const GroupKeyType&,const KeyType&) const
762 }; // eo GroupedKeyValueData
767 ** typedef the most common case; suitable for holding the content of
768 ** INI style config files.
772 typedef GroupedKeyValueData<
778 typedef ConfigData::GroupDataType ConfigSectionData;
782 } // eo namespace I2n