From adcb15514fbc943d25321bbe57f92b4039d290b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Wed, 11 Nov 2015 01:55:37 +0000 Subject: [PATCH] [INTERPRETER] * NEW: Collection.Default is a new property that allows to define the default value returned by a collection when a key has no value. Note that assigning the default value does not clear the key. You have to explicitly assign NULL for that. git-svn-id: svn://localhost/gambas/trunk@7468 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- main/gbx/gbx_c_collection.c | 52 ++++++++++++++++++++++++++++------ main/gbx/gbx_c_collection.h | 56 +++++++++++++++++++------------------ 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/main/gbx/gbx_c_collection.c b/main/gbx/gbx_c_collection.c index 88a674b3a..e3a76ecfb 100644 --- a/main/gbx/gbx_c_collection.c +++ b/main/gbx/gbx_c_collection.c @@ -110,6 +110,7 @@ BEGIN_METHOD(Collection_new, GB_INTEGER mode) HASH_TABLE_create(&THIS->hash_table, TYPE_sizeof(T_VARIANT), mode); THIS->last = NULL; + THIS->default_value.type = GB_T_NULL; END_METHOD @@ -117,6 +118,7 @@ END_METHOD BEGIN_METHOD_VOID(Collection_free) clear(THIS); + GB_StoreVariant(NULL, POINTER(&THIS->default_value)); END_METHOD @@ -204,7 +206,14 @@ END_METHOD BEGIN_METHOD(Collection_get, GB_STRING key) - GB_ReturnVariant(get_key(THIS, STRING(key), LENGTH(key), !DEBUG_inside_eval)); + void *value = get_key(THIS, STRING(key), LENGTH(key), !DEBUG_inside_eval); + if (!value) + { + if (THIS->has_default) + value = (void *)&THIS->default_value; + } + + GB_ReturnVariant(value); END_METHOD @@ -250,6 +259,20 @@ BEGIN_METHOD_VOID(Collection_Copy) END_METHOD + +BEGIN_PROPERTY(Collection_Default) + + if (READ_PROPERTY) + GB_ReturnVariant(&THIS->default_value); + else + { + GB_StoreVariant(PROP(GB_VARIANT), POINTER(&THIS->default_value)); + THIS->has_default = THIS->default_value.type != GB_T_NULL; + } + +END_PROPERTY + + #endif @@ -263,6 +286,7 @@ GB_DESC NATIVE_Collection[] = GB_PROPERTY_READ("Count", "i", Collection_Count), GB_PROPERTY_READ("Length", "i", Collection_Count), GB_PROPERTY("Key", "s", Collection_Key), + GB_PROPERTY("Default", "v", Collection_Default), GB_METHOD("Add", NULL, Collection_Add, "(Value)v(Key)s"), GB_METHOD("Exist", "b", Collection_Exist, "(Key)s"), @@ -312,21 +336,33 @@ bool GB_CollectionSet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *v bool GB_CollectionGet(GB_COLLECTION col, const char *key, int len, GB_VARIANT *value) { + CCOLLECTION *_object = (CCOLLECTION *)col; VARIANT *var; + bool ret; - var = (VARIANT *)get_key((CCOLLECTION *)col, key, len, !DEBUG_inside_eval); value->type = T_VARIANT; - if (var) + + var = (VARIANT *)get_key(THIS, key, len, !DEBUG_inside_eval); + + if (!var) { - value->value.type = var->type; - value->value.value.data = var->value.data; - return FALSE; + if (!THIS->has_default) + { + value->value.type = GB_T_NULL; + return TRUE; + } + + var = (VARIANT *)&THIS->default_value; + ret = TRUE; } else { - value->value.type = GB_T_NULL; - return TRUE; + ret = FALSE; } + + value->value.type = var->type; + value->value.value.data = var->value.data; + return ret; } bool GB_CollectionEnum(GB_COLLECTION col, GB_COLLECTION_ITER *iter, GB_VARIANT *value, char **key, int *len) diff --git a/main/gbx/gbx_c_collection.h b/main/gbx/gbx_c_collection.h index ad401ade6..025184271 100644 --- a/main/gbx/gbx_c_collection.h +++ b/main/gbx/gbx_c_collection.h @@ -1,23 +1,23 @@ /*************************************************************************** - gbx_c_collection.h + gbx_c_collection.h - (c) 2000-2013 Benoît Minisini + (c) 2000-2013 Benoît Minisini - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - MA 02110-1301, USA. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. ***************************************************************************/ @@ -30,21 +30,23 @@ #include "gbx_object.h" typedef - struct { - int sort; - void *data; - } - CCOL_DESC; + struct { + int sort; + void *data; + } + CCOL_DESC; typedef - struct { - OBJECT object; - HASH_TABLE *hash_table; - HASH_NODE *last; - short mode; - char locked; - } - CCOLLECTION; + struct { + OBJECT object; + HASH_TABLE *hash_table; + HASH_NODE *last; + GB_VARIANT_VALUE default_value; + short mode; + unsigned locked : 1; + unsigned has_default : 1; + } + CCOLLECTION; #ifndef __GBX_C_COLLECTION_C