Update instructions in containers.rst
[gromacs.git] / src / gromacs / options / valuestore.h
blob1352f63e096258169fa31d78f3deda94e8ca7f3b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,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 /*! \file
36 * \brief
37 * Declares implementations for IOptionValueStore.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_options
42 #ifndef GMX_OPTIONS_VALUESTORE_H
43 #define GMX_OPTIONS_VALUESTORE_H
45 #include <vector>
47 #include "gromacs/utility/arrayref.h"
49 #include "ivaluestore.h"
51 namespace gmx
54 template<typename T>
55 class OptionValueStorePlain : public IOptionValueStore<T>
57 public:
58 OptionValueStorePlain(T* store, int* storeCount, int initialCount) :
59 count_(initialCount),
60 store_(store),
61 storeCount_(storeCount)
65 int valueCount() override { return count_; }
66 ArrayRef<T> values() override { return arrayRefFromArray(store_, count_); }
67 void clear() override
69 count_ = 0;
70 if (storeCount_ != nullptr)
72 *storeCount_ = count_;
75 void reserve(size_t /*count*/) override {}
76 void append(const T& value) override
78 store_[count_] = value;
79 ++count_;
80 if (storeCount_ != nullptr)
82 *storeCount_ = count_;
86 private:
87 int count_;
88 T* store_;
89 int* storeCount_;
92 template<typename T>
93 class OptionValueStoreVector : public IOptionValueStore<T>
95 public:
96 explicit OptionValueStoreVector(std::vector<T>* store) : store_(store) {}
98 int valueCount() override { return static_cast<int>(store_->size()); }
99 ArrayRef<T> values() override { return *store_; }
100 void clear() override { store_->clear(); }
101 void reserve(size_t count) override { store_->reserve(store_->size() + count); }
102 void append(const T& value) override { store_->push_back(value); }
104 private:
105 std::vector<T>* store_;
108 // Specialization that works around std::vector<bool> specialities.
109 template<>
110 class OptionValueStoreVector<bool> : public IOptionValueStore<bool>
112 public:
113 explicit OptionValueStoreVector(std::vector<bool>* store) : store_(store) {}
115 int valueCount() override { return static_cast<int>(store_->size()); }
116 ArrayRef<bool> values() override
118 return arrayRefFromArray(reinterpret_cast<bool*>(boolStore_.data()), boolStore_.size());
120 void clear() override
122 boolStore_.clear();
123 store_->clear();
125 void reserve(size_t count) override
127 boolStore_.reserve(boolStore_.size() + count);
128 store_->reserve(store_->size() + count);
130 void append(const bool& value) override
132 boolStore_.push_back({ value });
133 store_->push_back(value);
136 private:
137 struct Bool
139 bool value;
142 std::vector<Bool> boolStore_;
143 std::vector<bool>* store_;
146 /*! \internal
147 * \brief
148 * Value storage that does not store anywhere.
150 * This is needed because even though the values are not stored anywhere, the
151 * code still expects to access them later through valueCount() and values().
153 * \ingroup module_options
155 template<typename T>
156 class OptionValueStoreNull : public IOptionValueStore<T>
158 public:
159 OptionValueStoreNull() : store_(&vector_) {}
161 int valueCount() override { return store_.valueCount(); }
162 ArrayRef<T> values() override { return store_.values(); }
163 void clear() override { store_.clear(); }
164 void reserve(size_t count) override { store_.reserve(count); }
165 void append(const T& value) override { store_.append(value); }
167 private:
168 std::vector<T> vector_;
169 OptionValueStoreVector<T> store_;
172 } // namespace gmx
174 #endif