Update headers, fix style for some py files
[alexxy/gromacs.git] / src / python / sip / options / pyoptionsholder.sip
index 5f04152467632ea9d4d29f465fbbc4a2a69f75ee..0b4e57412de6fd8ae857d73110087c77e3e0f613 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2014, by the GROMACS development team, led by
+ * Copyright (c) 2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -57,12 +57,12 @@ public:
         size_t size;
         const std::vector<std::string> *vector;
     };
-    gmx::DoubleOption doubleOption(const char*, size_t = 1, double = 0);
-    gmx::IntegerOption integerOption(const char*, size_t = 1, int = 0);
-    gmx::StringOption stringOption(const char*, size_t = 1, const char* = "");
+    gmx::DoubleOption doubleOption(const char*, size_t = 1, double* = NULL);
+    gmx::IntegerOption integerOption(const char*, size_t = 1, int* = NULL);
+    gmx::StringOption stringOption(const char*, size_t = 1, const char* = NULL);
     gmx::BooleanOption booleanOption(const char*, size_t = 1, bool = 0);
     gmx::SelectionOption selectionOption(const char*, size_t = 1);
-    gmx::FileNameOption fileNameOption(const char*, size_t = 1, const char* = 0);
+    gmx::FileNameOption fileNameOption(const char*, size_t = 1);
     PyObject* get_value(const char*);
     ~PyOptionsHolder();
 private:
@@ -74,7 +74,7 @@ private:
         const char *name;
     };
     std::map<std::string, option_value> storage;
-    template<typename T, typename U> U createStorage(const char*, const char*, int);
+    template<typename T, typename U> U createStorage(const char*, const char*, int, const T* = 0);
     template<typename T> PyObject* buildValue(const char*, const option_value&, const sipTypeDef* = NULL, size_t = 0);
     template<typename T> void deleteStorage(const option_value&);
 };
@@ -101,55 +101,68 @@ const char* PyOptionsHolder::StringList::operator[](size_t i) {
     return vector ? (*vector)[i].data() : list[i].data();
 }
 
-template<typename T, typename U> U PyOptionsHolder::createStorage(const char *name, const char *type, int count) {
+template<typename T, typename U> U PyOptionsHolder::createStorage(const char *name, const char *type, int count, const T *def) {
     if (storage.count(name))
         throw DuplicateOption();
 
     U option(name);
 
-    int *saved_count = new int;
-    option.storeCount(saved_count);
-
     option_value value;
     value.type = type;
-    value.count = saved_count;
+    value.count = new int;
     value.name = name;
 
     if (count == 0) { // std::vector of values
         std::vector<T> *store = new std::vector<T>();
         option.storeVector(store);
         option.multiValue();
+        option.storeCount(value.count);
+
+        *value.count = 0;
+        if (def) {
+            store->push_back(*def);
+            *value.count = 1;
+        }
 
         value.value = store;
         value.vector = true;
     } else { // exactly `count` values
         T *store = new T[count];
+        if (def)
+            store[0] = *def;
+
         option.store(store);
         if (count > 1)
             option.valueCount(count);
 
         value.value = store;
         value.vector = false;
+        *value.count = count;
     }
 
     storage[name] = value;
     return option;
 }
 
-gmx::DoubleOption PyOptionsHolder::doubleOption(const char *name, size_t count, double def) {
-    gmx::DoubleOption option = createStorage<double, gmx::DoubleOption>(name, "d", count);
+gmx::DoubleOption PyOptionsHolder::doubleOption(const char *name, size_t count, double *def) {
+    gmx::DoubleOption option = createStorage<double, gmx::DoubleOption>(name, "d", count, def);
 
     return option;
 }
 
-gmx::IntegerOption PyOptionsHolder::integerOption(const char *name, size_t count, int def) {
-    gmx::IntegerOption option = createStorage<int, gmx::IntegerOption>(name, "i", count);
+gmx::IntegerOption PyOptionsHolder::integerOption(const char *name, size_t count, int *def) {
+    gmx::IntegerOption option = createStorage<int, gmx::IntegerOption>(name, "i", count, def);
 
     return option;
 }
 
 gmx::StringOption PyOptionsHolder::stringOption(const char *name, size_t count, const char* def) {
-    gmx::StringOption option = createStorage<std::string, gmx::StringOption>(name, "A", count);
+    std::string *s_def = NULL;
+    if (def)
+        s_def = new std::string(def);
+
+    gmx::StringOption option = createStorage<std::string, gmx::StringOption>(name, "A", count, s_def);
+    delete s_def;
 
     return option;
 }
@@ -175,7 +188,7 @@ gmx::SelectionOption PyOptionsHolder::selectionOption(const char *name, size_t c
     return option;
 }
 
-gmx::FileNameOption PyOptionsHolder::fileNameOption(const char *name, size_t count, const char* def) {
+gmx::FileNameOption PyOptionsHolder::fileNameOption(const char *name, size_t count) {
     gmx::FileNameOption option = createStorage<std::string, gmx::FileNameOption>(name, "f", count);
 
     return option;
@@ -293,12 +306,21 @@ public:
             }
         %End
     };
-    DoubleOption doubleOption(const char *name /KeepReference/, int count = 1, double def = 0) throw (PyOptionsHolder::DuplicateOption);
-    IntegerOption integerOption(const char *name /KeepReference/, int count = 1, int def = 0) throw (PyOptionsHolder::DuplicateOption);
-    StringOption stringOption(const char *name /KeepReference/, int count = 1, const char *def = 0) throw (PyOptionsHolder::DuplicateOption);
+    // These methods are given twice to workaround sip setting 0 for when '*def = NULL' option is not given
+    DoubleOption doubleOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
+    DoubleOption doubleOption(const char *name /KeepReference/, int count, const double *def) throw (PyOptionsHolder::DuplicateOption);
+
+    IntegerOption integerOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
+    IntegerOption integerOption(const char *name /KeepReference/, int count, const int *def) throw (PyOptionsHolder::DuplicateOption);
+
+    StringOption stringOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
+    StringOption stringOption(const char *name /KeepReference/, int count, const char *def) throw (PyOptionsHolder::DuplicateOption);
+
     BooleanOption booleanOption(const char *name /KeepReference/, int count = 1, bool def = 0) throw (PyOptionsHolder::DuplicateOption);
+
     SelectionOption selectionOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
-    FileNameOption fileNameOption(const char *name /KeepReference/, int count = 1, const char *def = 0) throw (PyOptionsHolder::DuplicateOption);
+
+    FileNameOption fileNameOption(const char *name /KeepReference/, int count = 1) throw (PyOptionsHolder::DuplicateOption);
     SIP_PYOBJECT __getitem__(const char *name);
     %MethodCode
         sipRes = sipCpp->get_value(a0);