| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**
- * @file NCDObject.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stddef.h>
- #include <ncd/static_strings.h>
- #include "NCDObject.h"
- NCDObject NCDObject_Build (NCD_string_id_t type, void *user, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj)
- {
- NCDObject obj;
- obj.type = type;
- obj.method_user = user;
- obj.user = user;
- obj.user2 = NULL;
- obj.uv.func_getvar = func_getvar;
- obj.uo.func_getobj = func_getobj;
-
- return obj;
- }
- NCDObject NCDObject_Build2 (NCD_string_id_t type, void *user, void *user2, NCDObject_func_getvar2 func_getvar2, NCDObject_func_getobj2 func_getobj2)
- {
- ASSERT(user2)
-
- NCDObject obj;
- obj.type = type;
- obj.method_user = user;
- obj.user = user;
- obj.user2 = user2;
- obj.uv.func_getvar2 = func_getvar2;
- obj.uo.func_getobj2 = func_getobj2;
-
- return obj;
- }
- NCDObject NCDObject_BuildMethodUser (NCD_string_id_t type, void *method_user, void *user, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj)
- {
- NCDObject obj;
- obj.type = type;
- obj.method_user = method_user;
- obj.user = user;
- obj.user2 = NULL;
- obj.uv.func_getvar = func_getvar;
- obj.uo.func_getobj = func_getobj;
-
- return obj;
- }
- NCDObject NCDObject_BuildMethodUser2 (NCD_string_id_t type, void *method_user, void *user, void *user2, NCDObject_func_getvar2 func_getvar2, NCDObject_func_getobj2 func_getobj2)
- {
- ASSERT(user2)
-
- NCDObject obj;
- obj.type = type;
- obj.method_user = method_user;
- obj.user = user;
- obj.user2 = user2;
- obj.uv.func_getvar2 = func_getvar2;
- obj.uo.func_getobj2 = func_getobj2;
-
- return obj;
- }
- NCD_string_id_t NCDObject_Type (NCDObject *o)
- {
- return o->type;
- }
- void * NCDObject_MethodUser (NCDObject *o)
- {
- ASSERT(o->type >= 0)
-
- return o->method_user;
- }
- int NCDObject_GetObj (NCDObject *o, NCD_string_id_t name, NCDObject *out_object)
- {
- ASSERT(out_object)
-
- int res;
- if (o->user2) {
- res = (!o->uo.func_getobj2 ? 0 : o->uo.func_getobj2(o->user, o->user2, name, out_object));
- } else {
- res = (!o->uo.func_getobj ? 0 : o->uo.func_getobj(o->user, name, out_object));
- }
-
- ASSERT(res == 0 || res == 1)
-
- return res;
- }
- int NCDObject_GetVar (NCDObject *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
- {
- ASSERT(mem)
- ASSERT(out_value)
-
- int res;
- if (o->user2) {
- res = (!o->uv.func_getvar2 ? 0 : o->uv.func_getvar2(o->user, o->user2, name, mem, out_value));
- } else {
- res = (!o->uv.func_getvar ? 0 : o->uv.func_getvar(o->user, name, mem, out_value));
- }
-
- ASSERT(res == 0 || res == 1)
- ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
-
- return res;
- }
- static NCDObject dig_into_object (NCDObject object)
- {
- NCDObject obj2;
- while (NCDObject_GetObj(&object, NCD_STRING_EMPTY, &obj2)) {
- object = obj2;
- }
-
- return object;
- }
- int NCDObject_ResolveObjExprCompact (NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
- {
- ASSERT(num_names == 0 || names)
- ASSERT(num_names >= 0)
- ASSERT(out_object)
-
- NCDObject object = dig_into_object(*o);
-
- while (num_names > 0) {
- NCDObject obj2;
- if (!NCDObject_GetObj(&object, *names, &obj2)) {
- return 0;
- }
-
- object = dig_into_object(obj2);
-
- names++;
- num_names--;
- }
-
- *out_object = object;
- return 1;
- }
- int NCDObject_ResolveVarExprCompact (NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
- {
- ASSERT(num_names == 0 || names)
- ASSERT(num_names >= 0)
- ASSERT(mem)
- ASSERT(out_value)
-
- NCDObject object = dig_into_object(*o);
-
- while (num_names > 0) {
- NCDObject obj2;
- if (!NCDObject_GetObj(&object, *names, &obj2)) {
- if (num_names == 1 && NCDObject_GetVar(&object, *names, mem, out_value)) {
- return 1;
- }
-
- return 0;
- }
-
- object = dig_into_object(obj2);
-
- names++;
- num_names--;
- }
-
- return NCDObject_GetVar(&object, NCD_STRING_EMPTY, mem, out_value);
- }
|