Annotation of parser3/src/types/pa_vhash.h, revision 1.74

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.74    ! moko       11: #define IDENT_PA_VHASH_H "$Id: pa_vhash.h,v 1.73 2016/05/19 12:46:35 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.26      paf        26: // forwards
                     27: 
                     28: class VHash_lock;
                     29: 
1.11      paf        30: /// value of type 'hash', implemented with Hash
1.46      paf        31: class VHash: public VStateless_object {
1.26      paf        32:        friend class VHash_lock;
1.1       paf        33: public: // value
                     34: 
1.46      paf        35:        override const char* type() const { return VHASH_TYPE; }
                     36:        override VStateless_class *get_class() { return hash_class; }
1.17      parser     37: 
1.19      parser     38:        /// VHash: finteger
1.46      paf        39:        override int as_int() const { return fhash.count(); }
1.19      parser     40:        /// VHash: finteger
1.68      misha      41:        override double as_double() const { return fhash.count(); }
1.17      parser     42:        /// VHash: count!=0
1.68      misha      43:        override bool is_defined() const { return fhash.count()!=0; }
1.23      parser     44:        /// VHash: count!=0
1.68      misha      45:        override bool as_bool() const { return fhash.count()!=0; }
1.20      parser     46:        /// VHash: count
1.65      moko       47:        override Value& as_expr_result() { return *new VInt(as_int()); }
1.1       paf        48: 
1.11      paf        49:        /// VHash: fhash
1.46      paf        50:        override HashStringValue *get_hash() { return &hash(); }
1.14      paf        51: 
1.66      misha      52:        override HashStringValue* get_fields() { return &fhash; }
                     53: 
1.11      paf        54:        /// VHash: (key)=value
1.62      misha      55:        override Value* get_element(const String& aname) { 
1.70      moko       56:                // $element first
1.46      paf        57:                if(Value* result=fhash.get(aname))
1.13      paf        58:                        return result;
1.42      paf        59: 
                     60:                // $fields -- pseudo field to make 'hash' more like 'table'
1.74    ! moko       61:                if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
1.42      paf        62:                        return this;
1.13      paf        63: 
1.71      moko       64: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
1.73      moko       65:                // $method, CLASS, CLASS_NAME
1.62      misha      66:                if(Value* result=VStateless_object::get_element(aname))
1.16      parser     67:                        return result;
1.71      moko       68: #endif
1.16      parser     69: 
                     70:                // default value
1.22      parser     71:                return get_default();
1.1       paf        72:        }
                     73:        
1.70      moko       74: #ifdef FEATURE_GET_ELEMENT4CALL
                     75:        override Value* get_element4call(const String& aname) {
                     76:                // $method first
                     77:                if(Value* result=VStateless_object::get_element(aname))
                     78:                        return result;
                     79: 
                     80:                // $element
                     81:                if(Value* result=fhash.get(aname))
                     82:                        return result;
                     83: 
                     84:                // default value
                     85:                return get_default();
                     86:        }
                     87: #endif
                     88:        
1.11      paf        89:        /// VHash: (key)=value
1.69      moko       90:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.74    ! moko       91:                if(SYMBOLS_EQ(aname,_DEFAULT_SYMBOL))
1.47      paf        92:                        set_default(avalue);
                     93:                else 
                     94:                        if(flocked) {
1.55      paf        95:                                if(!fhash.put_replaced(aname, avalue))
1.59      misha      96:                                        throw Exception(PARSER_RUNTIME,
1.47      paf        97:                                                &aname,
                     98:                                                "can not insert new hash key (hash flocked)");
                     99:                        } else
                    100:                                        fhash.put(aname, avalue);
1.36      paf       101: 
1.54      paf       102:                return PUT_ELEMENT_REPLACED_ELEMENT;
1.1       paf       103:        }
1.52      paf       104: 
1.61      misha     105:        override VFile* as_vfile(String::Language lang, const Request_charsets *charsets=0);
1.1       paf       106: 
                    107: public: // usage
                    108: 
1.47      paf       109:        VHash(): flocked(false), _default(0) {}
1.18      parser    110: 
1.47      paf       111:        VHash(const HashStringValue& source): fhash(source), flocked(false), _default(0) {}
1.7       paf       112: 
1.46      paf       113:        HashStringValue& hash() { 
                    114:                check_lock();
1.26      paf       115:                return fhash; 
                    116:        }
1.1       paf       117: 
1.67      moko      118:        HashStringValue& hash_ro() { 
                    119:                return fhash; 
                    120:        }
                    121: 
1.44      paf       122:        void set_default(Value* adefault) { 
1.47      paf       123:                _default=adefault;
1.22      parser    124:        }
1.46      paf       125:        Value* get_default() { 
1.47      paf       126:                return _default;
1.22      parser    127:        }
1.53      paf       128: 
                    129:        void extract_default();
1.13      paf       130: 
1.46      paf       131:        void check_lock() {
                    132:                if(flocked)
1.59      misha     133:                        throw Exception(PARSER_RUNTIME,
1.46      paf       134:                                0,
                    135:                                "can not modify hash (flocked)");
1.26      paf       136:        }
                    137: 
1.1       paf       138: private:
1.6       paf       139: 
1.48      paf       140:        HashStringValue fhash;
1.46      paf       141:        bool flocked;
1.47      paf       142:        Value* _default;
1.26      paf       143: 
                    144: };
                    145: 
                    146: class VHash_lock {
                    147:        VHash& fhash;
                    148:        bool saved;
                    149: public:
1.46      paf       150:        VHash_lock(VHash& ahash): fhash(ahash) {
                    151:                saved=fhash.flocked;
                    152:                fhash.flocked=true;
1.26      paf       153:        }
                    154:        ~VHash_lock() {
1.46      paf       155:                fhash.flocked=saved;
1.26      paf       156:        }
1.6       paf       157: 
1.1       paf       158: };
                    159: 
                    160: #endif

E-mail: