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

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

E-mail: