@@ -12,98 +12,139 @@ namespace py = pybind11;
1212
1313namespace number_based {
1414namespace internal {
15+
16+ /* !
17+ * \brief Bind the comparison policies to the Cache class
18+ *
19+ * This function binds the comparison policies to the Cache class. The policies
20+ * are passed as variadic template arguments. The function overloads the
21+ * `cached` function for each policy.
22+ */
1523template <typename CacheT, typename NumberT, typename ... ComparisonPolicyTs>
16- void bindPolicies (py::class_<CacheT, std::shared_ptr<CacheT>>& cacheClass) {
17- (cacheClass.def (
18- " cached" ,
19- [](CacheT& self, const NumberT& key, const ComparisonPolicyTs& policy) {
20- return self.template cached <ComparisonPolicyTs>(key, policy);
21- },
22- py::arg (" key" ),
23- py::arg (" policy" )),
24- ...);
24+ void bindPolicies (py::class_<CacheT, std::shared_ptr<CacheT>> &cacheClass) {
25+ (cacheClass.def (
26+ " cached" ,
27+ [](CacheT &self, const NumberT &key, const ComparisonPolicyTs &policy) {
28+ return self.template cached <ComparisonPolicyTs>(key, policy);
29+ },
30+ py::arg (" key" ), py::arg (" policy" )),
31+ ...);
2532}
2633} // namespace internal
2734
35+ /* !
36+ * \brief Bind the ApproximateNumber policy
37+ *
38+ * This function adds bindings for the ApproximateNumber policy to the given
39+ * python module under the given name.
40+ */
2841template <typename NumberT>
29- void bindApproximatePolicy (py::module & module , const std::string& name = " ApproximateNumber" ) {
30- using ApproximateNumberT = policies::ApproximateNumber<NumberT>;
31- py::class_<ApproximateNumberT, std::shared_ptr<ApproximateNumberT>>(module , name.c_str ())
32- .def (py::init<NumberT>(), py::arg (" threshold" ))
33- .def (" __call__" , &ApproximateNumberT::operator (), " Compare two numbers" );
42+ void bindApproximatePolicy (py::module &module ,
43+ const std::string &name = " ApproximateNumber" ) {
44+ using ApproximateNumberT = policies::ApproximateNumber<NumberT>;
45+ py::class_<ApproximateNumberT, std::shared_ptr<ApproximateNumberT>>(
46+ module , name.c_str ())
47+ .def (py::init<NumberT>(), py::arg (" threshold" ))
48+ .def (" __call__" , &ApproximateNumberT::operator (), " Compare two numbers" );
3449}
3550
3651/* !
3752 * \brief Bindings for a Cache that is based on number comparisons
3853 *
39- * This function binds the Cache class for a specific number-based key type (NumberT) and value type (ValueT).
40- * Call this function once inside PYBIND11_MODULE macro to create a Python module with the bindings.
54+ * This function binds the Cache class for a specific number-based key type
55+ * (NumberT) and value type (ValueT). Optionally, add a list of comparison
56+ * policies to the list of template parameters. The `cached` function will be
57+ * overloaded for each one of them. Call this function once inside
58+ * PYBIND11_MODULE macro to create the bindings for the Cache class.
4159 */
4260template <typename NumberT, typename ValueT, typename ... ComparisonPolicies>
43- void bindCache (py::module & module ) {
44- using CacheT = Cache<NumberT, ValueT>;
45- py::class_<CacheT, std::shared_ptr<CacheT>> cache (module , " Cache" );
46- cache
47- .def (py::init<>())
48- // We cannot pass template parameters to python functions, therefore we need to explicitly bind all
49- // instantiations to different python functions.
50- // We need to use the lambdas here to handle the seconds argument, defining the comparison policy.
51- .def (
52- " cached" ,
53- [](CacheT& self, const NumberT& key) { return self.template cached <std::equal_to<NumberT>>(key); },
54- py::arg (" key" ))
55- .def (" cache" , &CacheT::cache, py::arg (" key" ), py::arg (" value" ))
56- .def (" reset" , &CacheT::reset);
57-
58- internal::bindPolicies<CacheT, NumberT, ComparisonPolicies...>(cache);
61+ void bindCache (py::module &module ) {
62+ using CacheT = Cache<NumberT, ValueT>;
63+ py::class_<CacheT, std::shared_ptr<CacheT>> cache (module , " Cache" );
64+ cache
65+ .def (py::init<>())
66+ // We cannot pass template parameters to python functions, therefore we
67+ // need to explicitly bind all instantiations to different python
68+ // functions. We need to use the lambdas here to handle the seconds
69+ // argument, defining the comparison policy.
70+ .def (
71+ " cached" ,
72+ [](CacheT &self, const NumberT &key) {
73+ return self.template cached <std::equal_to<NumberT>>(key);
74+ },
75+ py::arg (" key" ))
76+ .def (" cache" , &CacheT::cache, py::arg (" key" ), py::arg (" value" ))
77+ .def (" reset" , &CacheT::reset);
78+
79+ internal::bindPolicies<CacheT, NumberT, ComparisonPolicies...>(cache);
5980}
6081
6182} // namespace number_based
6283
6384namespace time_based {
64-
6585namespace internal {
86+
87+ /* !
88+ * \brief Bind the comparison policies to the Cache class
89+ *
90+ * This function binds the comparison policies to the Cache class. The policies
91+ * are passed as variadic template arguments. The function overloads the
92+ * `cached` function for each policy.
93+ */
6694template <typename CacheT, typename TimeT, typename ... ComparisonPolicyTs>
67- void bindPolicies (py::class_<CacheT, std::shared_ptr<CacheT>>& cache) {
68- (cache.def (
69- " cached" ,
70- [](CacheT& self, const TimeT& key, const ComparisonPolicyTs& policy) {
71- return self.template cached <ComparisonPolicyTs>(key, policy);
72- },
73- py::arg (" key" ),
74- py::arg (" policy" )),
75- ...);
95+ void bindPolicies (py::class_<CacheT, std::shared_ptr<CacheT>> &cache) {
96+ (cache.def (
97+ " cached" ,
98+ [](CacheT &self, const TimeT &key, const ComparisonPolicyTs &policy) {
99+ return self.template cached <ComparisonPolicyTs>(key, policy);
100+ },
101+ py::arg (" key" ), py::arg (" policy" )),
102+ ...);
76103}
77104} // namespace internal
78105
106+ /* !
107+ * \brief Bind the ApproximateTime policy
108+ *
109+ * This function adds bindings for the ApproximateTime policy to the given
110+ * python module under the given name.
111+ */
79112template <typename TimeT, typename ThresholdTimeUnitT>
80- void bindApproximatePolicy (py::module & module , const std::string& name = " ApproximateTime" ) {
81- using ApproximateTimeT = policies::ApproximateTime<TimeT, ThresholdTimeUnitT>;
82- py::class_<ApproximateTimeT, std::shared_ptr<ApproximateTimeT>>(module , name.c_str ())
83- .def (py::init<double >(), py::arg (" threshold" ))
84- .def (" __call__" , &ApproximateTimeT::operator (), " Compare two time points" );
113+ void bindApproximatePolicy (py::module &module ,
114+ const std::string &name = " ApproximateTime" ) {
115+ using ApproximateTimeT = policies::ApproximateTime<TimeT, ThresholdTimeUnitT>;
116+ py::class_<ApproximateTimeT, std::shared_ptr<ApproximateTimeT>>(module ,
117+ name.c_str ())
118+ .def (py::init<double >(), py::arg (" threshold" ))
119+ .def (" __call__" , &ApproximateTimeT::operator (),
120+ " Compare two time points" );
85121}
86122
87123/* !
88- * \brief Bindings for a Cache that is based on time comparisons
124+ * \brief Bindings for a Cache that is based on time comparisons.
89125 *
90- * This function binds the Cache class for a specific time-based key type (TimeT) and value type (ValueT).
91- * Call this function once inside PYBIND11_MODULE macro to create a Python module with the bindings.
126+ * This function binds the Cache class for a specific time-based key type
127+ * (TimeT) and value type (ValueT). Optionally, add a list of comparison
128+ * policies to the list of template parameters. The `cached` function will be
129+ * overloaded for each one of them. Call this function once inside
130+ * PYBIND11_MODULE macro to create the bindings for the Cache class.
92131 */
93132template <typename TimeT, typename ValueT, typename ... ComparisonPolicyTs>
94- void bindCache (py::module & module ) {
95- using CacheT = Cache<TimeT, ValueT>;
96-
97- py::class_<CacheT, std::shared_ptr<CacheT>> cache (module , " Cache" );
98- cache.def (py::init<>())
99- .def (
100- " cached" ,
101- [](CacheT& self, const TimeT& key) { return self.template cached <std::equal_to<TimeT>>(key); },
102- py::arg (" key" ))
103- .def (" cache" , &CacheT::cache, py::arg (" key" ), py::arg (" value" ))
104- .def (" reset" , &CacheT::reset);
105-
106- internal::bindPolicies<CacheT, TimeT, ComparisonPolicyTs...>(cache);
133+ void bindCache (py::module &module ) {
134+ using CacheT = Cache<TimeT, ValueT>;
135+
136+ py::class_<CacheT, std::shared_ptr<CacheT>> cache (module , " Cache" );
137+ cache.def (py::init<>())
138+ .def (
139+ " cached" ,
140+ [](CacheT &self, const TimeT &key) {
141+ return self.template cached <std::equal_to<TimeT>>(key);
142+ },
143+ py::arg (" key" ))
144+ .def (" cache" , &CacheT::cache, py::arg (" key" ), py::arg (" value" ))
145+ .def (" reset" , &CacheT::reset);
146+
147+ internal::bindPolicies<CacheT, TimeT, ComparisonPolicyTs...>(cache);
107148}
108149
109150} // namespace time_based
0 commit comments