[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
This commit is contained in:
Benoît Minisini 2015-11-11 01:55:37 +00:00
parent e6bc6243e1
commit adcb15514f
2 changed files with 73 additions and 35 deletions

View file

@ -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)

View file

@ -1,23 +1,23 @@
/***************************************************************************
gbx_c_collection.h
gbx_c_collection.h
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
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