2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * Declares classes for building the data structures in keyvaluetree.h.
39 * These are separate from the data structures to enforce clear separation of
40 * the APIs, and to make the data structure immutable after construction.
42 * For the main use case described in \ref page_mdmodules, they are mainly
43 * used internally, but currently gmx::KeyValueTreeObjectBuilder (and
44 * everything it references) is exposed for more complex transforms through
45 * gmx::IKeyValueTreeTransformRules.
47 * \author Teemu Murtola <teemu.murtola@gmail.com>
49 * \ingroup module_utility
51 #ifndef GMX_UTILITY_KEYVALUETREEBUILDER_H
52 #define GMX_UTILITY_KEYVALUETREEBUILDER_H
54 #include <initializer_list>
59 #include "gromacs/utility/any.h"
60 #include "gromacs/utility/gmxassert.h"
61 #include "gromacs/utility/keyvaluetree.h"
66 class KeyValueTreeArrayBuilder
;
67 class KeyValueTreeObjectBuilder
;
69 /*! \libinternal \brief
70 * Root builder for creating trees that have an object at the root.
73 * \ingroup module_utility
75 class KeyValueTreeBuilder
78 //! Returns a builder for the root object.
79 KeyValueTreeObjectBuilder
rootObject();
82 * Builds the final object.
84 * The builder should not be accessed after this call.
86 KeyValueTreeObject
build() { return std::move(root_
); }
90 * Helper function for other builders to create values of certain type.
93 static KeyValueTreeValue
createValue(const T
&value
)
95 return KeyValueTreeValue(Any::create
<T
>(value
));
98 * Helper function for other builders to create default-constructed
101 template <typename T
>
102 static KeyValueTreeValue
createValue()
104 return KeyValueTreeValue(Any::create
<T
>(T()));
107 KeyValueTreeObject root_
;
109 //! For access to createValue() methods.
110 friend class KeyValueTreeObjectArrayBuilder
;
111 //! For access to createValue() methods.
112 friend class KeyValueTreeObjectBuilder
;
113 //! For access to createValue() methods.
114 template <typename T
>
115 friend class KeyValueTreeUniformArrayBuilder
;
118 /*! \libinternal \brief
119 * Builder for KeyValueTreeValue objects.
121 * This builder can be constructed directly and can create self-standing
122 * KeyValueTreeValue objects.
125 * \ingroup module_utility
127 class KeyValueTreeValueBuilder
130 //! Assigns a scalar value of certain type.
131 template <typename T
>
132 void setValue(const T
&value
)
134 value_
= Any::create
<T
>(value
);
136 //! Assigns a Any value to the built value.
137 void setAnyValue(Any
&&value
)
139 value_
= std::move(value
);
142 * Returns an object builder for building an object into this value.
144 * Any method call in this value builder invalidates the returned
147 KeyValueTreeObjectBuilder
createObject();
149 * Returns an array builder for building an array into this value.
151 * Any method call in this value builder invalidates the returned
154 KeyValueTreeArrayBuilder
createArray();
157 * Builds the final value.
159 * The builder should not be accessed after this call.
161 KeyValueTreeValue
build() { return KeyValueTreeValue(std::move(value_
)); }
167 class KeyValueTreeArrayBuilderBase
170 //! Creates an array builder for populating given array object.
171 explicit KeyValueTreeArrayBuilderBase(KeyValueTreeArray
*array
)
176 //! Appends a raw Any value to the array.
177 KeyValueTreeValue
&addRawValue(Any
&&value
)
179 KeyValueTreeValueBuilder builder
;
180 builder
.setAnyValue(std::move(value
));
181 array_
->values_
.push_back(builder
.build());
182 return array_
->values_
.back();
184 //! Appends a raw KeyValueTreeValue to the array.
185 KeyValueTreeValue
&addRawValue(KeyValueTreeValue
&&value
)
187 array_
->values_
.push_back(std::move(value
));
188 return array_
->values_
.back();
192 KeyValueTreeArray
*array_
;
195 class KeyValueTreeArrayBuilder
: public KeyValueTreeArrayBuilderBase
198 using KeyValueTreeArrayBuilderBase::addRawValue
;
201 explicit KeyValueTreeArrayBuilder(KeyValueTreeArray
*array
)
202 : KeyValueTreeArrayBuilderBase(array
)
206 friend class KeyValueTreeObjectBuilder
;
207 friend class KeyValueTreeValueBuilder
;
210 /*! \libinternal \brief
211 * Builder for KeyValueTreeArray objects where all elements are of type `T`.
213 * The builder does not own the array being constructed, but instead holds a
214 * reference to an object within a tree rooted in KeyValueTreeBuilder or
215 * KeyValueTreeValueBuilder.
218 * \ingroup module_utility
220 template <typename T
>
221 class KeyValueTreeUniformArrayBuilder
: public KeyValueTreeArrayBuilderBase
224 //! Appends a value to the array.
225 void addValue(const T
&value
)
227 addRawValue(KeyValueTreeBuilder::createValue
<T
>(value
));
231 explicit KeyValueTreeUniformArrayBuilder(KeyValueTreeArray
*array
)
232 : KeyValueTreeArrayBuilderBase(array
)
236 friend class KeyValueTreeObjectBuilder
;
239 /*! \libinternal \brief
240 * Builder for KeyValueTreeArray objects where all elements are
241 * KeyValueTreeObject objects.
243 * The builder does not own the array being constructed, but instead holds a
244 * reference to an object within a tree rooted in KeyValueTreeBuilder or
245 * KeyValueTreeValueBuilder.
248 * \ingroup module_utility
250 class KeyValueTreeObjectArrayBuilder
: public KeyValueTreeArrayBuilderBase
254 * Appends an object to the array.
256 * The object is created empty and can be built using the returned
259 KeyValueTreeObjectBuilder
addObject();
262 explicit KeyValueTreeObjectArrayBuilder(KeyValueTreeArray
*array
)
263 : KeyValueTreeArrayBuilderBase(array
)
267 friend class KeyValueTreeObjectBuilder
;
270 /*! \libinternal \brief
271 * Builder for KeyValueTreeObject objects.
273 * The builder does not own the object being constructed, but instead holds a
274 * reference to an object within a tree rooted in KeyValueTreeBuilder or
275 * KeyValueTreeValueBuilder.
278 * \ingroup module_utility
280 class KeyValueTreeObjectBuilder
283 //! Adds a property with given key from a KeyValueTreeValue.
284 void addRawValue(const std::string
&key
, KeyValueTreeValue
&&value
)
286 addProperty(key
, std::move(value
));
288 //! Adds a property with given key from a Any value.
289 void addRawValue(const std::string
&key
, Any
&&value
)
291 addProperty(key
, KeyValueTreeValue(std::move(value
)));
293 //! Adds a scalar property with given key, type, and value.
294 template <typename T
>
295 void addValue(const std::string
&key
, const T
&value
)
297 addRawValue(key
, KeyValueTreeBuilder::createValue
<T
>(value
));
300 * Adds an object-valued property with given key.
302 * The object is created empty and can be built using the returned
305 KeyValueTreeObjectBuilder
addObject(const std::string
&key
)
307 auto iter
= addProperty(key
, KeyValueTreeBuilder::createValue
<KeyValueTreeObject
>());
308 return KeyValueTreeObjectBuilder(&iter
->second
);
311 * Adds a generic array-valued property with given key.
313 * The array is created empty and can be built using the returned
316 KeyValueTreeArrayBuilder
addArray(const std::string
&key
)
318 auto iter
= addProperty(key
, KeyValueTreeBuilder::createValue
<KeyValueTreeArray
>());
319 return KeyValueTreeArrayBuilder(&iter
->second
.asArray());
322 * Adds an array-valued property with uniform value types with given
325 * \tparam T Type for all values in the array.
327 * The array is created empty and can be built using the returned
330 template <typename T
>
331 KeyValueTreeUniformArrayBuilder
<T
> addUniformArray(const std::string
&key
)
333 auto iter
= addProperty(key
, KeyValueTreeBuilder::createValue
<KeyValueTreeArray
>());
334 return KeyValueTreeUniformArrayBuilder
<T
>(&iter
->second
.asArray());
337 * Adds an array-valued property with uniform value types with given
340 * \tparam T Type for all values in the array.
342 * The array is created to contain the values from `values`.
344 template <typename T
>
345 void addUniformArray(const std::string
&key
, std::initializer_list
<T
> values
)
347 auto builder
= addUniformArray
<T
>(key
);
348 for (const auto &value
: values
)
350 builder
.addValue(value
);
354 * Adds an array-valued property with objects in the array with given
357 * The array is created empty and can be built using the returned
360 KeyValueTreeObjectArrayBuilder
addObjectArray(const std::string
&key
)
362 auto iter
= addProperty(key
, KeyValueTreeBuilder::createValue
<KeyValueTreeArray
>());
363 return KeyValueTreeObjectArrayBuilder(&iter
->second
.asArray());
366 //! Whether a property with given key exists.
367 bool keyExists(const std::string
&key
) const { return object_
->keyExists(key
); }
368 //! Returns value for a given key.
369 const KeyValueTreeValue
&operator[](const std::string
&key
) const
371 return (*object_
)[key
];
373 //! Returns an object builder for an existing object.
374 KeyValueTreeObjectBuilder
getObjectBuilder(const std::string
&key
)
376 GMX_ASSERT(keyExists(key
), "Requested non-existent value");
377 GMX_ASSERT((*this)[key
].isObject(), "Accessing non-object value as object");
378 return KeyValueTreeObjectBuilder(&object_
->valueMap_
.at(key
).asObject());
382 * Returns whether the given object shares any keys with \p this.
384 bool objectHasDistinctProperties(const KeyValueTreeObject
&obj
) const
386 return object_
->hasDistinctProperties(obj
);
389 * Merges properties from a given object to `this`.
391 * The objects should not share any keys, i.e.,
392 * objectHasDistinctProperties() should return `true`.
394 void mergeObject(KeyValueTreeObject
&&obj
)
396 GMX_ASSERT(objectHasDistinctProperties(obj
),
397 "Trying to merge overlapping object");
398 for (auto &prop
: obj
.valueMap_
)
400 addRawValue(prop
.first
, std::move(prop
.second
));
405 explicit KeyValueTreeObjectBuilder(KeyValueTreeObject
*object
)
409 explicit KeyValueTreeObjectBuilder(KeyValueTreeValue
*value
)
410 : object_(&value
->asObject())
414 std::map
<std::string
, KeyValueTreeValue
>::iterator
415 addProperty(const std::string
&key
, KeyValueTreeValue
&&value
)
417 GMX_RELEASE_ASSERT(!keyExists(key
), "Duplicate key value");
418 object_
->values_
.reserve(object_
->values_
.size() + 1);
419 auto iter
= object_
->valueMap_
.insert(std::make_pair(key
, std::move(value
))).first
;
420 object_
->values_
.push_back(KeyValueTreeProperty(iter
));
424 KeyValueTreeObject
*object_
;
426 friend class KeyValueTreeBuilder
;
427 friend class KeyValueTreeValueBuilder
;
428 friend class KeyValueTreeObjectArrayBuilder
;
431 /********************************************************************
432 * Inline functions that could not be declared within the classes
435 inline KeyValueTreeObjectBuilder
KeyValueTreeBuilder::rootObject()
437 return KeyValueTreeObjectBuilder(&root_
);
440 inline KeyValueTreeObjectBuilder
KeyValueTreeValueBuilder::createObject()
442 value_
= Any::create
<KeyValueTreeObject
>(KeyValueTreeObject());
443 return KeyValueTreeObjectBuilder(&value_
.castRef
<KeyValueTreeObject
>());
446 inline KeyValueTreeArrayBuilder
KeyValueTreeValueBuilder::createArray()
448 value_
= Any::create
<KeyValueTreeArray
>(KeyValueTreeArray());
449 return KeyValueTreeArrayBuilder(&value_
.castRef
<KeyValueTreeArray
>());
452 inline KeyValueTreeObjectBuilder
KeyValueTreeObjectArrayBuilder::addObject()
454 auto &value
= addRawValue(KeyValueTreeBuilder::createValue
<KeyValueTreeObject
>());
455 return KeyValueTreeObjectBuilder(&value
);