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

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.35    ! paf        11: static const char* IDENT_VHASH_H="$Date: pa_vhash.h,v 1.34 2002/08/01 11:26:55 paf 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.1       paf        17: 
1.26      paf        18: // externs
                     19: 
1.33      paf        20: extern Methoded *hash_class;
1.13      paf        21: 
1.26      paf        22: // forwards
                     23: 
                     24: class VHash_lock;
                     25: 
1.11      paf        26: /// value of type 'hash', implemented with Hash
1.31      paf        27: class VHash : public VStateless_object {
1.26      paf        28:        friend class VHash_lock;
1.1       paf        29: public: // value
                     30: 
1.2       paf        31:        const char *type() const { return "hash"; }
1.33      paf        32:        VStateless_class *get_class() { return hash_class; }
1.17      parser     33: 
1.19      parser     34:        /// VHash: finteger
                     35:        int as_int() const { return fhash.size(); }
                     36:        /// VHash: finteger
                     37:        double as_double() const { return as_int(); }
1.17      parser     38:        /// VHash: count!=0
1.19      parser     39:        bool is_defined() const { return as_int()!=0; }
1.23      parser     40:        /// VHash: count!=0
                     41:        bool as_bool() const { return is_defined(); }
1.20      parser     42:        /// VHash: count
1.24      parser     43:        Value *as_expr_result(bool ) { return NEW VInt(pool(), as_int()); }
1.1       paf        44: 
1.11      paf        45:        /// VHash: fhash
1.26      paf        46:        Hash *get_hash(const String *source) { return &hash(source); }
1.14      paf        47: 
1.11      paf        48:        /// VHash: (key)=value
1.1       paf        49:        Value *get_element(const String& name) { 
1.15      parser     50:                // $CLASS,$method
1.31      paf        51:                if(Value *result=VStateless_object::get_element(name))
1.13      paf        52:                        return result;
                     53: 
                     54:                // $element
1.16      parser     55:                if(Value *result=static_cast<Value *>(fhash.get(name)))
                     56:                        return result;
                     57: 
                     58:                // default value
1.22      parser     59:                return get_default();
1.1       paf        60:        }
                     61:        
1.11      paf        62:        /// VHash: (key)=value
1.1       paf        63:        void put_element(const String& name, Value *value) { 
1.26      paf        64:                hash(&name).put(name, value);
1.1       paf        65:        }
                     66: 
                     67: public: // usage
                     68: 
1.33      paf        69:        VHash(Pool& apool) : VStateless_object(apool), 
1.26      paf        70:                fhash(apool), locked(false) {
1.18      parser     71:        }
                     72: 
1.33      paf        73:        VHash(Pool& apool, const Hash& source) : VStateless_object(apool), 
1.26      paf        74:                fhash(source), locked(false) {
1.13      paf        75:        }
1.7       paf        76: 
1.26      paf        77:        Hash& hash(const String *source) { 
                     78:                check_lock(source);
                     79:                return fhash; 
                     80:        }
1.1       paf        81: 
1.22      parser     82:        void set_default(Value& adefault) { 
1.32      paf        83:                hash(0).put(*hash_default_element_name, &adefault);
1.22      parser     84:        }
                     85:        Value *get_default() { 
                     86:                return static_cast<Value *>(fhash.get(*hash_default_element_name)); 
                     87:        }
1.13      paf        88: 
1.26      paf        89:        void check_lock(const String *source) {
                     90:                if(locked)
1.30      paf        91:                        throw Exception("parser.runtime",
1.26      paf        92:                                source,
                     93:                                "can not modify hash (locked)");
                     94:        }
                     95: 
1.1       paf        96: private:
1.6       paf        97: 
1.26      paf        98:        bool locked;
1.9       paf        99:        Hash fhash;
1.26      paf       100: 
                    101: };
                    102: 
                    103: class VHash_lock {
                    104:        VHash& fhash;
                    105:        bool saved;
                    106: public:
                    107:        VHash_lock(VHash& ahash) : fhash(ahash) {
                    108:                saved=fhash.locked;
                    109:                fhash.locked=true;
                    110:        }
                    111:        ~VHash_lock() {
                    112:                fhash.locked=saved;
                    113:        }
1.6       paf       114: 
1.1       paf       115: };
                    116: 
                    117: #endif

E-mail: