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

1.10      paf         1: /** @file
                      2:        Parser: @b hash parser type decl.
                      3: 
1.43      paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (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.44    ! paf        11: static const char* IDENT_VHASH_H="$Date: 2003/01/21 15:51:19 $";
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.1       paf        17: 
1.39      paf        18: // defines
                     19: 
                     20: #define VHASH_TYPE "hash"
1.42      paf        21: #define HASH_FIELDS_NAME "fields"
1.39      paf        22: 
1.26      paf        23: // externs
                     24: 
1.33      paf        25: extern Methoded *hash_class;
1.13      paf        26: 
1.26      paf        27: // forwards
                     28: 
                     29: class VHash_lock;
                     30: 
1.11      paf        31: /// value of type 'hash', implemented with Hash
1.31      paf        32: class VHash : public VStateless_object {
1.26      paf        33:        friend class VHash_lock;
1.1       paf        34: public: // value
                     35: 
1.39      paf        36:        const char *type() const { return VHASH_TYPE; }
1.33      paf        37:        VStateless_class *get_class() { return hash_class; }
1.17      parser     38: 
1.19      parser     39:        /// VHash: finteger
                     40:        int as_int() const { return fhash.size(); }
                     41:        /// VHash: finteger
                     42:        double as_double() const { return as_int(); }
1.17      parser     43:        /// VHash: count!=0
1.19      parser     44:        bool is_defined() const { return as_int()!=0; }
1.23      parser     45:        /// VHash: count!=0
                     46:        bool as_bool() const { return is_defined(); }
1.20      parser     47:        /// VHash: count
1.24      parser     48:        Value *as_expr_result(bool ) { return NEW VInt(pool(), as_int()); }
1.1       paf        49: 
1.11      paf        50:        /// VHash: fhash
1.26      paf        51:        Hash *get_hash(const String *source) { return &hash(source); }
1.14      paf        52: 
1.11      paf        53:        /// VHash: (key)=value
1.41      paf        54:        Value *get_element(const String& aname, Value& aself, bool looking_up) { 
1.36      paf        55:                // $method
1.38      paf        56:                if(Value *result=VStateless_object::get_element(aname, aself, looking_up))
1.13      paf        57:                        return result;
1.42      paf        58: 
                     59:                // $fields -- pseudo field to make 'hash' more like 'table'
                     60:                if(aname == HASH_FIELDS_NAME)
                     61:                        return this;
1.13      paf        62: 
                     63:                // $element
1.36      paf        64:                if(Value *result=static_cast<Value *>(fhash.get(aname)))
1.16      parser     65:                        return result;
                     66: 
                     67:                // default value
1.22      parser     68:                return get_default();
1.1       paf        69:        }
                     70:        
1.11      paf        71:        /// VHash: (key)=value
1.36      paf        72:        /*override*/ bool put_element(const String& aname, Value *avalue, bool /*replace*/) { 
1.40      paf        73:                if(locked) {
                     74:                        if(!fhash.put_replace(aname, avalue))
                     75:                                throw Exception("parser.runtime",
                     76:                                        &aname,
                     77:                                        "can not insert new hash key (hash locked)");
1.44    ! paf        78:                } else {
        !            79:                        if(aname==*hash_default_element_name)
        !            80:                                set_default(avalue);
        !            81:                        else 
        !            82:                                fhash.put(aname, avalue);
        !            83:                }
1.36      paf        84: 
                     85:                return true;
1.1       paf        86:        }
                     87: 
                     88: public: // usage
                     89: 
1.33      paf        90:        VHash(Pool& apool) : VStateless_object(apool), 
1.44    ! paf        91:                fhash(apool), locked(false), _default(0) {
1.18      parser     92:        }
                     93: 
1.33      paf        94:        VHash(Pool& apool, const Hash& source) : VStateless_object(apool), 
1.44    ! paf        95:                fhash(source), locked(false), _default(0) {
1.13      paf        96:        }
1.7       paf        97: 
1.26      paf        98:        Hash& hash(const String *source) { 
                     99:                check_lock(source);
                    100:                return fhash; 
                    101:        }
1.1       paf       102: 
1.44    ! paf       103:        void set_default(Value* adefault) { 
        !           104:                _default=adefault;
1.22      parser    105:        }
                    106:        Value *get_default() { 
1.44    ! paf       107:                return _default;
1.22      parser    108:        }
1.13      paf       109: 
1.26      paf       110:        void check_lock(const String *source) {
                    111:                if(locked)
1.30      paf       112:                        throw Exception("parser.runtime",
1.26      paf       113:                                source,
                    114:                                "can not modify hash (locked)");
                    115:        }
                    116: 
1.1       paf       117: private:
1.6       paf       118: 
1.26      paf       119:        bool locked;
1.9       paf       120:        Hash fhash;
1.44    ! paf       121:        Value* _default;
1.26      paf       122: 
                    123: };
                    124: 
                    125: class VHash_lock {
                    126:        VHash& fhash;
                    127:        bool saved;
                    128: public:
                    129:        VHash_lock(VHash& ahash) : fhash(ahash) {
                    130:                saved=fhash.locked;
                    131:                fhash.locked=true;
                    132:        }
                    133:        ~VHash_lock() {
                    134:                fhash.locked=saved;
                    135:        }
1.6       paf       136: 
1.1       paf       137: };
                    138: 
                    139: #endif

E-mail: