Uncrustify all
[alexxy/gromacs-pyapi.git] / src / sip / definitions.sip
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 %Exception gmx::GromacsException(SIP_Exception) /Default/
37 {
38     %TypeHeaderCode
39 #include <gromacs/utility/exceptions.h>
40     %End
41     %RaiseCode
42     std::string detail = formatExceptionMessageToString(sipExceptionRef);
43
44     SIP_BLOCK_THREADS
45     PyErr_SetString(sipException_gmx_GromacsException, detail.data());
46     SIP_UNBLOCK_THREADS
47     %End
48 };
49
50 // This MappedType makes sure that unique_ptr does not get destructed while wrapped pointer is still needed.
51 // A class called 'TYPEPointer' needs to be created for every use.
52
53 template < TYPE >
54 %MappedType std::unique_ptr < TYPE > /NoRelease/ {
55
56     %ConvertFromTypeCode
57     PyObject *wrp = sipConvertFromType(sipCpp->get(), sipType_TYPE, NULL);
58     PyObject *ptr = sipConvertFromNewType(sipCpp, sipType_TYPEPointer, wrp);
59     // ptr is created only to bind wrp's ownership to it
60     GMX_UNUSED_VALUE(ptr);
61
62     return wrp;
63     %End
64
65     %ConvertToTypeCode
66     // Make it impossible to convert into this type
67     if (!sipIsErr)
68     {
69         return 0;
70     }
71
72     GMX_ASSERT(true, "Converting something into unique_ptr. This should not happen!");
73     return 0;
74     %End
75 };
76
77 %VirtualErrorHandler vehandler
78 SIP_RELEASE_GIL(sipGILState);
79
80 PyErr_PrintEx(1);
81 GMX_THROW(gmx::InternalError("Python virtual overload raised an exception, see traceback"));
82 %End
83
84 %ModuleHeaderCode
85 #include <functional>
86 int argv_wrapper(PyObject *, const std::function < int(int, char**) > &);
87 template < typename T > class PyFactory;
88 %End
89
90 // This is put into UnitPostIncludeCode so that sip* declarations are available.
91 %UnitPostIncludeCode
92 #include <memory>
93 #include <gromacs/utility/exceptions.h>
94 template < typename T > class PyFactory {
95     public:
96         PyFactory(const sipTypeDef *sipType, PyObject *mod) : sipType_(sipType), mod(mod) {
97         };
98         std::unique_ptr < T > operator () () {
99             int iserr = 0;
100             int can   = sipCanConvertToType(mod, sipType_, 0);
101
102             if (!can)
103             {
104                 GMX_THROW(gmx::APIError("Provided python object can not be converted to type required by runAsMain-like function"));
105             }
106
107             T *module = (T*) sipConvertToType(
108                     mod, sipType_, NULL, 0, NULL, &iserr);
109             return std::unique_ptr < T > (module);
110         }
111     private:
112         const sipTypeDef *sipType_;
113         PyObject         *mod;
114 };
115 %End
116
117 %ModuleCode
118 int argv_wrapper(PyObject *py_argv, const std::function < int(int, char**) > &runner)
119 {
120     int    argc = PyList_GET_SIZE(py_argv);
121
122     char **argv = new char *[argc + 1];
123     bool   to_free[argc];
124     std::fill(to_free, to_free+argc, false);
125
126     // Convert the list.
127     // TODO: Use something better than AsLatin1String to avoid unicode errors
128     for (int a = 0; a < argc; ++a)
129     {
130         PyObject   *arg_obj = PyList_GET_ITEM(py_argv, a);
131         const char *arg     = sipString_AsLatin1String(&arg_obj);
132
133         if (arg)
134         {
135             arg = strdup(arg);
136             Py_DECREF(arg_obj);
137             to_free[a] = true;
138         }
139         else
140         {
141             arg = "unknown";
142         }
143
144         argv[a] = const_cast < char * > (arg);
145     }
146
147     argv[argc] = NULL;
148
149     int result = runner(argc, argv);
150
151     for (int i = 0; i < argc; i++)
152     {
153         if (to_free[i])
154         {
155             free(argv[i]);
156         }
157     }
158     delete[] argv;
159
160     return result;
161 }
162 %End