File tree 3 files changed +28
-6
lines changed 3 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -1282,22 +1282,36 @@ class py3_enum {
1282
1282
update ();
1283
1283
}
1284
1284
1285
- py3_enum& value (const char * name, T value) {
1286
- entries[name] = cast (static_cast <underlying_type>(value));
1287
- update ();
1285
+ py3_enum& value (const char * name, T value) & {
1286
+ add_entry (name, value);
1288
1287
return *this ;
1289
1288
}
1290
1289
1290
+ py3_enum&& value(const char * name, T value) && {
1291
+ add_entry (name, value);
1292
+ return std::move (*this );
1293
+ }
1294
+
1295
+ class_<T> extend () && {
1296
+ return cast<class_<T>>(type);
1297
+ }
1298
+
1291
1299
private:
1292
1300
const char *name;
1293
1301
handle scope;
1294
1302
dict entries;
1295
1303
object ctor;
1296
1304
object unique;
1297
1305
dict kwargs;
1306
+ object type;
1307
+
1308
+ void add_entry (const char *name, T value) {
1309
+ entries[name] = cast (static_cast <underlying_type>(value));
1310
+ update ();
1311
+ }
1298
1312
1299
1313
void update () {
1300
- object type = unique (ctor (**kwargs));
1314
+ type = unique (ctor (**kwargs));
1301
1315
setattr (scope, name, type);
1302
1316
detail::type_caster<T>::bind (type, entries);
1303
1317
}
Original file line number Diff line number Diff line change @@ -82,10 +82,14 @@ test_initializer enums([](py::module &m) {
82
82
auto scope = py::class_<DummyScope>(m, " DummyScope" );
83
83
py::py3_enum<Py3EnumEmpty>(scope, " Py3EnumEmpty" );
84
84
85
- py::py3_enum<Py3Enum>(m, " Py3Enum" )
85
+ auto e = py::py3_enum<Py3Enum>(m, " Py3Enum" )
86
86
.value (" A" , Py3Enum::A)
87
87
.value (" B" , Py3Enum::B)
88
- .value (" C" , Py3Enum::C);
88
+ .value (" C" , Py3Enum::C)
89
+ .extend ()
90
+ .def (" add" , [](Py3Enum x, int y) { return static_cast <int >(x) + y; })
91
+ .def_property_readonly (" is_b" , [](Py3Enum e) { return e == Py3Enum::B; })
92
+ .def_property_readonly_static (" ultimate_answer" , [](py::object) { return 42 ; });
89
93
90
94
py::py3_enum<Py3EnumScoped>(m, " Py3EnumScoped" )
91
95
.value (" X" , Py3EnumScoped::X)
Original file line number Diff line number Diff line change @@ -164,3 +164,7 @@ def test_py3_enum():
164
164
with pytest .raises (ValueError ) as excinfo :
165
165
non_unique_py3_enum ()
166
166
assert 'duplicate values found' in str (excinfo .value )
167
+
168
+ assert Py3Enum .ultimate_answer == 42
169
+ assert not Py3Enum .A .is_b and Py3Enum .B .is_b and not Py3Enum .C .is_b
170
+ assert Py3Enum .A .add (10 ) == - 32 and Py3Enum .C .add (- 1 ) == 41
You can’t perform that action at this time.
0 commit comments