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

1.12      paf         1: /**    @file
                      2:        Parser: write context class decl.
                      3: 
1.60      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (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.67    ! moko       11: #define IDENT_PA_WCONTEXT_H "$Id: pa_wcontext.h,v 1.66 2016/10/27 22:40:48 moko Exp $"
1.1       paf        12: 
                     13: #include "pa_value.h"
                     14: #include "pa_vstring.h"
                     15: #include "pa_vhash.h"
                     16: 
                     17: class Request;
                     18: 
1.63      moko       19: /** ValueRef
                     20:        convenient helper when delayed initialization required
                     21: */
                     22: 
                     23: class ValueRef {
1.29      paf        24: public:
1.63      moko       25:        ValueRef() : fvalue(0) {}
                     26:        ValueRef(Value& avalue) : fvalue(&avalue) {}
                     27:        operator Value& () { return *fvalue; }
1.29      paf        28: private:
1.43      paf        29:        Value* fvalue;
1.29      paf        30: };
                     31: 
1.12      paf        32: /** Write context
                     33:        they do different write()s here, later picking up the result
                     34:        @see Request::wcontext
                     35: */
1.63      moko       36: 
1.43      paf        37: class WContext: public Value {
1.18      paf        38:        friend class Request;
1.19      paf        39: 
1.1       paf        40: public: // Value
                     41: 
1.43      paf        42:        override const char* type() const { return "wcontext"; }
1.12      paf        43:        /// WContext: accumulated fstring
1.51      misha      44:        override const String* get_string() {
                     45:                static String empty;
1.63      moko       46:                return fstring ? fstring : &empty;
1.51      misha      47:        };
1.1       paf        48: 
1.12      paf        49:        /// WContext: none yet | transparent
1.63      moko       50:        override VStateless_class *get_class() { return fvalue ? fvalue->get_class() : 0; }
1.1       paf        51: 
                     52: public: // WContext
                     53: 
1.12      paf        54:        /// appends a fstring to result
1.65      moko       55:        virtual void write(const String& astring) {
1.51      misha      56:                if(!fstring) fstring=new String;
1.65      moko       57:                astring.append_to(*fstring);
1.35      paf        58:        }
1.32      paf        59:        /// writes Value; raises an error if already, providing origin
1.43      paf        60:        virtual void write(Value& avalue);
1.1       paf        61: 
1.66      moko       62:        /// if value is string convertable writes fstring, else writes Value
1.65      moko       63:        virtual void write_as_string(Value& avalue) {
1.62      moko       64:                if(const String* string=avalue.get_string())
1.65      moko       65:                        write(*string);
1.32      paf        66:                else
1.43      paf        67:                        write(avalue);
1.32      paf        68:        }
1.1       paf        69: 
1.12      paf        70:        /**
                     71:                retrives the resulting value
1.29      paf        72:                that can be String if value==0 or the Value object
1.12      paf        73:                wmethod_frame first checks for $result and if there is one, returns it instead
                     74:        */
1.63      moko       75:        virtual Value& result() {
1.51      misha      76:                static String empty;
1.63      moko       77:                static VString vempty(empty);
                     78:                return fvalue ? *fvalue: fstring ? *new VString(*fstring) : vempty;
1.7       paf        79:        }
                     80: 
1.52      misha      81:        void attach_junction(VJunction* ajunction) {
1.43      paf        82:                junctions+=ajunction;
1.39      paf        83:        }
                     84: 
1.1       paf        85: public: // usage
                     86: 
1.55      misha      87:        WContext(WContext *aparent):
                     88:                fparent(aparent),
1.51      misha      89:                fstring(0),
1.67    ! moko       90:                fvalue(0){}
1.54      misha      91: 
1.44      paf        92:        virtual ~WContext() {
1.39      paf        93:                detach_junctions();
                     94:        }
1.1       paf        95: 
1.39      paf        96: private:
                     97: 
                     98:        void detach_junctions(); 
                     99: 
1.1       paf       100: protected:
1.55      misha     101:        WContext *fparent;
1.51      misha     102:        String* fstring;
1.43      paf       103:        Value* fvalue;
1.48      paf       104: 
1.39      paf       105: private:
1.52      misha     106:        Array<VJunction*> junctions;
1.38      paf       107: 
1.1       paf       108: };
                    109: 
                    110: #endif

E-mail: