Annotation of parser3/src/types/pa_vhash.h, revision 1.91
1.10 paf 1: /** @file
2: Parser: @b hash parser type decl.
3:
1.89 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.83 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 paf 6: */
7:
8: #ifndef PA_VHASH_H
9: #define PA_VHASH_H
1.34 paf 10:
1.91 ! moko 11: #define IDENT_PA_VHASH_H "$Id: pa_vhash.h,v 1.90 2026/09/24 02:23:26 moko Exp $"
1.1 paf 12:
1.13 paf 13: #include "classes.h"
1.1 paf 14: #include "pa_value.h"
15: #include "pa_hash.h"
1.21 parser 16: #include "pa_vint.h"
1.46 paf 17: #include "pa_globals.h"
1.74 moko 18: #include "pa_symbols.h"
1.1 paf 19:
1.39 paf 20: // defines
21:
22: #define VHASH_TYPE "hash"
1.63 moko 23:
1.46 paf 24: extern Methoded* hash_class;
1.13 paf 25:
1.76 moko 26: class VHashBase: public VStateless_object {
1.1 paf 27: public: // value
28:
1.46 paf 29: override const char* type() const { return VHASH_TYPE; }
30: override VStateless_class *get_class() { return hash_class; }
1.17 parser 31:
1.76 moko 32: public: // usage
33:
34: virtual HashStringValue& hash()=0;
35: virtual void set_default(Value* adefault)=0;
36: virtual Value* get_default()=0;
1.85 moko 37: virtual void add(Value* avalue)=0;
1.76 moko 38: };
39:
40:
41: /// value of type 'hash', implemented with Hash
42: class VHash: public VHashBase {
43: public: // value
44:
45: /// VHash: count
1.46 paf 46: override int as_int() const { return fhash.count(); }
1.68 misha 47: override double as_double() const { return fhash.count(); }
1.80 moko 48: override bool is_defined() const { return fhash.count()!=0 || _default!=NULL; }
1.68 misha 49: override bool as_bool() const { return fhash.count()!=0; }
1.76 moko 50: override Value& as_expr_result() { return *new VInt(fhash.count()); }
1.1 paf 51:
1.11 paf 52: /// VHash: fhash
1.76 moko 53: override HashStringValue *get_hash() { return &fhash; }
1.66 misha 54: override HashStringValue* get_fields() { return &fhash; }
1.76 moko 55: override HashStringValue* get_fields_reference() { return &fhash; }
1.66 misha 56:
1.11 paf 57: /// VHash: (key)=value
1.76 moko 58: override Value* get_element(const String& aname) {
1.70 moko 59: // $element first
1.46 paf 60: if(Value* result=fhash.get(aname))
1.13 paf 61: return result;
1.42 paf 62:
63: // $fields -- pseudo field to make 'hash' more like 'table'
1.74 moko 64: if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
1.42 paf 65: return this;
1.13 paf 66:
1.91 ! moko 67: MAYBE_GET_SPECIAL_OR_METHOD(VStateless_object::get_element(aname));
1.16 parser 68:
69: // default value
1.22 parser 70: return get_default();
1.1 paf 71: }
1.84 moko 72:
1.70 moko 73: #ifdef FEATURE_GET_ELEMENT4CALL
74: override Value* get_element4call(const String& aname) {
75: // $method first
76: if(Value* result=VStateless_object::get_element(aname))
77: return result;
78:
79: // $element
80: if(Value* result=fhash.get(aname))
81: return result;
82:
83: // default value
1.84 moko 84: if(Value* result=get_default())
85: return result;
86:
87: return bark("%s method not found", &aname);
1.70 moko 88: }
89: #endif
1.84 moko 90:
1.11 paf 91: /// VHash: (key)=value
1.69 moko 92: override const VJunction* put_element(const String& aname, Value* avalue) {
1.74 moko 93: if(SYMBOLS_EQ(aname,_DEFAULT_SYMBOL))
1.47 paf 94: set_default(avalue);
95: else
1.75 moko 96: fhash.put(aname, avalue);
1.36 paf 97:
1.81 moko 98: return 0;
1.1 paf 99: }
1.52 paf 100:
1.61 misha 101: override VFile* as_vfile(String::Language lang, const Request_charsets *charsets=0);
1.1 paf 102:
103: public: // usage
104:
1.75 moko 105: VHash(): _default(0) {}
1.18 parser 106:
1.75 moko 107: VHash(const HashStringValue& source): fhash(source), _default(0) {}
1.7 paf 108:
1.76 moko 109: override HashStringValue& hash() { return fhash; }
110: override void set_default(Value* adefault) { _default=adefault; }
111: override Value* get_default() { return _default; }
1.88 moko 112: override void add(Value* avalue) { fhash.put(String::Body::uitoa(fhash.count()), avalue); }
1.76 moko 113:
114: void extract_default();
115:
116: private:
117:
118: HashStringValue fhash;
119: Value* _default;
120:
121: };
122:
123: /// value of type 'hash', implemented with Hash reference
124: class VHashReference: public VHashBase {
125: public: // value
126:
127: /// VHash: count
1.90 moko 128: override int as_int() const { return hash().count(); }
129: override double as_double() const { return hash().count(); }
130: override bool is_defined() const { return hash().count()!=0; }
131: override bool as_bool() const { return hash().count()!=0; }
132: override Value& as_expr_result() { return *new VInt(hash().count()); }
1.76 moko 133:
134: /// VHash: fhash
1.90 moko 135: override HashStringValue *get_hash() { return &hash(); }
136: override HashStringValue* get_fields() { return &hash(); }
137: override HashStringValue* get_fields_reference() { return &hash(); }
1.76 moko 138:
139: /// VHash: (key)=value
140: override Value* get_element(const String& aname) {
141: // $element first
1.90 moko 142: if(Value* result=hash().get(aname))
1.76 moko 143: return result;
144:
145: // $fields -- pseudo field to make 'hash' more like 'table'
146: if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
147: return this;
148:
1.91 ! moko 149: MAYBE_GET_SPECIAL_OR_METHOD(VStateless_object::get_element(aname));
1.76 moko 150:
151: return 0;
1.67 moko 152: }
1.76 moko 153:
154: #ifdef FEATURE_GET_ELEMENT4CALL
155: override Value* get_element4call(const String& aname) {
156: // $method first
157: if(Value* result=VStateless_object::get_element(aname))
158: return result;
1.67 moko 159:
1.76 moko 160: // $element
1.90 moko 161: if(Value* result=hash().get(aname))
1.76 moko 162: return result;
163:
164: return 0;
165: }
166: #endif
167:
168: /// VHash: (key)=value
169: override const VJunction* put_element(const String& aname, Value* avalue) {
1.90 moko 170: hash().put(aname, avalue);
1.81 moko 171: return 0;
1.22 parser 172: }
1.75 moko 173:
1.76 moko 174: public: // usage
175:
176: VHashReference(HashStringValue& source): fhash(&source) {}
1.53 paf 177:
1.76 moko 178: override HashStringValue& hash() { return *fhash; }
1.90 moko 179: HashStringValue& hash() const { return const_cast<VHashReference*>(this)->hash(); }
180:
1.78 moko 181: override void set_default(Value*) { }
1.76 moko 182: override Value* get_default() { return 0; }
1.90 moko 183: override void add(Value* avalue) { hash().put(String::Body::uitoa(hash().count()), avalue); }
1.13 paf 184:
1.90 moko 185: protected:
1.6 paf 186:
1.90 moko 187: VHashReference(): fhash(0) {}
1.76 moko 188: HashStringValue *fhash;
1.26 paf 189: };
190:
1.1 paf 191: #endif
E-mail: