Annotation of parser3/src/types/pa_varray.h, revision 1.1
1.1 ! moko 1: /** @file
! 2: Parser: @b array parser type decl.
! 3:
! 4: Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
! 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
! 6: */
! 7:
! 8: #ifndef PA_VARRAY_H
! 9: #define PA_VARRAY_H
! 10:
! 11: #define IDENT_PA_VARRAY_H "$Id: pa_varray.h,v 1.84 2023/10/02 02:58:01 moko Exp $"
! 12:
! 13: #include "classes.h"
! 14: #include "pa_value.h"
! 15: #include "pa_array.h"
! 16: #include "pa_vhash.h"
! 17: #include "pa_vint.h"
! 18: #include "pa_globals.h"
! 19: #include "pa_symbols.h"
! 20:
! 21: // defines
! 22:
! 23: #define VARRAY_TYPE "sparse-array"
! 24:
! 25: extern Methoded* array_class;
! 26:
! 27: class VArray: public VHashBase {
! 28: public: // value
! 29:
! 30: override const char* type() const { return VARRAY_TYPE; }
! 31: override VStateless_class *get_class() { return array_class; }
! 32:
! 33: /// VArray: defined elements count
! 34: override int as_int() const { return count(); }
! 35: override double as_double() const { return count(); }
! 36: override bool is_defined() const { return count()!=0; }
! 37: override bool as_bool() const { return count()!=0; }
! 38: override Value& as_expr_result() { return *new VInt(count()); }
! 39:
! 40: /// VArray: virtual hash
! 41: override HashStringValue *get_hash() { return &hash(); }
! 42: override HashStringValue* get_fields() { return &hash(); }
! 43: override HashStringValue* get_fields_reference() { return &hash(); }
! 44:
! 45: /// VArray: json-string
! 46: override const String* get_json_string(Json_options& options);
! 47:
! 48: /// VArray: (key)=value
! 49: override Value* get_element(const String& aname) {
! 50: // $element first
! 51: if(Value* result=get(index(aname)))
! 52: return result;
! 53:
! 54: // $fields -- pseudo field to make 'hash' and 'array' more like 'table'
! 55: if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
! 56: return this;
! 57:
! 58: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
! 59: // $method, CLASS, CLASS_NAME
! 60: if(Value* result=VStateless_object::get_element(aname))
! 61: return result;
! 62: #endif
! 63: return NULL;
! 64: }
! 65:
! 66: #ifdef FEATURE_GET_ELEMENT4CALL
! 67: override Value* get_element4call(const String& aname) {
! 68: // $method first
! 69: if(Value* result=VStateless_object::get_element(aname))
! 70: return result;
! 71:
! 72: // $element
! 73: if(Value* result=get(index(aname)))
! 74: return result;
! 75:
! 76: return bark("%s method not found", &aname);
! 77: }
! 78: #endif
! 79:
! 80: /// VArray: (key)=value
! 81: override const VJunction* put_element(const String& aname, Value* avalue) {
! 82: farray.fit(index(aname), avalue);
! 83: clear_hash();
! 84: return 0;
! 85: }
! 86:
! 87: public: // VHashBase
! 88:
! 89: override HashStringValue& hash();
! 90: override void set_default(Value*) { }
! 91: override Value* get_default() { return 0; }
! 92: override void add(Value* avalue) { farray+=avalue; /* only json uses it, thus no need to clear_hash()*/ }
! 93:
! 94: public: // usage
! 95:
! 96: VArray(size_t initial=0): farray(initial), fhash(0), fcount(0) {}
! 97: ~VArray() { clear_hash(); }
! 98:
! 99: ArrayValue &array() { return farray; }
! 100: size_t count() const;
! 101:
! 102: Value *get(size_t aindex){
! 103: if(aindex<farray.count())
! 104: return farray.get(aindex);
! 105: return NULL;
! 106: }
! 107:
! 108: static size_t index(int aindex){
! 109: if(aindex<0)
! 110: throw Exception("number.format", 0, "out of range (negative)");
! 111: return aindex;
! 112: }
! 113:
! 114: static size_t index(const String& aindex){
! 115: int result=aindex.as_int();
! 116: if(result<0)
! 117: throw Exception("number.format", &aindex, "out of range (negative)");
! 118: return result;
! 119: }
! 120:
! 121: static size_t index(const Value& aindex){ return index(aindex.as_int()); }
! 122:
! 123: void clear(size_t index){
! 124: if(index < farray.count()){
! 125: farray.put(index, NULL);
! 126: clear_hash();
! 127: }
! 128: }
! 129:
! 130: bool contains(size_t index){
! 131: return index < farray.count() && farray.get(index) != NULL;
! 132: }
! 133:
! 134: void clear(){
! 135: farray.clear();
! 136: clear_hash();
! 137: }
! 138:
! 139: void clear_hash() {
! 140: #ifdef USE_DESTRUCTORS
! 141: if(fhash)
! 142: delete(fhash);
! 143: #endif
! 144: fhash=0;
! 145: fcount=0;
! 146: }
! 147:
! 148: private:
! 149:
! 150: ArrayValue farray;
! 151: HashStringValue *fhash;
! 152: mutable size_t fcount;
! 153:
! 154: };
! 155:
! 156: #endif
E-mail: