API of the c2py_module

  1// ==========  Module declaration ==========
  2
  3// Configuration of the module in the namespace c2py_module
  4// One module per file only
  5//
  6//  * Only the following variables, declarations, are authorized in this namespace,
  7//       anything else is an error (except "using namespace" clauses).
  8//
  9namespace c2py_module {
 10
 11  // Name of the package if any. Default is ""
 12  auto package_name = "Package"; // the module will be Package.MyModule
 13
 14  // The documentation string of the module. Default = ""
 15  auto documentation = "Module documentation";
 16
 17  // -------- Automatic selection of function, classes, enums -----------
 18  //
 19  // If match_names/match_names are set, filter for all elements (functions, classes, enums).
 20  // Both fields are regex.
 21  //
 22  // Algorithm is (for an element X of fully qualified name Qname)
 23  // if match_names and not match_names match Qname : skip X
 24  // if reject_names and reject_names match Qname : skip X
 25  // else keep X.
 26  auto match_names = "a regex";
 27
 28  auto reject_names = "a regex";
 29
 30  // Only keep elements which are declared in files matching the pattern
 31  auto match_files = "a regex"; // TODO implement.
 32
 33  // -----------------------------------------------------
 34  // An optional function `module_init` to be executed at the start of the module
 35  // Signature must be () -> void
 36  // nb : can be a function, or a lambda ...
 37  auto module_init = []() {};
 38
 39  // -----------------------------------------------------
 40  // Add in this namespace the function, classes
 41  // to be manually added (or renamed) to the automatic detection.
 42  // in particular the template instantiation
 43
 44  namespace add {
 45
 46    // ----------------------------------------------------------
 47    // List of Python functions to be added explicitly to the module
 48    // in addition/replacement of the automatically detected ones.
 49    // The Python function is a dynamic dispatch of the list of function (ordinary or template instantiation)
 50    // The name of declaration is the name of the function in Python
 51    //
 52    // NB :
 53    // - The automatically detected functions are equivalent to an implicit declaration
 54    // auto f = c2py::dispatch< all accepted f overloads>
 55    // This implicit declaration is overruled by an explicit declaration of f
 56
 57    // Examples:
 58    // declares a function h, dispatching the 2 instantiations.
 59    auto h = c2py::dispatch<N::h<int>, N::h<double>>;
 60
 61    // A function fg in Python which dynamically dispatch N::f, and N::g
 62    // We can regroup any set of functions we want in a dispatch, mixing template instantiation, ordinary function, etc...
 63    auto fg = c2py::dispatch<N::f, N::g>;
 64
 65    // Simply rename a function. NB: we should also reject N::f from automatic detection,
 66    // as this declaration does not remove the `f` function
 67    auto f_new_name = c2py::dispatch<N::f>;
 68
 69    // ---------- Properties ----------------
 70
 71    auto prop1 = c2py::property<x_getter, N::x_setter>;
 72    //
 73    // Or tag a getter function with
 74    // C2PY_PROPERTY
 75    // C2PY_PROPERTY_SET(NAME)
 76    //
 77    // getter_as_properties = "regex"; // for all classes matching the regex, the method () returning non void are transformed into a property
 78
 79    // ----------------------------------------------------------
 80    // List of struct/classes to be wrapped, in addition the ones automatically detected
 81
 82    // struct NameInPython = c2py::wrap<NameinC++>{};
 83    // some additional methods can be added to the class
 84
 85    using ClsPythonName = ClsCppName;
 86    // .e.g ...
 87    using A  = N::A;
 88    using Bi = N::B<int>;
 89
 90  } // namespace add
 91
 92  /// Additional methods to add to the Python class
 93  /// e.g. method template instantiation, or new injected function h
 94  template <> struct add_methods_to<A> {
 95    // NB : for some obscure reason, C++ requires the & for a template method, but it can be omitted for regular functions
 96    static constexpr auto h = c2py::dispatch<&N::A<int>::h<int>, N::h<double>>;
 97
 98    // A static method.
 99    // TODO : implement and test
100    static constexpr auto h1 = c2py::dispatch_static<&N::A<int>::h<int>, N::h<double>>;
101  };
102
103  // Note. One can also make a derived struct in C++
104  // with the additional methods, and wrap it, merging the parent methods by blacklisting the parent.
105  // TODO : improve the test/example
106
107  // ----------------------------------------------------------
108  // Arithmetic
109  // Must be declared manually, there are too many cases to auto detect.
110  // TODO make the arithmetic depends on a string for clearer syntax.
111
112  // Enumerate all the triplet A op B -> ReturnType for every op, and every class
113  template <> auto arithmetic<A, "+"> = std::tuple<std::tuple<A, A, R>, ...>{};
114  // ... other operators.
115
116  // Short cuts
117  // TODO implement
118  template <auto op> auto arithmetic<A, op> = c2py::arithmetic::algebra<A, double, op>{};
119
120} // namespace c2py_module