CollectionUtil.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef _COLLECTION_UTIL_H_
  19. #define _COLLECTION_UTIL_H_
  20. /**
  21. * \file
  22. * CollectionBuilder and DictionaryBuilder classes and collection utility functions
  23. */
  24. namespace Util
  25. {
  26. // *********************************************************
  27. /** A shortcut to use for building collections.
  28. * This class is a wrapper around standard STL collection containers such as vector.
  29. * It allows one to conveniently build collections at the variable initialization time:
  30. * \code
  31. * #include "CollectionUtil.h"
  32. * #include "Vector.h" // for ostream << operator overload for STL vector
  33. * using Util;
  34. *
  35. * int main()
  36. * {
  37. * typedef vector<string> MyVector;
  38. * MyVector myVector=CollectionBuilder<MyVector>()("str1")("str2")("str3");
  39. * cout<<myVector;
  40. * // the following output will be produced:
  41. * // [str1,str2,str3]
  42. * }
  43. * \endcode
  44. */
  45. template <class CONT>
  46. class CollectionBuilder
  47. {
  48. public:
  49. /// Type of the collection container.
  50. typedef CONT CollectionType;
  51. /// Container's value type.
  52. typedef typename CollectionType::value_type value_type;
  53. /// Container's constant iterator type.
  54. typedef typename CollectionType::const_iterator const_iterator;
  55. /// Container's size type.
  56. typedef typename CollectionType::size_type size_type;
  57. /** Operator function call overload to allow call chaining.
  58. * \param value the value to be inserted into the container
  59. */
  60. CollectionBuilder<CONT>& operator()(const value_type& value){
  61. return push_back(value);
  62. }
  63. /** Same as regular STL push_back() but allows call chaining.
  64. * \param value the value to be inserted into the container
  65. */
  66. CollectionBuilder<CONT>& push_back(const value_type& value){
  67. collection_.push_back(value);
  68. return *this;
  69. }
  70. /// \name Standard STL container interface
  71. /// @{
  72. const_iterator begin() const{return collection_.begin();}
  73. const_iterator end() const{return collection_.end();}
  74. size_type size() const{return collection_.size();}
  75. void clear() {collection_.clear();}
  76. ///@}
  77. /// Explicit typecast operator.
  78. operator const CollectionType&() const {return collection_;}
  79. private:
  80. /// \cond PRIVATE
  81. CollectionType collection_;
  82. /// \endcond
  83. };
  84. // *********************************************************
  85. /** A shortcut to use for building dictionaries.
  86. * This class is a wrapper around standard STL associative containers such as map.
  87. * It allows one to conveniently build dictionaries at the variable initialization time:
  88. * \code
  89. * #include "CollectionUtil.h"
  90. * #include "Map.h" // for ostream << operator overload for STL map
  91. * using Util;
  92. *
  93. * int main()
  94. * {
  95. * typedef map<string,int> MyMap;
  96. * MyMap myMap=DictionaryBuilder<MyMap>()("str1",1)("str2",2)("str3",3);
  97. * cout<<myMap;
  98. * // the following output will be produced:
  99. * // [str1=1,str2=2,str3=3]
  100. * }
  101. * \endcode
  102. */
  103. template <class CONT>
  104. class DictionaryBuilder
  105. {
  106. public:
  107. /// The type of the associative container
  108. typedef CONT DictionaryType;
  109. /// Container's element type (usually a pair<key_type,mapped_type>)
  110. typedef typename DictionaryType::value_type value_type;
  111. /// Container's key type
  112. typedef typename DictionaryType::key_type key_type;
  113. /// Container's value type
  114. typedef typename DictionaryType::mapped_type mapped_type;
  115. /// Container's constant iterator type
  116. typedef typename DictionaryType::const_iterator const_iterator;
  117. /// Container's writable iterator type
  118. typedef typename DictionaryType::iterator iterator;
  119. /// Container's size type
  120. typedef typename DictionaryType::size_type size_type;
  121. /** Operator function call overload to allow call chaining.
  122. * \param key the value key to be inserted
  123. * \param value the value to be inserted into the container
  124. * \return a non-const reference to self
  125. */
  126. DictionaryBuilder<CONT>& operator()(const key_type& key,const mapped_type& value){
  127. dict_.insert(value_type(key,value));
  128. return *this;
  129. }
  130. /** Lookup value by key.
  131. * \param key the key associated with the value.
  132. * \return a non-const iterator pointing to the element whose key matched the \a key parameter
  133. */
  134. iterator find(const key_type& key){
  135. return dict_.find(key);
  136. }
  137. /** Lookup value by key.
  138. * \param key the key associated with the value.
  139. * \return a const iterator pointing to the element whose key matched the \a key parameter
  140. */
  141. const_iterator find(const key_type& key) const{
  142. return dict_.find(key);
  143. }
  144. /// \name Standard STL container interface
  145. /// @{
  146. const_iterator begin() const{return dict_.begin();}
  147. const_iterator end() const{return dict_.end();}
  148. size_type size() const{return dict_.size();}
  149. void clear() {dict_.clear();}
  150. ///@}
  151. /// Explicit typecast operator.
  152. operator const DictionaryType&() const {return dict_;}
  153. private:
  154. DictionaryType dict_;
  155. };
  156. // ***********************************************************
  157. /** Deletes all dynamically allocated elements of a collection.
  158. * C::value_type is expected to be a pointer to a dynamically allocated object, or it won't compile.
  159. * The function will iterate over all container elements and call delete for each of them.
  160. * \param c a collection (vector,set) whose elements are being deleted.
  161. */
  162. template <class C>
  163. void clearCollection(C& c){
  164. for(typename C::const_iterator it=c.begin();it!=c.end();++it)
  165. delete *it;
  166. c.clear();
  167. }
  168. /** Deletes all dynamically allocated values of the assotiative container.
  169. * The function expects the M::value_type to be a pair<..., ptr_to_type>, or it won't compile.
  170. * It first deletes the objects pointed to by ptr_to_type
  171. * and then clears (calls m.clear()) the container.
  172. * \param m an associative container (map,hash_map) whose elements are being deleted.
  173. */
  174. template <class M>
  175. void clearMap(M& m){
  176. for(typename M::const_iterator it=m.begin();it!=m.end();++it)
  177. delete it->second;
  178. m.clear();
  179. }
  180. } // namespace Util
  181. #endif // _COLLECTION_UTIL_H_