Skip to content

Commit e7a213a

Browse files
committed
py: Add enum helper code
This makes it much easier to implement enums, and the printing code is shared. We might want to convert other enums to this in the future.
1 parent 183649a commit e7a213a

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

py/enum.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* SPDX-FileCopyrightText: Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/enum.h"
28+
#include "py/runtime.h"
29+
30+
mp_obj_t cp_enum_find(const mp_obj_type_t *type, int value) {
31+
const mp_obj_dict_t *dict = type->locals_dict;
32+
for (size_t i=0; i<dict->map.used; i++) {
33+
const cp_enum_obj_t *v = dict->map.table[i].value;
34+
if (v->value == value) {
35+
return (mp_obj_t)v;
36+
}
37+
}
38+
return mp_const_none;
39+
}
40+
41+
int cp_enum_value(const mp_obj_type_t *type, mp_obj_t *obj) {
42+
if (!MP_OBJ_IS_TYPE(obj, type)) {
43+
mp_raise_TypeError_varg(translate("Expected a %q"), type->name);
44+
}
45+
return ((cp_enum_obj_t*)MP_OBJ_TO_PTR(obj))->value;
46+
}
47+
48+
void cp_enum_obj_print_helper(uint16_t module, const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
49+
(void) kind;
50+
cp_enum_obj_t *self = self_in;
51+
mp_printf(print, "%q.%q.%q", module, self->base.type->name, self->name);
52+
}

py/enum.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* SPDX-FileCopyrightText: Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once
28+
29+
#include "py/obj.h"
30+
31+
typedef struct {
32+
mp_obj_base_t base;
33+
int16_t value;
34+
int16_t name;
35+
} cp_enum_obj_t;
36+
37+
#define MAKE_ENUM_VALUE(type, prefix, name, value) \
38+
STATIC const cp_enum_obj_t prefix ## _ ## name ## _obj = { \
39+
{ &type }, value, MP_QSTR_ ## name, \
40+
}
41+
42+
#define MAKE_ENUM_MAP(name) \
43+
STATIC const mp_rom_map_elem_t name ## _locals_table[] =
44+
45+
#define MAKE_ENUM_MAP_ENTRY(prefix, name) \
46+
{ MP_ROM_QSTR(MP_QSTR_ ## name), MP_ROM_PTR(&prefix ## _ ## name ## _obj) }
47+
48+
#define MAKE_PRINTER(module, typename) \
49+
STATIC void typename ## _ ## print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { \
50+
cp_enum_obj_print_helper(MP_QSTR_ ## module, print, self_in, kind); \
51+
}
52+
53+
#define MAKE_ENUM_TYPE(module, type, typename) \
54+
const mp_obj_type_t typename ## _type = { \
55+
{ &mp_type_type }, \
56+
.name = MP_QSTR_ ## type, \
57+
.print = typename ## _print, \
58+
.locals_dict = (mp_obj_t)&typename ## _locals_dict, \
59+
}
60+
61+
62+
63+
mp_obj_t cp_enum_find(const mp_obj_type_t *type, int value);
64+
int cp_enum_value(const mp_obj_type_t *type, mp_obj_t *obj);
65+
void cp_enum_obj_print_helper(uint16_t module, const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
181181
argcheck.o \
182182
warning.o \
183183
map.o \
184+
enum.o \
184185
obj.o \
185186
objarray.o \
186187
objattrtuple.o \

0 commit comments

Comments
 (0)