2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015, 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.
38 #include "gromacs/options/basicoptions.h"
39 #include "gromacs/options/filenameoption.h"
40 #include "gromacs/selection/selectionoption.h"
44 class PyOptionsHolder {
46 class DuplicateOption : public std::exception {
48 virtual const char* what() const noexcept;
52 StringList(const std::vector<std::string>*);
53 StringList(const std::string*, size_t);
54 const char* operator[] (size_t);
56 const std::string *list;
58 const std::vector<std::string> *vector;
60 gmx::DoubleOption doubleOption(const char*, size_t = 1, double* = NULL);
61 gmx::IntegerOption integerOption(const char*, size_t = 1, int* = NULL);
62 gmx::StringOption stringOption(const char*, size_t = 1, const char* = NULL);
63 gmx::BooleanOption booleanOption(const char*, size_t = 1, bool = 0);
64 gmx::SelectionOption selectionOption(const char*, size_t = 1);
65 gmx::FileNameOption fileNameOption(const char*, size_t = 1);
66 PyObject* get_value(const char*);
76 std::map<std::string, option_value> storage;
77 template<typename T, typename U> U createStorage(const char*, const char*, int, const T* = 0);
78 template<typename T> PyObject* buildValue(const char*, const option_value&, const sipTypeDef* = NULL, size_t = 0);
79 template<typename T> void deleteStorage(const option_value&);
84 #include "gromacs/utility/gmxassert.h"
86 const char* PyOptionsHolder::DuplicateOption::what() const noexcept {
87 return "This option is already defined";
90 PyOptionsHolder::StringList::StringList(const std::vector<std::string> *vector) :
91 list(NULL), size(vector->size()), vector(vector)
94 PyOptionsHolder::StringList::StringList(const std::string *list, size_t size) :
95 list(list), size(size), vector(NULL)
98 const char* PyOptionsHolder::StringList::operator[](size_t i) {
101 return vector ? (*vector)[i].data() : list[i].data();
104 template<typename T, typename U> U PyOptionsHolder::createStorage(const char *name, const char *type, int count, const T *def) {
105 if (storage.count(name))
106 throw DuplicateOption();
112 value.count = new int;
115 if (count == 0) { // std::vector of values
116 auto *store = new std::vector<T>();
117 option.storeVector(store);
119 option.storeCount(value.count);
123 store->push_back(*def);
129 } else { // exactly `count` values
130 auto *store = new T[count];
136 option.valueCount(count);
139 value.vector = false;
140 *value.count = count;
143 storage[name] = value;
147 gmx::DoubleOption PyOptionsHolder::doubleOption(const char *name, size_t count, double *def) {
148 gmx::DoubleOption option = createStorage<double, gmx::DoubleOption>(name, "d", count, def);
153 gmx::IntegerOption PyOptionsHolder::integerOption(const char *name, size_t count, int *def) {
154 gmx::IntegerOption option = createStorage<int, gmx::IntegerOption>(name, "i", count, def);
159 gmx::StringOption PyOptionsHolder::stringOption(const char *name, size_t count, const char* def) {
160 std::string *s_def = NULL;
162 s_def = new std::string(def);
164 gmx::StringOption option = createStorage<std::string, gmx::StringOption>(name, "A", count, s_def);
170 // FIXME: Unify with the rest
171 gmx::BooleanOption PyOptionsHolder::booleanOption(const char *name, size_t count, bool def) {
172 if (storage.count(name))
173 throw DuplicateOption();
175 bool *store = new bool;
177 gmx::BooleanOption option(name);
179 storage[name] = {store, "b"};
184 gmx::SelectionOption PyOptionsHolder::selectionOption(const char *name, size_t count) {
185 gmx::SelectionOption option = createStorage<gmx::Selection, gmx::SelectionOption>(name, "S", count);
190 gmx::FileNameOption PyOptionsHolder::fileNameOption(const char *name, size_t count) {
191 gmx::FileNameOption option = createStorage<std::string, gmx::FileNameOption>(name, "f", count);
196 template<typename T> PyObject* PyOptionsHolder::buildValue(const char *sipType, const PyOptionsHolder::option_value &value, const sipTypeDef *td, size_t size) {
197 int count = *value.count;
198 T *store = !value.vector ? (T*) value.value : ((std::vector<T>*) value.value)->data();
200 if (count == 1 && !value.vector) {
202 return sipConvertFromType(store, td, NULL);
204 return sipBuildResult(NULL, sipType, *store);
207 return sipConvertToTypedArray(store, td, NULL, size, count, SIP_READ_ONLY);
209 return sipConvertToArray(store, sipType, count, SIP_READ_ONLY);
214 PyObject* PyOptionsHolder::get_value(const char *name) {
215 if (!storage.count(name))
218 option_value v = storage[name];
221 return buildValue<double>("d", v);
224 return buildValue<int>("i", v);
226 case 'A': // std::string
227 case 'f': // FileNameOption
229 return sipConvertFromType(new PyOptionsHolder::StringList(((std::vector<std::string>*) v.value)), sipType_PyOptionsHolder_StringList, Py_None);
230 else if (*v.count == 1)
231 return sipBuildResult(NULL, "A", ((std::string*) v.value)->data());
233 return sipConvertFromType(new PyOptionsHolder::StringList((std::string*) v.value, *v.count), sipType_PyOptionsHolder_StringList, Py_None);
236 // TODO: support vector bool options
237 return sipBuildResult(NULL, v.type, *((bool*) v.value));
239 case 'S': // Selection
240 return buildValue<gmx::Selection>(NULL, v, sipType_Selection, sizeof(gmx::Selection));
244 GMX_ASSERT(false, "Some type is not handled in PyOptionsHolder.get_value");
248 template<typename T> void PyOptionsHolder::deleteStorage(const PyOptionsHolder::option_value &value) {
250 delete (std::vector<T> *) value.value;
252 delete[] (T*) value.value;
257 PyOptionsHolder::~PyOptionsHolder() {
258 for (auto e : storage)
259 switch (*e.second.type) {
261 deleteStorage<double>(e.second);
264 deleteStorage<int>(e.second);
266 case 'A': // std::string
267 case 'f': // FileNameOption (the storage type is still std::string)
268 deleteStorage<std::string>(e.second);
271 delete (bool*) e.second.value;
273 case 'S': // Selection
274 deleteStorage<gmx::Selection>(e.second);
280 %Exception PyOptionsHolder::DuplicateOption {
283 PyErr_SetString(PyExc_ValueError, sipExceptionRef.what());
288 class PyOptionsHolder {
290 #include "gromacs/options/basicoptions.h"
294 class StringList /NoDefaultCtors/ {
296 const char* operator[] (int);
298 sipRes = (*sipCpp)[a0];
302 PyErr_SetString(PyExc_IndexError, "Index out of range");
308 // These methods are given twice to workaround sip setting 0 for when '*def = NULL' option is not given
309 DoubleOption doubleOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
310 DoubleOption doubleOption(const char *name /KeepReference/, int count, const double *def) throw (PyOptionsHolder::DuplicateOption);
312 IntegerOption integerOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
313 IntegerOption integerOption(const char *name /KeepReference/, int count, const int *def) throw (PyOptionsHolder::DuplicateOption);
315 StringOption stringOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
316 StringOption stringOption(const char *name /KeepReference/, int count, const char *def) throw (PyOptionsHolder::DuplicateOption);
318 BooleanOption booleanOption(const char *name /KeepReference/, int count = 1, bool def = 0) throw (PyOptionsHolder::DuplicateOption);
320 SelectionOption selectionOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
322 FileNameOption fileNameOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
323 SIP_PYOBJECT __getitem__(const char *name);
325 sipRes = sipCpp->get_value(a0);
329 PyErr_SetString(PyExc_KeyError, "Invalid option name");