Annotation of parser3/src/types/pa_wcontext.h, revision 1.55

1.12      paf         1: /**    @file
                      2:        Parser: write context class decl.
                      3: 
1.51      misha       4:        Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.27      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_WCONTEXT_H
                      9: #define PA_WCONTEXT_H
1.33      paf        10: 
1.55    ! misha      11: static const char * const IDENT_WCONTEXT_H="$Date: 2009-06-16 08:38:40 $";
1.1       paf        12: 
                     13: #include "pa_value.h"
                     14: #include "pa_vstring.h"
                     15: #include "pa_vhash.h"
                     16: 
1.54      misha      17: #define OPTIMIZE_CONSTRUCT_OBJECT
                     18: 
1.1       paf        19: class Request;
                     20: 
1.29      paf        21: class StringOrValue {
                     22: public:
                     23:        StringOrValue() : fstring(0), fvalue(0) {}
                     24:        /// anticipating either String or Value [must not be 0&0]
1.47      paf        25:        StringOrValue(const String& astring) : fstring(&astring), fvalue(0) {}
                     26:        StringOrValue(Value& avalue) : fstring(0), fvalue(&avalue) {}
                     27: 
1.30      paf        28:        void set_string(const String& astring) { fstring=&astring; }
1.29      paf        29:        void set_value(Value& avalue) { fvalue=&avalue; }
1.43      paf        30:        const String* get_string() { return fstring; }
                     31:        Value* get_value() { return fvalue; }
1.29      paf        32:        Value& as_value() const {
1.43      paf        33:                Value* result=fvalue?fvalue:new VString(*fstring);
                     34:                return *result;
1.29      paf        35:        }
                     36:        const String& as_string() const {
                     37:                return fstring?*fstring:fvalue->as_string();
                     38:        }
                     39: private:
1.43      paf        40:        const String* fstring;
                     41:        Value* fvalue;
1.29      paf        42: };
                     43: 
1.12      paf        44: /** Write context
                     45:        they do different write()s here, later picking up the result
                     46:        @see Request::wcontext
                     47: */
1.43      paf        48: class WContext: public Value {
1.18      paf        49:        friend class Request;
1.19      paf        50: 
1.1       paf        51: public: // Value
                     52: 
1.43      paf        53:        override const char* type() const { return "wcontext"; }
1.12      paf        54:        /// WContext: accumulated fstring
1.51      misha      55:        override const String* get_string() {
                     56:                static String empty;
                     57:                return fstring?fstring:&empty;
                     58:        };
1.1       paf        59: 
1.12      paf        60:        /// WContext: none yet | transparent
1.43      paf        61:        override VStateless_class *get_class() { return fvalue?fvalue->get_class():0; }
1.1       paf        62: 
                     63: public: // WContext
                     64: 
1.12      paf        65:        /// appends a fstring to result
1.43      paf        66:        virtual void write(const String& astring, String::Language alang) {
1.51      misha      67:                if(!fstring) fstring=new String;
                     68:                fstring->append(astring, alang);
1.35      paf        69:        }
1.32      paf        70:        /// writes Value; raises an error if already, providing origin
1.43      paf        71:        virtual void write(Value& avalue);
1.1       paf        72: 
1.12      paf        73:        /**
                     74:                if value is VString writes fstring,
1.32      paf        75:                else writes Value; raises an error if already, providing origin
                     76:        */
1.53      misha      77:        virtual void write(Value& avalue, String::Language alang) {
1.43      paf        78:                if(const String* fstring=avalue.get_string())
                     79:                        write(*fstring, alang);
1.32      paf        80:                else
1.43      paf        81:                        write(avalue);
1.32      paf        82:        }
1.1       paf        83: 
1.12      paf        84:        /**
                     85:                retrives the resulting value
1.29      paf        86:                that can be String if value==0 or the Value object
1.12      paf        87:                wmethod_frame first checks for $result and if there is one, returns it instead
                     88:        */
1.29      paf        89:        virtual StringOrValue result() {
1.51      misha      90:                static String empty;
                     91:                return fvalue?StringOrValue(*fvalue):StringOrValue(fstring?*fstring:empty);
1.7       paf        92:        }
                     93: 
1.52      misha      94:        void attach_junction(VJunction* ajunction) {
1.43      paf        95:                junctions+=ajunction;
1.39      paf        96:        }
                     97: 
1.1       paf        98: public: // usage
                     99: 
1.55    ! misha     100:        WContext(WContext *aparent):
        !           101:                fparent(aparent),
1.51      misha     102:                fstring(0),
1.55    ! misha     103:                fvalue(0),
1.54      misha     104: #ifndef OPTIMIZE_CONSTRUCT_OBJECT
                    105:                constructing(false),
                    106: #endif 
                    107:                in_expression(false),
                    108:                entered_class(false){}
                    109: 
1.44      paf       110:        virtual ~WContext() {
1.39      paf       111:                detach_junctions();
                    112:        }
1.1       paf       113: 
1.54      misha     114: #ifdef OPTIMIZE_CONSTRUCT_OBJECT
                    115: #define GET_CONSTRUCTING(w) false
                    116: #define SET_CONSTRUCTING(w,v)
                    117: #else
1.48      paf       118:        void set_constructing(bool aconstructing) { constructing=aconstructing; }
                    119:        bool get_constructing() { return constructing; }
1.54      misha     120: #define GET_CONSTRUCTING(w) (w->get_constructing())
                    121: #define SET_CONSTRUCTING(w,v) w->set_constructing(v);
                    122: #endif 
1.19      paf       123: 
1.48      paf       124:        void set_in_expression(bool ain_expression) { in_expression=ain_expression; }
                    125:        bool get_in_expression() { return in_expression; }
1.28      paf       126: 
1.48      paf       127:        void set_somebody_entered_some_class() { entered_class=true; }
                    128:        bool get_somebody_entered_some_class() { return entered_class; }
1.14      paf       129: 
1.39      paf       130: private:
                    131: 
                    132:        void detach_junctions(); 
                    133: 
1.1       paf       134: protected:
1.55    ! misha     135:        WContext *fparent;
1.51      misha     136:        String* fstring;
1.43      paf       137:        Value* fvalue;
1.48      paf       138: 
                    139: private: // status
                    140: 
1.54      misha     141: #ifndef OPTIMIZE_CONSTRUCT_OBJECT
1.48      paf       142:        bool constructing;
1.54      misha     143: #endif 
1.48      paf       144:        bool in_expression;
                    145:        bool entered_class;
1.39      paf       146: 
                    147: private:
1.52      misha     148:        Array<VJunction*> junctions;
1.38      paf       149: 
1.1       paf       150: };
                    151: 
                    152: #endif

E-mail: