source: src/Actions/Action_impl_python.hpp

Candidate_v1.6.1
Last change on this file was e7ad08, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

Python interface now converts dashes in tokens to underscores, store-session used keyword arguments.

  • MoleCuilder's python functions (i.e. commands) typically have quite a number of arguments and therefore can be easily confused. We circumvent this by using keyword (or named) arguments that are also independent of position. Moreover, in that case only the non-default arguments need to be given.
  • TESTS: Marked failing python tests as XFAIL for the moment.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Action_impl_python.hpp
3 *
4 * Created on: Sep 25, 2011
5 * Author: heber
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <boost/preprocessor/cat.hpp>
14#include <boost/preprocessor/comparison/equal.hpp>
15#include <boost/preprocessor/control/if.hpp>
16#include <boost/preprocessor/control/expr_if.hpp>
17#include <boost/preprocessor/debug/assert.hpp>
18#include <boost/preprocessor/facilities/expand.hpp>
19#include <boost/preprocessor/iteration/local.hpp>
20#include <boost/preprocessor/list/adt.hpp>
21#include <boost/preprocessor/punctuation/comma_if.hpp>
22#include <boost/preprocessor/punctuation/paren.hpp>
23#include <boost/preprocessor/seq/elem.hpp>
24#include <boost/preprocessor/seq/size.hpp>
25#include <boost/preprocessor/seq/transform.hpp>
26
27// some derived names: if CATEGORY is not given, we don't prefix with it
28#ifdef CATEGORY
29#define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
30#define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
31#define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
32#else
33#define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
34#define COMMAND ACTIONNAME
35#define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
36#endif
37
38// for paramdefaults entries
39#define PARAM_DEFAULT(x) \
40 (x, BOOST_PP_NIL)
41
42// check if no lists given
43#ifndef paramtokens
44#define MAXPARAMTYPES 0
45#else
46#define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtokens)
47#endif
48
49// calculate numbers and check whether all have same size
50#ifdef paramtokens
51BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdescriptions)),\
52 ERROR: There are not the same number of "paramtokens" and "paramdescriptions" in: __FILE__ \
53)
54#endif
55#ifdef paramreferences
56BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
57 ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
58)
59#endif
60
61#ifdef paramtypes
62BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtypes)),\
63 ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
64)
65#endif
66
67// print a list of "action separator description linenend, i.e. "Action - descrption\n"
68#define help_print(z,n,TYPELIST, VARLIST, separator, lineend) \
69 BOOST_PP_IF(n, +, BOOST_PP_EMPTY()) \
70 "\t" + std::string( BOOST_PP_SEQ_ELEM(n, TYPELIST) ) + \
71 std::string( separator ) + \
72 std::string( BOOST_PP_SEQ_ELEM(n, VARLIST) ) + \
73 std::string( lineend )
74
75// print a list of comma-separated list, i.e. (,)arg("Action")=default
76#define stringdefault_print(z,n,STRINGLIST, DEFAULTLIST) \
77 BOOST_PP_COMMA_IF(n) \
78 boost::python::arg( \
79 MoleCuilder::normalizeToken( BOOST_PP_SEQ_ELEM(n, STRINGLIST) ).c_str() \
80 ) \
81 = \
82 BOOST_PP_IF( \
83 BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, paramdefaults) ) ), \
84 toString BOOST_PP_LPAREN() \
85 BOOST_PP_LIST_FIRST( BOOST_PP_SEQ_ELEM(n, DEFAULTLIST) ) \
86 BOOST_PP_RPAREN(), \
87 std::string("") \
88 )
89
90// print a list of comma-separated list, i.e. (,)arg("Action")
91#define string_print(z,n,STRINGLIST) \
92 BOOST_PP_COMMA_IF(n) \
93 boost::python::arg( \
94 MoleCuilder::normalizeToken( BOOST_PP_SEQ_ELEM(n, STRINGLIST) ).c_str() \
95 )
96
97// print a list of type ref followed, i.e. "int i, double position"
98#define type_list(z,n,TYPELIST, VARLIST) \
99 BOOST_PP_COMMA_IF(n) \
100 BOOST_PP_SEQ_ELEM(n, TYPELIST) \
101 BOOST_PP_SEQ_ELEM(n, VARLIST)
102
103// prints set/queryCurrentValue (command) for paramreferences and paramtokens
104#define value_print(z, n, container, prefix) \
105 prefix \
106 BOOST_PP_SEQ_ELEM(n, container) \
107 .set( \
108 BOOST_PP_SEQ_ELEM(n, container) \
109 );
110
111#define stringtype std::string
112
113#define type2string(s, data, elem) \
114 stringtype
115
116#include "Actions/Action.hpp"
117
118namespace MoleCuilder {
119#ifdef returntype
120 returntype
121#else
122 void
123#endif
124BOOST_PP_CAT( COMMAND, _stringargs)(
125#if defined paramtypes
126#define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, ~, paramtypes), paramreferences)
127#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
128#include BOOST_PP_LOCAL_ITERATE()
129#endif
130 );
131}
132
133void BOOST_PP_CAT(export_, COMMAND)()
134{
135 std::string docstring =
136 std::string( DESCRIPTION ) + "\n\n"
137#if defined paramtokens && defined paramdescriptions
138#define BOOST_PP_LOCAL_MACRO(n) help_print(~, n, paramtokens, paramdescriptions, " - ", "\n")
139#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
140#include BOOST_PP_LOCAL_ITERATE()
141#endif
142 ;
143 boost::python::def(
144 BOOST_PP_STRINGIZE(COMMAND),
145 MoleCuilder:: BOOST_PP_CAT( COMMAND, _stringargs)
146#if defined paramtokens // do we have parameters at all?
147 ,(
148#if defined paramdefaults
149#define BOOST_PP_LOCAL_MACRO(n) stringdefault_print(~, n, paramtokens, paramdefaults)
150#else
151#define BOOST_PP_LOCAL_MACRO(n) string_print(~, n, paramtokens)
152#endif
153#define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
154#include BOOST_PP_LOCAL_ITERATE()
155 ), docstring.c_str()
156#else
157 , DESCRIPTION
158#endif
159 );
160}
161
162#undef COMMAND
163#undef ACTION
164#undef PARAMS
165#undef MAXPARAMTYPES
166#undef PARAM_DEFAULT
167
168#undef help_print
169#undef string_print
170#undef stringdefault_print
171#undef type_list
172#undef value_print
173
174#undef type2string
175#undef stringtype
Note: See TracBrowser for help on using the repository browser.