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 @@ -1279,22 +1279,36 @@ class py3_enum {
1279
1279
update ();
1280
1280
}
1281
1281
1282
- py3_enum& value (const char * name, T value) {
1283
- entries[name] = cast (static_cast <underlying_type>(value));
1284
- update ();
1282
+ py3_enum& value (const char * name, T value) & {
1283
+ add_entry (name, value);
1285
1284
return *this ;
1286
1285
}
1287
1286
1287
+ py3_enum&& value(const char * name, T value) && {
1288
+ add_entry (name, value);
1289
+ return std::move (*this );
1290
+ }
1291
+
1292
+ class_<T> extend () && {
1293
+ return cast<class_<T>>(type);
1294
+ }
1295
+
1288
1296
private:
1289
1297
const char *name;
1290
1298
handle scope;
1291
1299
dict entries;
1292
1300
object ctor;
1293
1301
object unique;
1294
1302
dict kwargs;
1303
+ object type;
1304
+
1305
+ void add_entry (const char *name, T value) {
1306
+ entries[name] = cast (static_cast <underlying_type>(value));
1307
+ update ();
1308
+ }
1295
1309
1296
1310
void update () {
1297
- object type = unique (ctor (**kwargs));
1311
+ type = unique (ctor (**kwargs));
1298
1312
setattr (scope, name, type);
1299
1313
detail::type_caster<T>::bind (type, entries);
1300
1314
}
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 @@ -152,3 +152,7 @@ def test_py3_enum():
152
152
with pytest .raises (ValueError ) as excinfo :
153
153
non_unique_py3_enum ()
154
154
assert 'duplicate values found' in str (excinfo .value )
155
+
156
+ assert Py3Enum .ultimate_answer == 42
157
+ assert not Py3Enum .A .is_b and Py3Enum .B .is_b and not Py3Enum .C .is_b
158
+ assert Py3Enum .A .add (10 ) == - 32 and Py3Enum .C .add (- 1 ) == 41
You can’t perform that action at this time.
0 commit comments