Skip to content

Commit 3891d92

Browse files
committed
Add wrap_registry_methods to handle reused wrappers.
Also add _docstrings module for easier definitions of docstrings to be used in the srreal_ext extension module.
1 parent 595de7a commit 3891d92

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

diffpy/srreal/_docstrings.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
##############################################################################
3+
#
4+
# diffpy.srreal Complex Modeling Initiative
5+
# (c) 2016 Brookhaven Science Associates,
6+
# Brookhaven National Laboratory.
7+
# All rights reserved.
8+
#
9+
# File coded by: Pavol Juhas
10+
#
11+
# See AUTHORS.txt for a list of people who contributed.
12+
# See LICENSE.txt for license information.
13+
#
14+
##############################################################################
15+
16+
17+
"""\
18+
Docstrings for classes and functions in srreal_ext module.
19+
"""
20+
21+
# Shared docstrings for classes derived from HasClassRegistry ----------------
22+
23+
def get_registry_docstrings(cls):
24+
"""Build a dictionary of docstrings per each HasClassRegistry method.
25+
26+
Parameters
27+
----------
28+
cls : class type that is wrapped
29+
This parameter is used to extract the class name and substitute
30+
it in the docstrings template.
31+
32+
Returns a dictionary mapping Python method names to their docstrins.
33+
"""
34+
n = cls.__name__
35+
rv = {k : v.replace('@NAME@', n) for k, v in (
36+
("create", doc_HasClassRegistry_create),
37+
("clone", doc_HasClassRegistry_clone),
38+
("type", doc_HasClassRegistry_type),
39+
("_registerThisType", doc_HasClassRegistry__registerThisType),
40+
("createByType", doc_HasClassRegistry_createByType),
41+
("getRegisteredTypes", doc_HasClassRegistry_getRegisteredTypes),
42+
)}
43+
return rv
44+
45+
46+
doc_HasClassRegistry_create = """\
47+
Return a new instance of the same type as self.
48+
49+
This method must be overloaded in a derived class.
50+
"""
51+
52+
53+
doc_HasClassRegistry_clone = """\
54+
Return a new instance that is a copy of self.
55+
56+
This method must be overloaded in a derived class.
57+
"""
58+
59+
60+
doc_HasClassRegistry_type = """\
61+
Return a unique string type that identifies a @NAME@-derived class.
62+
The string type is used for class registration and in the `createByType`
63+
function.
64+
65+
This method must be overloaded in a derived class.
66+
"""
67+
68+
69+
doc_HasClassRegistry__registerThisType = """\
70+
Add this class to the global registry of @NAME@ types.
71+
72+
This method must be called once after definition of the derived
73+
class to support pickling and the `createByType` factory.
74+
"""
75+
76+
77+
doc_HasClassRegistry_createByType = """\
78+
Return a new @NAME@ instance of the specified string type.
79+
80+
Parameters
81+
----------
82+
tp : str
83+
string type identifying a registered @NAME@ class.
84+
85+
Returns a new instance of the @NAME@-derived class.
86+
87+
See Also
88+
--------
89+
getRegisteredTypes : Return set of the recognized type strings.
90+
"""
91+
92+
93+
doc_HasClassRegistry_getRegisteredTypes = """\
94+
Return a set of string types of the registered @NAME@ classes.
95+
96+
These are the allowed arguments for the `createByType` factory.
97+
"""

srrealmodule/srreal_registry.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*****************************************************************************
2+
*
3+
* diffpy.srreal Complex Modeling Initiative
4+
* (c) 2016 Brookhaven Science Associates,
5+
* Brookhaven National Laboratory.
6+
* All rights reserved.
7+
*
8+
* File coded by: Pavol Juhas
9+
*
10+
* See AUTHORS.txt for a list of people who contributed.
11+
* See LICENSE.txt for license information.
12+
*
13+
******************************************************************************
14+
*
15+
* Utilities for wrapping classes that derive from HasClassRegistry.
16+
*
17+
*****************************************************************************/
18+
19+
#include <boost/python/object.hpp>
20+
#include <boost/python/import.hpp>
21+
22+
namespace srrealmodule {
23+
24+
using namespace boost::python;
25+
26+
27+
/// get dictionary of Python-defined docstrings for the cls class.
28+
object get_registry_docstrings(object& cls)
29+
{
30+
object mod = import("diffpy.srreal._docstrings");
31+
object getdocs = mod.attr("get_registry_docstrings");
32+
object rv = getdocs(cls);
33+
return rv;
34+
}
35+
36+
37+
} // namespace srrealmodule
38+
39+
// End of file

srrealmodule/srreal_registry.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SRREAL_REGISTRY_HPP_INCLUDED
2121

2222
#include <boost/python/object.hpp>
23+
#include <boost/python/extract.hpp>
2324

2425
namespace srrealmodule {
2526

@@ -66,6 +67,55 @@ class wrapper_registry_configurator
6667
mutable PyObject* mpyptr;
6768
};
6869

70+
71+
/// retrieve a dictionary of Python-defined docstrings for the cls class.
72+
::boost::python::object get_registry_docstrings(::boost::python::object& cls);
73+
74+
75+
/// helper wrapper function for return value conversion.
76+
template <class W>
77+
::boost::python::object getRegisteredTypes_asset()
78+
{
79+
return convertToPythonSet(W::getRegisteredTypes());
80+
}
81+
82+
83+
/// template function that wraps HasClassRegistry methods
84+
template <class C>
85+
C& wrap_registry_methods(C& boostpythonclass)
86+
{
87+
namespace bp = boost::python;
88+
using namespace boost::python;
89+
typedef typename C::wrapped_type::base B;
90+
typedef extract<const char*> CString;
91+
// get docstrings for the class registry methods.
92+
object d = get_registry_docstrings(boostpythonclass);
93+
const char* doc_create = CString(d["create"]);
94+
const char* doc_clone = CString(d["clone"]);
95+
const char* doc_type = CString(d["type"]);
96+
const char* doc__registerThisType = CString(d["_registerThisType"]);
97+
const char* doc_createByType = CString(d["createByType"]);
98+
const char* doc_getRegisteredTypes = CString(d["getRegisteredTypes"]);
99+
// define the class registry related methods.
100+
boostpythonclass
101+
.def("create", &B::create, doc_create)
102+
.def("clone", &B::clone, doc_clone)
103+
.def("type", &B::type,
104+
return_value_policy<copy_const_reference>(),
105+
doc_type)
106+
.def("_registerThisType", &B::registerThisType,
107+
doc__registerThisType)
108+
.def("createByType", &B::createByType,
109+
bp::arg("tp"), doc_createByType)
110+
.staticmethod("createByType")
111+
.def("getRegisteredTypes", getRegisteredTypes_asset<B>,
112+
doc_getRegisteredTypes)
113+
.staticmethod("getRegisteredTypes")
114+
;
115+
return boostpythonclass;
116+
}
117+
118+
69119
} // namespace srrealmodule
70120

71121
#endif // SRREAL_REGISTRY_HPP_INCLUDED

0 commit comments

Comments
 (0)