Annotation of parser3/src/types/pa_vhash.h, revision 1.72
1.10 paf 1: /** @file
2: Parser: @b hash parser type decl.
3:
1.72 ! moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.29 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_VHASH_H
9: #define PA_VHASH_H
1.34 paf 10:
1.72 ! moko 11: #define IDENT_PA_VHASH_H "$Id: pa_vhash.h,v 1.71 2015/10/15 18:12:29 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.1 paf 18:
1.39 paf 19: // defines
20:
21: #define VHASH_TYPE "hash"
1.63 moko 22:
1.42 paf 23: #define HASH_FIELDS_NAME "fields"
1.47 paf 24: #define HASH_DEFAULT_ELEMENT_NAME "_default"
1.63 moko 25: extern const String hash_fields_name, hash_default_element_name;
1.26 paf 26:
1.46 paf 27: extern Methoded* hash_class;
1.13 paf 28:
1.26 paf 29: // forwards
30:
31: class VHash_lock;
32:
1.11 paf 33: /// value of type 'hash', implemented with Hash
1.46 paf 34: class VHash: public VStateless_object {
1.26 paf 35: friend class VHash_lock;
1.1 paf 36: public: // value
37:
1.46 paf 38: override const char* type() const { return VHASH_TYPE; }
39: override VStateless_class *get_class() { return hash_class; }
1.17 parser 40:
1.19 parser 41: /// VHash: finteger
1.46 paf 42: override int as_int() const { return fhash.count(); }
1.19 parser 43: /// VHash: finteger
1.68 misha 44: override double as_double() const { return fhash.count(); }
1.17 parser 45: /// VHash: count!=0
1.68 misha 46: override bool is_defined() const { return fhash.count()!=0; }
1.23 parser 47: /// VHash: count!=0
1.68 misha 48: override bool as_bool() const { return fhash.count()!=0; }
1.20 parser 49: /// VHash: count
1.65 moko 50: override Value& as_expr_result() { return *new VInt(as_int()); }
1.1 paf 51:
1.11 paf 52: /// VHash: fhash
1.46 paf 53: override HashStringValue *get_hash() { return &hash(); }
1.14 paf 54:
1.66 misha 55: override HashStringValue* get_fields() { return &fhash; }
56:
1.11 paf 57: /// VHash: (key)=value
1.62 misha 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.63 moko 64: if(aname == hash_fields_name)
1.42 paf 65: return this;
1.13 paf 66:
1.71 moko 67: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
1.45 paf 68: // $method
1.62 misha 69: if(Value* result=VStateless_object::get_element(aname))
1.16 parser 70: return result;
1.71 moko 71: #endif
1.16 parser 72:
73: // default value
1.22 parser 74: return get_default();
1.1 paf 75: }
76:
1.70 moko 77: #ifdef FEATURE_GET_ELEMENT4CALL
78: override Value* get_element4call(const String& aname) {
79: // $method first
80: if(Value* result=VStateless_object::get_element(aname))
81: return result;
82:
83: // $element
84: if(Value* result=fhash.get(aname))
85: return result;
86:
87: // $fields -- pseudo field to make 'hash' more like 'table'
88: if(aname == hash_fields_name)
89: return this;
90:
91: // default value
92: return get_default();
93: }
94: #endif
95:
1.11 paf 96: /// VHash: (key)=value
1.69 moko 97: override const VJunction* put_element(const String& aname, Value* avalue) {
1.63 moko 98: if(aname==hash_default_element_name)
1.47 paf 99: set_default(avalue);
100: else
101: if(flocked) {
1.55 paf 102: if(!fhash.put_replaced(aname, avalue))
1.59 misha 103: throw Exception(PARSER_RUNTIME,
1.47 paf 104: &aname,
105: "can not insert new hash key (hash flocked)");
106: } else
107: fhash.put(aname, avalue);
1.36 paf 108:
1.54 paf 109: return PUT_ELEMENT_REPLACED_ELEMENT;
1.1 paf 110: }
1.52 paf 111:
1.61 misha 112: override VFile* as_vfile(String::Language lang, const Request_charsets *charsets=0);
1.1 paf 113:
114: public: // usage
115:
1.47 paf 116: VHash(): flocked(false), _default(0) {}
1.18 parser 117:
1.47 paf 118: VHash(const HashStringValue& source): fhash(source), flocked(false), _default(0) {}
1.7 paf 119:
1.46 paf 120: HashStringValue& hash() {
121: check_lock();
1.26 paf 122: return fhash;
123: }
1.1 paf 124:
1.67 moko 125: HashStringValue& hash_ro() {
126: return fhash;
127: }
128:
1.44 paf 129: void set_default(Value* adefault) {
1.47 paf 130: _default=adefault;
1.22 parser 131: }
1.46 paf 132: Value* get_default() {
1.47 paf 133: return _default;
1.22 parser 134: }
1.53 paf 135:
136: void extract_default();
1.13 paf 137:
1.46 paf 138: void check_lock() {
139: if(flocked)
1.59 misha 140: throw Exception(PARSER_RUNTIME,
1.46 paf 141: 0,
142: "can not modify hash (flocked)");
1.26 paf 143: }
144:
1.1 paf 145: private:
1.6 paf 146:
1.48 paf 147: HashStringValue fhash;
1.46 paf 148: bool flocked;
1.47 paf 149: Value* _default;
1.26 paf 150:
151: };
152:
153: class VHash_lock {
154: VHash& fhash;
155: bool saved;
156: public:
1.46 paf 157: VHash_lock(VHash& ahash): fhash(ahash) {
158: saved=fhash.flocked;
159: fhash.flocked=true;
1.26 paf 160: }
161: ~VHash_lock() {
1.46 paf 162: fhash.flocked=saved;
1.26 paf 163: }
1.6 paf 164:
1.1 paf 165: };
166:
167: #endif
E-mail: