Annotation of parser3/src/types/pa_vhash.h, revision 1.70
1.10 paf 1: /** @file
2: Parser: @b hash parser type decl.
3:
1.64 moko 4: Copyright (c) 2001-2012 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.70 ! moko 11: #define IDENT_PA_VHASH_H "$Id: pa_vhash.h,v 1.69 2013/10/04 21:21:56 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.45 paf 67: // $method
1.62 misha 68: if(Value* result=VStateless_object::get_element(aname))
1.16 parser 69: return result;
70:
71: // default value
1.22 parser 72: return get_default();
1.1 paf 73: }
74:
1.70 ! moko 75: #ifdef FEATURE_GET_ELEMENT4CALL
! 76: override Value* get_element4call(const String& aname) {
! 77: // $method first
! 78: if(Value* result=VStateless_object::get_element(aname))
! 79: return result;
! 80:
! 81: // $element
! 82: if(Value* result=fhash.get(aname))
! 83: return result;
! 84:
! 85: // $fields -- pseudo field to make 'hash' more like 'table'
! 86: if(aname == hash_fields_name)
! 87: return this;
! 88:
! 89: // default value
! 90: return get_default();
! 91: }
! 92: #endif
! 93:
1.11 paf 94: /// VHash: (key)=value
1.69 moko 95: override const VJunction* put_element(const String& aname, Value* avalue) {
1.63 moko 96: if(aname==hash_default_element_name)
1.47 paf 97: set_default(avalue);
98: else
99: if(flocked) {
1.55 paf 100: if(!fhash.put_replaced(aname, avalue))
1.59 misha 101: throw Exception(PARSER_RUNTIME,
1.47 paf 102: &aname,
103: "can not insert new hash key (hash flocked)");
104: } else
105: fhash.put(aname, avalue);
1.36 paf 106:
1.54 paf 107: return PUT_ELEMENT_REPLACED_ELEMENT;
1.1 paf 108: }
1.52 paf 109:
1.61 misha 110: override VFile* as_vfile(String::Language lang, const Request_charsets *charsets=0);
1.1 paf 111:
112: public: // usage
113:
1.47 paf 114: VHash(): flocked(false), _default(0) {}
1.18 parser 115:
1.47 paf 116: VHash(const HashStringValue& source): fhash(source), flocked(false), _default(0) {}
1.7 paf 117:
1.46 paf 118: HashStringValue& hash() {
119: check_lock();
1.26 paf 120: return fhash;
121: }
1.1 paf 122:
1.67 moko 123: HashStringValue& hash_ro() {
124: return fhash;
125: }
126:
1.44 paf 127: void set_default(Value* adefault) {
1.47 paf 128: _default=adefault;
1.22 parser 129: }
1.46 paf 130: Value* get_default() {
1.47 paf 131: return _default;
1.22 parser 132: }
1.53 paf 133:
134: void extract_default();
1.13 paf 135:
1.46 paf 136: void check_lock() {
137: if(flocked)
1.59 misha 138: throw Exception(PARSER_RUNTIME,
1.46 paf 139: 0,
140: "can not modify hash (flocked)");
1.26 paf 141: }
142:
1.1 paf 143: private:
1.6 paf 144:
1.48 paf 145: HashStringValue fhash;
1.46 paf 146: bool flocked;
1.47 paf 147: Value* _default;
1.26 paf 148:
149: };
150:
151: class VHash_lock {
152: VHash& fhash;
153: bool saved;
154: public:
1.46 paf 155: VHash_lock(VHash& ahash): fhash(ahash) {
156: saved=fhash.flocked;
157: fhash.flocked=true;
1.26 paf 158: }
159: ~VHash_lock() {
1.46 paf 160: fhash.flocked=saved;
1.26 paf 161: }
1.6 paf 162:
1.1 paf 163: };
164:
165: #endif
E-mail: