blob: 323696cf159d204a464a76f732db059da6c55683 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
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
Damien George777b0f32014-04-13 18:59:45 +010027#include <stdlib.h>
28#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/runtime.h"
Damien George777b0f32014-04-13 18:59:45 +010032
Damien Georgefb510b32014-06-01 13:32:54 +010033#if MICROPY_PY_BUILTINS_PROPERTY
Damien George777b0f32014-04-13 18:59:45 +010034
35typedef struct _mp_obj_property_t {
36 mp_obj_base_t base;
37 mp_obj_t proxy[3]; // getter, setter, deleter
38} mp_obj_property_t;
39
Damien Georgea0c97812016-01-03 09:59:18 +000040STATIC mp_obj_t property_make_new(mp_obj_t type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeee7a8802014-05-11 18:37:21 +010041 mp_arg_check_num(n_args, n_kw, 0, 4, false);
Damien George777b0f32014-04-13 18:59:45 +010042
43 mp_obj_property_t *o = m_new_obj(mp_obj_property_t);
Damien George999cedb2015-11-27 17:01:44 +000044 o->base.type = MP_OBJ_TO_PTR(type_in);
Damien George777b0f32014-04-13 18:59:45 +010045 if (n_args >= 4) {
46 // doc ignored
47 }
48 if (n_args >= 3) {
49 o->proxy[2] = args[2];
50 } else {
51 o->proxy[2] = mp_const_none;
52 }
53 if (n_args >= 2) {
54 o->proxy[1] = args[1];
55 } else {
56 o->proxy[1] = mp_const_none;
57 }
58 if (n_args >= 1) {
59 o->proxy[0] = args[0];
60 } else {
61 o->proxy[0] = mp_const_none;
62 }
Damien George999cedb2015-11-27 17:01:44 +000063 return MP_OBJ_FROM_PTR(o);
Damien George777b0f32014-04-13 18:59:45 +010064}
65
66STATIC mp_obj_t property_getter(mp_obj_t self_in, mp_obj_t getter) {
67 mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
Damien George999cedb2015-11-27 17:01:44 +000068 *p2 = *(mp_obj_property_t*)MP_OBJ_TO_PTR(self_in);
Damien George777b0f32014-04-13 18:59:45 +010069 p2->proxy[0] = getter;
Damien George999cedb2015-11-27 17:01:44 +000070 return MP_OBJ_FROM_PTR(p2);
Damien George777b0f32014-04-13 18:59:45 +010071}
72
73STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_getter_obj, property_getter);
74
75STATIC mp_obj_t property_setter(mp_obj_t self_in, mp_obj_t setter) {
76 mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
Damien George999cedb2015-11-27 17:01:44 +000077 *p2 = *(mp_obj_property_t*)MP_OBJ_TO_PTR(self_in);
Damien George777b0f32014-04-13 18:59:45 +010078 p2->proxy[1] = setter;
Damien George999cedb2015-11-27 17:01:44 +000079 return MP_OBJ_FROM_PTR(p2);
Damien George777b0f32014-04-13 18:59:45 +010080}
81
82STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_setter_obj, property_setter);
83
84STATIC mp_obj_t property_deleter(mp_obj_t self_in, mp_obj_t deleter) {
85 mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
Damien George999cedb2015-11-27 17:01:44 +000086 *p2 = *(mp_obj_property_t*)MP_OBJ_TO_PTR(self_in);
Damien George777b0f32014-04-13 18:59:45 +010087 p2->proxy[2] = deleter;
Damien George999cedb2015-11-27 17:01:44 +000088 return MP_OBJ_FROM_PTR(p2);
Damien George777b0f32014-04-13 18:59:45 +010089}
90
91STATIC MP_DEFINE_CONST_FUN_OBJ_2(property_deleter_obj, property_deleter);
92
Damien Georgecbf76742015-11-27 13:38:15 +000093STATIC const mp_rom_map_elem_t property_locals_dict_table[] = {
94 { MP_ROM_QSTR(MP_QSTR_getter), MP_ROM_PTR(&property_getter_obj) },
95 { MP_ROM_QSTR(MP_QSTR_setter), MP_ROM_PTR(&property_setter_obj) },
96 { MP_ROM_QSTR(MP_QSTR_deleter), MP_ROM_PTR(&property_deleter_obj) },
Damien George777b0f32014-04-13 18:59:45 +010097};
98
99STATIC MP_DEFINE_CONST_DICT(property_locals_dict, property_locals_dict_table);
100
101const mp_obj_type_t mp_type_property = {
102 { &mp_type_type },
103 .name = MP_QSTR_property,
104 .make_new = property_make_new,
Damien George999cedb2015-11-27 17:01:44 +0000105 .locals_dict = (mp_obj_dict_t*)&property_locals_dict,
Damien George777b0f32014-04-13 18:59:45 +0100106};
107
108const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) {
109 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_property));
Damien George999cedb2015-11-27 17:01:44 +0000110 mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
Damien George777b0f32014-04-13 18:59:45 +0100111 return self->proxy;
112}
113
Damien Georgefb510b32014-06-01 13:32:54 +0100114#endif // MICROPY_PY_BUILTINS_PROPERTY