Skip to content
Snippets Groups Projects
Commit 2a32d8cb authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

gmshc.hpp -> gmsh.h_cwrap

parent d5b7a03b
Branches
Tags
1 merge request!112C++ wrapper for C API
Pipeline #
......@@ -22,7 +22,7 @@ class arg:
self.c_post = ""
self.c = type_c + " " + name
self.hpp_arg = self.name
self.cwrap_arg = self.name
self.python_arg = "not_implemented"
self.python_return = "not_implemented"
......@@ -33,7 +33,7 @@ class arg:
def ibool(name, value=None, python_value=None):
a = arg(name, value, python_value, "const bool", "const int", False)
a.python_arg = "c_int(bool(" + name + "))"
a.hpp_arg = "int(" + name + ")"
a.cwrap_arg = "int(" + name + ")"
return a
def iint(name, value=None, python_value=None):
......@@ -49,7 +49,7 @@ def idouble(name, value=None, python_value=None):
def istring(name, value=None, python_value=None):
a = arg(name, value, python_value, "const std::string &", "const char *", False)
a.python_arg = "c_char_p(" + name + ".encode())"
a.hpp_arg = name + ".c_str()"
a.cwrap_arg = name + ".c_str()"
return a
def ivectorint(name, value=None, python_value=None):
......@@ -57,6 +57,7 @@ def ivectorint(name, value=None, python_value=None):
a.c_pre = " std::vector<int> api_" + name + "_(" + name + ", " + name + " + " + name + "_n);\n"
a.c_arg = "api_" + name + "_"
a.c = "int * " + name + ", size_t " + name + "_n"
a.cwrap_arg = "&" + name + "[0], " + name + ".size()"
a.python_pre = "api_" + name + "_, api_" + name + "_n_ = _ivectorint(" + name + ")"
a.python_arg = "api_" + name + "_, api_" + name + "_n_"
return a
......@@ -66,6 +67,7 @@ def ivectordouble(name, value=None, python_value=None):
a.c_pre = " std::vector<double> api_" + name + "_(" + name + ", " + name + " + " + name + "_n);\n"
a.c_arg = "api_" + name + "_"
a.c = "double * " + name + ", size_t " + name + "_n"
a.cwrap_arg = "&" + name + "[0], " + name + ".size()"
a.python_pre = "api_" + name + "_, api_" + name + "_n_ = _ivectordouble(" + name + ")"
a.python_arg = "api_" + name + "_, api_" + name + "_n_"
return a
......@@ -79,6 +81,9 @@ def ivectorpair(name, value=None, python_value=None):
" }\n"
a.c_arg = "api_" + name + "_"
a.c = "int * " + name + ", size_t " + name + "_n"
a.cwrap_pre = "TODO pre vector pair\n"
a.cwrap_arg = "TODO arg vector pair"
a.cwrap_post = "TODO post vector pair\n"
a.python_pre = "api_" + name + "_, api_" + name + "_n_ = _ivectorpair(" + name + ")"
a.python_arg = "api_" + name + "_, api_" + name + "_n_"
return a
......@@ -90,6 +95,9 @@ def ivectorvectorint(name, value=None, python_value=None):
" api_" + name + "_[i] = std::vector<int>(" + name + "[i], " + name + "[i] + " + name + "_n[i]);\n"
a.c_arg = "api_" + name + "_"
a.c = "const int ** " + name + ", const size_t * " + name + "_n, " + "size_t " + name + "_nn"
a.cwrap_pre = "TODO pre vector int\n"
a.cwrap_arg = "TODO arg vector int"
a.cwrap_post = "TODO post vector int\n"
a.python_pre = "api_" + name + "_, api_" + name + "_n_, api_" + name + "_nn_ = _ivectorvectorint(" + name + ")"
a.python_arg = "api_" + name + "_, api_" + name + "_n_, api_" + name + "_nn_"
return a
......@@ -101,6 +109,9 @@ def ivectorvectordouble(name, value=None, python_value=None):
" api_" + name + "_[i] = std::vector<double>(" + name + "[i], " + name + "[i] + " + name + "_n[i]);\n"
a.c_arg = "api_" + name + "_"
a.c = "const double ** " + name + ", const size_t * " + name + "_n, " + "size_t " + name + "_nn"
a.cwrap_pre = "TODO pre vector vector double\n"
a.cwrap_arg = "TODO arg vector vector double"
a.cwrap_post = "TODO post vector vector double\n"
a.python_pre = "api_" + name + "_, api_" + name + "_n_, api_" + name + "_nn_ = _ivectorvectordouble(" + name + ")"
a.python_arg = "api_" + name + "_, api_" + name + "_n_, api_" + name + "_nn_"
return a
......@@ -247,6 +258,7 @@ def argcargv() :
a.c = "int argc, char ** argv"
a.c_pre = ""
a.c_post = ""
a.cwrap_arg = "argc, argv"
a.name = "argv"
a.python_value = "[]"
a.python_arg = "api_argc_, api_argv_"
......@@ -428,23 +440,25 @@ void gmshFree(void *p) {
"""
c_hpp_header="""// Gmsh - Copyright (C) 1997-2018 C. Geuzaine, J.-F. Remacle
cwrap_header="""// Gmsh - Copyright (C) 1997-2018 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to the public mailing list <gmsh@onelab.info>.
#ifndef _GMSHC_HPP_
#define _GMSHC_HPP_
#ifndef _GMSH_H_
#define _GMSH_H_
// This file defines C++ bindings for the Gmsh C API. This file is provided
// as a convenience for users of the binary Gmsh SDK, whose C++ compiler ABI
// is not compatible with the ABI of the C++ compiler used to create the SDK
// This file redefines the C++ API in terms of the Gmsh C API. This file is
// provided as a convenience for users of the binary Gmsh SDK, whose C++ compiler
// ABI is not compatible with the ABI of the C++ compiler used to create the SDK
// (and who can thus not directly use the C++ API defined in `gmsh.h').
//
// Using this header file will lead to (slightly) reduced performance compared
// to using the native Gmsh C++ API defined in `gmsh.h', as it entails
// additional data copies between this C++ wrapper, the C API and the native
// C++ code.
// To use these C++ bindings of the C API instead of the native C++ API, simply
// rename this file as `gmsh.h'.
//
// Warning: using this header file will lead to (slightly) reduced performance
// compared to using the native Gmsh C++ API, as it entails additional data copies
// between this C++ wrapper, the C API and the native C++ code.
//
// Do not edit this file directly: it is automatically generated by `api/gen.py'.
......@@ -479,7 +493,7 @@ namespace gmsh {
"""
c_hpp_footer="""#endif
cwrap_footer="""#endif
"""
python_header = """# Gmsh - Copyright (C) 1997-2018 C. Geuzaine, J.-F. Remacle
......@@ -667,7 +681,7 @@ class API:
c_namespace += module.name[0].upper() + module.name[1:]
else:
c_namespace = module.name
fhpp.write(indent + "namespace " + module.name + " { // " + module.doc + "\n\n")
fcwrap.write(indent + "namespace " + module.name + " { // " + module.doc + "\n\n")
indent += " "
for rtype, name, args, doc, rawc in module.fs:
# gmshc.h
......@@ -698,38 +712,39 @@ class API:
if rtype:
fc.write(" return result_api_;\n");
fc.write("}\n\n")
# gmshc.hpp
fhpp.write(indent + "// " + ("\n" + indent + "// ").join(textwrap.wrap(doc, 80-len(indent))) + "\n")
# gmsh.h_cwrap
fcwrap.write(indent + "// " + ("\n" + indent + "// ").join(textwrap.wrap(doc, 80-len(indent))) + "\n")
rt = rtype.rtype_cpp if rtype else "void"
fnameapi = indent + "GMSH_API " + rt + " " + name + "(";
fhpp.write(fnameapi)
fcwrap.write(fnameapi)
if args:
fhpp.write((",\n" + ' ' * len(fnameapi)).join(a.cpp for a in args))
fhpp.write(")\n" + indent + "{\n" + indent + " int ierr = 0;\n" + indent + " ")
fcwrap.write((",\n" + ' ' * len(fnameapi)).join(a.cpp for a in args))
fcwrap.write(")\n" + indent + "{\n" + indent + " int ierr = 0;\n" + indent + " ")
if rtype:
fhpp.write("int result_api_ = ")
fhpp.write(fname + "(")
fcwrap.write("int result_api_ = ")
fcwrap.write(fname + "(" + ", ".join((a.cwrap_arg for a in args)))
fhpp.write(", ".join((a.hpp_arg for a in args)))
fhpp.write(", &ierr);\n")
fhpp.write(indent + " " + "if(ierr) throw ierr;\n");
if args:
fcwrap.write(", &ierr);\n")
else:
fcwrap.write("&ierr);\n")
fcwrap.write(indent + " " + "if(ierr) throw ierr;\n");
if rtype:
fhpp.write(indent + " return result_api_;\n")
fhpp.write(indent + "}\n\n")
fcwrap.write(indent + " return result_api_;\n")
fcwrap.write(indent + "}\n\n")
for m in module.submodules:
write_module(m, c_namespace, cpp_namespace, indent)
fhpp.write(indent[:-2] + "} // namespace " + module.name + "\n\n")
fcwrap.write(indent[:-2] + "} // namespace " + module.name + "\n\n")
with open("gmshc.h", "w") as f:
with open("gmshc.cpp", "w") as fc:
with open("gmshc.hpp", "w") as fhpp :
with open("gmsh.h_cwrap", "w") as fcwrap:
f.write(c_header)
fc.write(c_cpp_header)
fhpp.write(c_hpp_header)
fcwrap.write(cwrap_header)
for module in self.modules:
write_module(module, "", "", "")
f.write(c_footer)
fhpp.write(c_hpp_footer)
fcwrap.write(cwrap_footer)
def write_python(self):
def parg(a):
......
......@@ -2,11 +2,19 @@ This directory contains the Gmsh API:
- gmsh.h: the header defining the Gmsh C++ API
- gmshc.h: the header defining the Gmsh C API
- gmshc.cpp: the wrapper for the Gmsh C API
- gmsh.py: the module defining the Gmsh Python API
These files are all automatically generated by the Python script 'gen.py'.
The additional file `gmsh.h_crwap' redefines the C++ API in terms of the C API.
This is provided as a convenience for users of the binary Gmsh SDK, whose C++
compiler ABI is not compatible with the ABI of the C++ compiler used to create
the SDK. To use these C++ bindings of the C API instead of the native C++ API,
simply rename `gmsh.h_cwrap' as `gmsh.h'. Warning: this will lead to (slightly)
reduced performance compared to using the native Gmsh C++ API, as it entails
additional data copies between the C++ wrapper, the C API and the native C++
code.
See `gmsh/demos/api' for examples on how to use the Gmsh API. In particular,
this directory contains C++ and Python versions of several of the `.geo'
tutorials from `gmsh/tutorials'.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment