Molassembler  1.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AddToContainer.h
Go to the documentation of this file.
1 
16 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_ADD_TO_CONTAINER_H
17 #define INCLUDE_MOLASSEMBLER_TEMPLE_ADD_TO_CONTAINER_H
18 
21 
22 namespace Scine {
23 namespace Molassembler {
24 namespace Temple {
25 
30 template<class Container, typename T>
31 void addToContainer(
32  Container& container,
33  const T& value
34 );
35 
40 template<class Container, typename T>
41 void addToContainer(
42  Container& container,
43  T&& value
44 );
45 
68 template<
69  class TargetContainer,
70  class SourceContainer,
71  class SizeModifierUnary = Functor::Identity
72 > void reserveIfPossible(
73  TargetContainer& target,
74  const SourceContainer& source,
75  SizeModifierUnary&& sourceModifierUnary = SizeModifierUnary {}
76 );
77 
78 namespace Detail {
79 
81 template<class Container, typename T>
82 std::enable_if_t<
83  std::is_same<
84  Traits::getValueType<Container>,
85  T
86  >::value && Traits::hasPushBack<Container>::value,
87  void
88 > insertOrPushBack(
89  Container& container,
90  const T& value
91 ) {
92  container.push_back(value);
93 }
94 
96 template<class Container, typename T>
97 std::enable_if_t<
98  std::is_same<
99  Traits::getValueType<Container>,
100  T
101  >::value && Traits::hasInsert<Container>::value,
102  void
103 > insertOrPushBack(
104  Container& container,
105  const T& value
106 ) {
107  container.insert(value);
108 }
109 
111 template<class Container, typename T>
112 std::enable_if_t<
113  std::is_same<
114  Traits::getValueType<Container>,
115  T
116  >::value && Traits::hasEmplaceBack<Container>::value,
117  void
118 > emplaceOrEmplaceBack(
119  Container& container,
120  T&& value
121 ) {
122  container.emplace_back(std::forward<T>(value));
123 }
124 
126 template<class Container, typename T>
127 std::enable_if_t<
128  std::is_same<
129  Traits::getValueType<Container>,
130  T
131  >::value && Traits::hasEmplace<Container>::value,
132  void
133 > emplaceOrEmplaceBack(
134  Container& container,
135  T&& value
136 ) {
137  container.emplace(std::forward<T>(value));
138 }
139 
141 template<class TargetContainer, class SourceContainer, class SizeModifierUnary>
142 std::enable_if_t<
143  (
144  Traits::hasSize<SourceContainer>::value
145  && Traits::hasReserve<TargetContainer>::value
146  ),
147  void
148 > reserve(
149  TargetContainer& target,
150  const SourceContainer& source,
151  SizeModifierUnary&& sizeModifierUnary
152 ) {
153  target.reserve(
154  sizeModifierUnary(
155  source.size()
156  )
157  );
158 }
159 
161 template<class TargetContainer, class SourceContainer, class SizeModifierUnary>
162 std::enable_if_t<
163  !(
164  Traits::hasSize<SourceContainer>::value
165  && Traits::hasReserve<TargetContainer>::value
166  ),
167  void
168 > reserve(
169  TargetContainer& /* target */,
170  const SourceContainer& /* source */,
171  SizeModifierUnary&& /* sizeModifierUnary */
172 ) {
173  /* do nothing */
174 }
175 
176 } // namespace Detail
177 
178 template<class Container, typename T>
180  Container& container,
181  const T& value
182 ) {
183  Detail::insertOrPushBack(container, value);
184 }
185 
186 template<class Container, typename T>
188  Container& container,
189  T&& value
190 ) {
191  Detail::emplaceOrEmplaceBack(
192  container,
193  std::forward<T>(value)
194  );
195 }
196 
197 template<
198  class TargetContainer,
199  class SourceContainer,
200  class SizeModifierUnary
202  TargetContainer& target,
203  const SourceContainer& source,
204  SizeModifierUnary&& sourceModifierUnary
205 ) {
206  return Detail::reserve(
207  target,
208  source,
209  std::forward<SizeModifierUnary>(sourceModifierUnary)
210  );
211 }
212 
213 } // namespace Temple
214 } // namespace Molassembler
215 } // namespace Scine
216 
217 #endif
void reserveIfPossible(TargetContainer &target, const SourceContainer &source, SizeModifierUnary &&sourceModifierUnary=SizeModifierUnary{})
Rerves the space required in the target container if size can be determined from the source container...
Definition: AddToContainer.h:201
Provides functors for transformations.
void addToContainer(Container &container, const T &value)
Adds an lvalue element to a container by calling the container&#39;s insert or push_back member functions...
Definition: AddToContainer.h:179
Compile-time container type traits.