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 @@ -1263,22 +1263,36 @@ class py3_enum {
1263
1263
update ();
1264
1264
}
1265
1265
1266
- py3_enum& value (const char * name, T value) {
1267
- entries[name] = cast (static_cast <underlying_type>(value));
1268
- update ();
1266
+ py3_enum& value (const char * name, T value) & {
1267
+ add_entry (name, value);
1269
1268
return *this ;
1270
1269
}
1271
1270
1271
+ py3_enum&& value(const char * name, T value) && {
1272
+ add_entry (name, value);
1273
+ return std::move (*this );
1274
+ }
1275
+
1276
+ class_<T> extend () && {
1277
+ return cast<class_<T>>(type);
1278
+ }
1279
+
1272
1280
private:
1273
1281
const char *name;
1274
1282
handle scope;
1275
1283
dict entries;
1276
1284
object ctor;
1277
1285
object unique;
1278
1286
dict kwargs;
1287
+ object type;
1288
+
1289
+ void add_entry (const char *name, T value) {
1290
+ entries[name] = cast (static_cast <underlying_type>(value));
1291
+ update ();
1292
+ }
1279
1293
1280
1294
void update () {
1281
- object type = unique (ctor (**kwargs));
1295
+ type = unique (ctor (**kwargs));
1282
1296
setattr (scope, name, type);
1283
1297
detail::type_caster<T>::bind (type, entries);
1284
1298
}
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