1 /* Common operations. 2 3 Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #ifndef __OPS_H__ 20 #define __OPS_H__ 21 22 #include "types.h" 23 #include <string.h> /* for __COPY */ 24 25 /* Direct access and manipulation of static objects. */ 26 27 __attr __load_static_ignore(__ref obj); 28 __attr __load_static_replace(__ref context, __ref obj); 29 __attr __load_static_test(__ref context, __ref obj); 30 31 /* Direct retrieval operations, returning attributes. */ 32 33 __attr __load_via_class__(__ref obj, int pos); 34 __attr __load_via_object__(__ref obj, int pos); 35 __attr __get_class_and_load__(__ref obj, int pos); 36 37 #define __load_via_class(OBJ, ATTRNAME) (__load_via_class__(OBJ, __ATTRPOS(ATTRNAME))) 38 #define __load_via_object(OBJ, ATTRNAME) (__load_via_object__(OBJ, __ATTRPOS(ATTRNAME))) 39 #define __get_class_and_load(OBJ, ATTRNAME) (__get_class_and_load__(OBJ, __ATTRPOS(ATTRNAME))) 40 41 /* Direct storage operations. */ 42 43 int __store_via_object__(__ref obj, int pos, __attr value); 44 int __get_class_and_store__(__ref obj, int pos, __attr value); 45 46 #define __store_via_object(OBJ, ATTRNAME, VALUE) (__store_via_object__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 47 #define __get_class_and_store(OBJ, ATTRNAME, VALUE) (__get_class_and_store__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 48 49 /* Introspection. */ 50 51 int __is_instance(__ref obj); 52 int __is_subclass(__ref obj, __attr cls); 53 int __is_instance_subclass(__ref obj, __attr cls); 54 int __is_type_instance(__ref obj); 55 __ref __get_class(__ref obj); 56 __attr __get_class_attr(__ref obj); 57 58 /* Attribute testing operations. */ 59 60 __ref __test_specific_instance(__ref obj, __ref type); 61 __ref __test_specific_object(__ref obj, __ref type); 62 __ref __test_specific_type(__ref obj, __ref type); 63 64 __ref __test_common_instance__(__ref obj, int pos, int code); 65 __ref __test_common_object__(__ref obj, int pos, int code); 66 __ref __test_common_type__(__ref obj, int pos, int code); 67 68 #define __test_common_instance(OBJ, TYPENAME) (__test_common_instance__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 69 #define __test_common_object(OBJ, TYPENAME) (__test_common_object__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 70 #define __test_common_type(OBJ, TYPENAME) (__test_common_type__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 71 72 /* Attribute testing and retrieval operations. */ 73 74 __attr __check_and_load_via_object_null(__ref obj, int pos, int code); 75 76 __attr __check_and_load_via_class__(__ref obj, int pos, int code); 77 __attr __check_and_load_via_object__(__ref obj, int pos, int code); 78 __attr __check_and_load_via_any__(__ref obj, int pos, int code); 79 80 #define __check_and_load_via_class(OBJ, ATTRNAME) (__check_and_load_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 81 #define __check_and_load_via_object(OBJ, ATTRNAME) (__check_and_load_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 82 #define __check_and_load_via_any(OBJ, ATTRNAME) (__check_and_load_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 83 84 /* Attribute testing and storage operations. */ 85 86 int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value); 87 int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value); 88 int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value); 89 90 #define __check_and_store_via_class(OBJ, ATTRNAME, VALUE) (__check_and_store_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 91 #define __check_and_store_via_object(OBJ, ATTRNAME, VALUE) (__check_and_store_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 92 #define __check_and_store_via_any(OBJ, ATTRNAME, VALUE) (__check_and_store_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 93 94 /* Context-related operations. */ 95 96 int __test_context_update(__ref context, __attr attr); 97 __attr __test_context(__ref context, __attr attr); 98 __attr __update_context(__ref context, __attr attr); 99 __attr __test_context_revert(int target, __ref context, __attr attr, __ref contexts[]); 100 __attr __test_context_static(int target, __ref context, __ref value, __ref contexts[]); 101 102 #define __get_context(__TARGET) (__tmp_contexts[__TARGET]) 103 #define __set_context(__TARGET, __ATTR) (__tmp_contexts[__TARGET] = (__ATTR).value) 104 #define __set_private_context(__ATTR) (__tmp_private_context = (__ATTR).value) 105 #define __set_accessor(__ATTR) (__tmp_value = (__ATTR).value) 106 #define __set_target_accessor(__ATTR) (__tmp_target_value = (__ATTR).value) 107 108 /* Context testing for invocations. */ 109 110 __attr __unwrap_callable(__attr callable); 111 __attr (*__get_function(__ref context, __attr target))(__attr[]); 112 __attr (*__check_and_get_function(__ref context, __attr target))(__attr[]); 113 114 /* Parameter position operations. */ 115 116 int __HASPARAM(const __ptable *ptable, int ppos, int pcode); 117 118 /* Conversions. */ 119 120 __attr __CONTEXT_AS_VALUE(__attr attr); 121 122 /* Type testing. */ 123 124 __ref __ISFUNC(__ref obj); 125 int __ISNULL(__attr value); 126 127 /* Attribute codes and positions for type objects. */ 128 129 unsigned int __TYPECODE(__ref obj); 130 unsigned int __TYPEPOS(__ref obj); 131 132 /* Memory allocation. */ 133 134 void *__ALLOCATE(size_t nmemb, size_t size); 135 void *__REALLOCATE(void *ptr, size_t size); 136 137 /* Copying of structures. */ 138 139 __ref __COPY(__ref obj, int size); 140 141 #endif /* __OPS_H__ */