Annotation of parser3/src/include/pa_pool.h, revision 1.60

1.34      paf         1: /** @file
1.36      paf         2:        Parser: pool class decl.
                      3: 
1.27      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.36      paf         5: 
1.28      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.27      paf         7: 
1.60    ! parser      8:        $Id: pa_pool.h,v 1.59 2001/09/21 14:46:09 parser Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_POOL_H
                     12: #define PA_POOL_H
                     13: 
1.52      parser     14: #include "pa_config_includes.h"
1.1       paf        15: 
1.59      parser     16: #ifdef XML
1.55      parser     17: #include <XalanDOM/XalanDOMString.hpp>
                     18: #include <util/TransService.hpp>
                     19: #include <PlatformSupport/XSLException.hpp>
1.59      parser     20: #endif
1.55      parser     21: 
                     22: // forwards
                     23: 
1.21      paf        24: class Exception;
1.25      paf        25: class Temp_exception;
1.55      parser     26: class String;
                     27: 
1.35      paf        28: /** 
1.34      paf        29:        Pool mechanizm allows users not to free up allocated memory,
                     30:        leaving that problem to 'pools'.
                     31: 
                     32:        Also holds Exception object, which can be temporary set using 
                     33:        Temp_exception auto-object.
1.38      paf        34: 
                     35:        @see Pooled
1.34      paf        36: */
1.47      paf        37: 
1.1       paf        38: class Pool {
1.25      paf        39:        friend Temp_exception;
1.1       paf        40: public:
1.18      paf        41: 
1.55      parser     42:        Pool(void *astorage);
1.56      parser     43:        ~Pool();
1.18      paf        44: 
1.40      paf        45:        void set_context(void *acontext) { fcontext=acontext; }
                     46:        void *context() { return fcontext; }
1.39      paf        47: 
1.47      paf        48:        void set_tag(void *atag) { ftag=atag; }
                     49:        void *tag() { return ftag; }
1.44      paf        50: 
1.34      paf        51:        /// allocates some bytes on pool
1.51      parser     52:        void *malloc(size_t size/*, int place=0*/) {
                     53:                return check(real_malloc(size/*, place*/), size);
1.18      paf        54:        }
1.34      paf        55:        /// allocates some bytes clearing them with zeros
1.18      paf        56:        void *calloc(size_t size) {
                     57:                return check(real_calloc(size), size);
                     58:        }
1.39      paf        59: 
1.53      parser     60:        /// registers a routine to clean up non-pooled allocations
1.54      parser     61:        void register_cleanup(void (*cleanup) (void *), void *data) {
                     62:                if(!real_register_cleanup(cleanup, data))
                     63:                        fail_register_cleanup();
                     64:        }
1.53      parser     65: 
                     66:        /// current exception object of the pool
                     67:        Exception& exception() const { return *fexception; }
                     68: 
1.60    ! parser     69:        /// resets transcoder if they change charset 
        !            70:        void set_charset(const String &charset);
        !            71:        /// returns current charset
        !            72:        const String& get_charset() { return *charset; }
        !            73: 
1.39      paf        74: private:
                     75: 
                     76:        void *fstorage;
1.40      paf        77:        void *fcontext;
1.47      paf        78:        void *ftag;
1.60    ! parser     79:        const String *charset;
1.8       paf        80: 
1.50      parser     81: private: 
                     82:        
                     83:        //{
                     84:        /// @name implementation defined
1.51      parser     85:     void *real_malloc(size_t size/*, int place*/);
1.20      paf        86:     void *real_calloc(size_t size);
1.54      parser     87:        bool real_register_cleanup(void (*cleanup) (void *), void *data);
1.50      parser     88:        //}
1.17      paf        89: 
1.23      paf        90: private: 
1.17      paf        91: 
1.34      paf        92:        /// checks whether mem allocated OK. throws exception otherwise
1.23      paf        93:        void *check(void *ptr, size_t size) {
                     94:                if(ptr)
                     95:                        return ptr;
                     96: 
1.54      parser     97:                fail_alloc(size);
1.23      paf        98: 
                     99:                // never reached
                    100:                return 0;
                    101:        }
1.54      parser    102:        /// throws allocation exception
                    103:        void fail_alloc(size_t size) const;
                    104: 
                    105:        /// throws register cleanup exception
                    106:        void fail_register_cleanup() const;
1.23      paf       107: 
1.34      paf       108: private: // exception handling
1.17      paf       109: 
1.45      paf       110:        // exception replacement mechanism is 'private'zed from direct usage
1.34      paf       111:        // Temp_exception object enforces paired set/restore
1.23      paf       112:        Exception *set_exception(Exception *e){
1.31      paf       113:                Exception *r=fexception;
                    114:                fexception=e;
1.23      paf       115:                return r;
                    116:        }
                    117:        void restore_exception(Exception *e) {
1.31      paf       118:                fexception=e;
1.23      paf       119:        }
                    120: 
                    121: private:
                    122: 
                    123:        // current request's exception object
1.31      paf       124:        Exception *fexception;
1.17      paf       125: 
1.59      parser    126: #ifdef XML
                    127: 
                    128: public:
                    129:        /// converts Xalan string to char *
                    130:        const char *transcode_cstr(const XalanDOMString& s);
                    131:        /// converts Xalan string to parser String
                    132:        String& transcode(const XalanDOMString& s);
                    133:        /// converts XSL exception to parser exception
                    134:        void _throw(const String *source, const XSLException& e);
                    135: 
                    136: private:
                    137: 
                    138:        void set_charset(const char *new_scharset);
                    139:        void update_transcoder();
                    140: 
                    141: private:
                    142: 
                    143:        XMLTranscoder *transcoder;
                    144: 
                    145: #endif
                    146: 
1.8       paf       147: private: //disabled
                    148: 
1.18      paf       149:        // Pool(const Pool&) {}
1.16      paf       150:        Pool& operator = (const Pool&) { return *this; }
1.21      paf       151: };
                    152: 
1.35      paf       153: /** 
1.34      paf       154:        Base for all classes that are allocated in 'pools'.
                    155: 
                    156:        Holds Pool object. Contains useful wrappers to it's methods.
1.38      paf       157: 
                    158:        @see NEW, Temp_exception
1.34      paf       159: */
1.21      paf       160: class Pooled {
1.23      paf       161:        // the pool i'm allocated on
1.49      paf       162:        Pool *fpool;
1.21      paf       163: public:
1.37      paf       164: 
                    165:        /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21      paf       166:        static void *operator new(size_t size, Pool& apool) { 
1.51      parser    167:                return apool.malloc(size/*, 1*/);
1.21      paf       168:        }
                    169: 
1.49      paf       170:        Pooled(Pool& apool) : fpool(&apool) {}
1.23      paf       171: 
1.34      paf       172:        /// my pool
1.49      paf       173:        Pool& pool() const { return *fpool; }
                    174: 
                    175:        /** used for moving objects from one pool to another. 
                    176:                in between object can have no pool and can not be used
                    177:                @see SQL_Driver_manager
                    178:        */
                    179:        void set_pool(Pool *apool) { fpool=apool; }
1.21      paf       180: 
1.57      parser    181:        //{
                    182:        /// @name useful wrapper around pool
1.49      paf       183:        void *malloc(size_t size) const { return fpool->malloc(size); }
                    184:        void *calloc(size_t size) const { return fpool->calloc(size); }
1.57      parser    185:        void register_cleanup(void (*cleanup) (void *), void *data) { fpool->register_cleanup(cleanup, data); }
1.49      paf       186:        Exception& exception() const { return fpool->exception(); }
1.59      parser    187: #ifdef XML
1.57      parser    188:        const char *transcode_cstr(const XalanDOMString& s) { return fpool->transcode_cstr(s); }
                    189:        String& transcode(const XalanDOMString& s) { return fpool->transcode(s); }
1.55      parser    190:        void _throw(const String *source, const XSLException& e) { fpool->_throw(source, e); }
1.59      parser    191: #endif
1.57      parser    192:        //}
1.23      paf       193: };
1.34      paf       194: /// useful macro for creating objects on current Pooled object Pooled::pool()
1.23      paf       195: #define NEW new(pool())
                    196: 
1.35      paf       197: /** 
1.34      paf       198:        Auto-object used for temporary changing Pool's exception().
                    199: 
                    200:        Use by with these macros:
1.38      paf       201:        @code
1.34      paf       202:                TRY { 
1.38      paf       203:                        // ... 
1.34      paf       204:                        if(?) 
                    205:                                THROW(?); 
1.38      paf       206:                        // ...
1.34      paf       207:                } CATCH(e) { 
1.38      paf       208:                        // code, using e fields
                    209:                        // e.comment() 
1.34      paf       210:                }
                    211:                END_CATCH
1.38      paf       212:        @endcode
                    213: 
                    214:        @see TRY, THROW
1.34      paf       215: */
1.25      paf       216: class Temp_exception {
1.29      paf       217:        Pool& fpool;
1.23      paf       218:        Exception *saved_exception;
                    219: public:
1.25      paf       220:        Temp_exception(Pool& apool, Exception& exception) : 
1.29      paf       221:                fpool(apool),
1.23      paf       222:                saved_exception(apool.set_exception(&exception)) {
                    223:        }
1.25      paf       224:        ~Temp_exception() { 
1.29      paf       225:                fpool.restore_exception(saved_exception); 
1.23      paf       226:        }
1.1       paf       227: };
1.23      paf       228: 
1.33      paf       229: #define XTRY(pool) \
1.24      paf       230:        { \
                    231:                Exception temp_exception; \
1.33      paf       232:                Temp_exception le(pool, temp_exception); \
1.24      paf       233:                if(setjmp(temp_exception.mark)==0)
1.31      paf       234: 
1.33      paf       235: #define XTHROW(exception) exception._throw
                    236: #define XCATCH(e) \
1.24      paf       237:                else{ \
                    238:                        Exception& e=temp_exception;
1.31      paf       239: 
1.33      paf       240: #define XEND_CATCH \
1.24      paf       241:                } \
                    242:        }
1.33      paf       243: 
1.34      paf       244: //@{
                    245: /// @see Temp_exception 
1.33      paf       246: #define TRY XTRY(pool())
                    247: #define THROW XTHROW(exception())
                    248: #define CATCH(e) XCATCH(e)
                    249: #define END_CATCH XEND_CATCH 
                    250: 
                    251: #define PTRY XTRY(pool)
                    252: #define PTHROW XTHROW(pool.exception())
                    253: #define PCATCH(e) XCATCH(e)
                    254: #define PEND_CATCH XEND_CATCH 
1.34      paf       255: //@}
1.1       paf       256: 
                    257: #endif

E-mail: