Ignore:
Timestamp:
01/31/19 03:47:07 (6 years ago)
Author:
Maciej Komosinski
Message:

Added Dictionary.hasKey(). Accessing non-existent dictionary keys becomes an error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/vm/classes/collectionobj.cpp

    r746 r849  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    5050ParamEntry dictionary_paramtab[] =
    5151{
    52         { "Dictionary", 1, 11, "Dictionary", "Dictionary associates stored values with string keys "
     52        { "Dictionary", 1, 12, "Dictionary", "Dictionary associates stored values with string keys "
    5353        "(\"key\" is the first argument in get/set/remove functions). Integer key can be "
    5454        "used to enumerate all elements (note that while iterating, the elements are returned in no particular order).\n"
     
    7070        { "size", 0, PARAM_NOSTATIC | PARAM_READONLY, "Element count", "d", GETONLY(size), },
    7171        { "remove", 0, PARAM_NOSTATIC, "Remove", "p(x key)", PROCEDURE(p_remove), "Removes the named or indexed element (depending on the argument type: string or int)." },
    72         { "get", 0, PARAM_NOSTATIC, "Get element", "p x(x key)", PROCEDURE(p_get), "Retrieves the named or indexed element (depending on the argument type: string or int). null is returned for nonexistent keys.\nobject.get(key) can be shortened to 'object[key]'" },
     72        { "get", 0, PARAM_NOSTATIC, "Get element", "p x(x key)", PROCEDURE(p_get), "Retrieves the named or indexed element (depending on the argument type: string or int). Accessing nonexistent keys is an error (use hasKey() if necessary).\nobject.get(key) can be shortened to 'object[key]'" },
    7373        { "getKey", 0, PARAM_NOSTATIC, "Get a key", "p s(d index)", PROCEDURE(p_getKey), "Returns the key of the indexed element (0 <= index < size)" },
     74        { "hasKey", 0, PARAM_NOSTATIC, "Check if key exists", "p d(s key)", PROCEDURE(p_hasKey), "Returns 1 (interpreted as true) if dictionary contains the supplied key, or 0 (false) otherwise.\nExample:\n   if (obj.hasKey(\"a\"))\n      x = obj->a;" },
    7475        { "set", 0, PARAM_NOSTATIC, "Set element", "p x(x key,x value)", PROCEDURE(p_set), "Set element value for the specified key or index (depending on the argument type: string or int).\n"
    7576        "Returns the value previously associated with the given key (or index).\n"
     
    106107                for (int i = data.size() - 1; i >= 0; i--)
    107108                {
    108                 ExtValue *v = (ExtValue*)data.get(i);
    109                 if (v) delete v;
     109                        ExtValue *v = (ExtValue*)data.get(i);
     110                        if (v) delete v;
    110111                }
    111112        data.clear();
     
    221222        {
    222223                double d = a - ((ExtValue*)data.get(i))->getDouble();
    223                 s += d*d;
     224                s += d * d;
    224225        }
    225226        ret->setDouble(sqrt(s / max(1, data.size() - 1)));
     
    391392ExtValue DictionaryObject::get(SString key)
    392393{
    393         ExtValue *val = (ExtValue*)hash.get(key);
    394         if (val)
    395                 return *val;
    396         return ExtValue::empty();
     394        int found = 0;
     395        ExtValue *val = (ExtValue*)hash.get(key, &found);
     396        if (found == 0)
     397        {
     398                logPrintf("Dictionary", "get", LOG_ERROR, "Key '%s' not found", key.c_str());
     399                return ExtValue::invalid();
     400        }
     401        else
     402        {
     403                if (val)
     404                        return *val;
     405                return ExtValue::empty();
     406        }
    397407}
    398408
     
    422432        }
    423433        *ret = ExtValue();
     434}
     435
     436void DictionaryObject::p_hasKey(PARAMPROCARGS)
     437{
     438        int found = 0;
     439        hash.get(args->getString(), &found);
     440        ret->setInt(found);
    424441}
    425442
Note: See TracChangeset for help on using the changeset viewer.