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

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.59    ! parser      8:        $Id: pa_pool.h,v 1.58 2001/09/21 08:41:28 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.39      paf        69: private:
                     70: 
                     71:        void *fstorage;
1.40      paf        72:        void *fcontext;
1.47      paf        73:        void *ftag;
1.8       paf        74: 
1.50      parser     75: private: 
                     76:        
                     77:        //{
                     78:        /// @name implementation defined
1.51      parser     79:     void *real_malloc(size_t size/*, int place*/);
1.20      paf        80:     void *real_calloc(size_t size);
1.54      parser     81:        bool real_register_cleanup(void (*cleanup) (void *), void *data);
1.50      parser     82:        //}
1.17      paf        83: 
1.23      paf        84: private: 
1.17      paf        85: 
1.34      paf        86:        /// checks whether mem allocated OK. throws exception otherwise
1.23      paf        87:        void *check(void *ptr, size_t size) {
                     88:                if(ptr)
                     89:                        return ptr;
                     90: 
1.54      parser     91:                fail_alloc(size);
1.23      paf        92: 
                     93:                // never reached
                     94:                return 0;
                     95:        }
1.54      parser     96:        /// throws allocation exception
                     97:        void fail_alloc(size_t size) const;
                     98: 
                     99:        /// throws register cleanup exception
                    100:        void fail_register_cleanup() const;
1.23      paf       101: 
1.34      paf       102: private: // exception handling
1.17      paf       103: 
1.45      paf       104:        // exception replacement mechanism is 'private'zed from direct usage
1.34      paf       105:        // Temp_exception object enforces paired set/restore
1.23      paf       106:        Exception *set_exception(Exception *e){
1.31      paf       107:                Exception *r=fexception;
                    108:                fexception=e;
1.23      paf       109:                return r;
                    110:        }
                    111:        void restore_exception(Exception *e) {
1.31      paf       112:                fexception=e;
1.23      paf       113:        }
                    114: 
                    115: private:
                    116: 
                    117:        // current request's exception object
1.31      paf       118:        Exception *fexception;
1.17      paf       119: 
1.59    ! parser    120: #ifdef XML
        !           121: 
        !           122: public:
        !           123:        /// resets transcoder if they change charset 
        !           124:        void set_charset(const String &charset);
        !           125:        /// returns current charset
        !           126:        const String& get_charset() { return *charset; }
        !           127:        /// converts Xalan string to char *
        !           128:        const char *transcode_cstr(const XalanDOMString& s);
        !           129:        /// converts Xalan string to parser String
        !           130:        String& transcode(const XalanDOMString& s);
        !           131:        /// converts XSL exception to parser exception
        !           132:        void _throw(const String *source, const XSLException& e);
        !           133: 
        !           134: private:
        !           135: 
        !           136:        void set_charset(const char *new_scharset);
        !           137:        void update_transcoder();
        !           138: 
        !           139: private:
        !           140: 
        !           141:        const String *charset;
        !           142:        XMLTranscoder *transcoder;
        !           143: 
        !           144: #endif
        !           145: 
1.8       paf       146: private: //disabled
                    147: 
1.18      paf       148:        // Pool(const Pool&) {}
1.16      paf       149:        Pool& operator = (const Pool&) { return *this; }
1.21      paf       150: };
                    151: 
1.35      paf       152: /** 
1.34      paf       153:        Base for all classes that are allocated in 'pools'.
                    154: 
                    155:        Holds Pool object. Contains useful wrappers to it's methods.
1.38      paf       156: 
                    157:        @see NEW, Temp_exception
1.34      paf       158: */
1.21      paf       159: class Pooled {
1.23      paf       160:        // the pool i'm allocated on
1.49      paf       161:        Pool *fpool;
1.21      paf       162: public:
1.37      paf       163: 
                    164:        /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21      paf       165:        static void *operator new(size_t size, Pool& apool) { 
1.51      parser    166:                return apool.malloc(size/*, 1*/);
1.21      paf       167:        }
                    168: 
1.49      paf       169:        Pooled(Pool& apool) : fpool(&apool) {}
1.23      paf       170: 
1.34      paf       171:        /// my pool
1.49      paf       172:        Pool& pool() const { return *fpool; }
                    173: 
                    174:        /** used for moving objects from one pool to another. 
                    175:                in between object can have no pool and can not be used
                    176:                @see SQL_Driver_manager
                    177:        */
                    178:        void set_pool(Pool *apool) { fpool=apool; }
1.21      paf       179: 
1.57      parser    180:        //{
                    181:        /// @name useful wrapper around pool
1.49      paf       182:        void *malloc(size_t size) const { return fpool->malloc(size); }
                    183:        void *calloc(size_t size) const { return fpool->calloc(size); }
1.57      parser    184:        void register_cleanup(void (*cleanup) (void *), void *data) { fpool->register_cleanup(cleanup, data); }
1.49      paf       185:        Exception& exception() const { return fpool->exception(); }
1.59    ! parser    186: #ifdef XML
1.57      parser    187:        const char *transcode_cstr(const XalanDOMString& s) { return fpool->transcode_cstr(s); }
                    188:        String& transcode(const XalanDOMString& s) { return fpool->transcode(s); }
1.55      parser    189:        void _throw(const String *source, const XSLException& e) { fpool->_throw(source, e); }
1.59    ! parser    190: #endif
1.57      parser    191:        //}
1.23      paf       192: };
1.34      paf       193: /// useful macro for creating objects on current Pooled object Pooled::pool()
1.23      paf       194: #define NEW new(pool())
                    195: 
1.35      paf       196: /** 
1.34      paf       197:        Auto-object used for temporary changing Pool's exception().
                    198: 
                    199:        Use by with these macros:
1.38      paf       200:        @code
1.34      paf       201:                TRY { 
1.38      paf       202:                        // ... 
1.34      paf       203:                        if(?) 
                    204:                                THROW(?); 
1.38      paf       205:                        // ...
1.34      paf       206:                } CATCH(e) { 
1.38      paf       207:                        // code, using e fields
                    208:                        // e.comment() 
1.34      paf       209:                }
                    210:                END_CATCH
1.38      paf       211:        @endcode
                    212: 
                    213:        @see TRY, THROW
1.34      paf       214: */
1.25      paf       215: class Temp_exception {
1.29      paf       216:        Pool& fpool;
1.23      paf       217:        Exception *saved_exception;
                    218: public:
1.25      paf       219:        Temp_exception(Pool& apool, Exception& exception) : 
1.29      paf       220:                fpool(apool),
1.23      paf       221:                saved_exception(apool.set_exception(&exception)) {
                    222:        }
1.25      paf       223:        ~Temp_exception() { 
1.29      paf       224:                fpool.restore_exception(saved_exception); 
1.23      paf       225:        }
1.1       paf       226: };
1.23      paf       227: 
1.33      paf       228: #define XTRY(pool) \
1.24      paf       229:        { \
                    230:                Exception temp_exception; \
1.33      paf       231:                Temp_exception le(pool, temp_exception); \
1.24      paf       232:                if(setjmp(temp_exception.mark)==0)
1.31      paf       233: 
1.33      paf       234: #define XTHROW(exception) exception._throw
                    235: #define XCATCH(e) \
1.24      paf       236:                else{ \
                    237:                        Exception& e=temp_exception;
1.31      paf       238: 
1.33      paf       239: #define XEND_CATCH \
1.24      paf       240:                } \
                    241:        }
1.33      paf       242: 
1.34      paf       243: //@{
                    244: /// @see Temp_exception 
1.33      paf       245: #define TRY XTRY(pool())
                    246: #define THROW XTHROW(exception())
                    247: #define CATCH(e) XCATCH(e)
                    248: #define END_CATCH XEND_CATCH 
                    249: 
                    250: #define PTRY XTRY(pool)
                    251: #define PTHROW XTHROW(pool.exception())
                    252: #define PCATCH(e) XCATCH(e)
                    253: #define PEND_CATCH XEND_CATCH 
1.34      paf       254: //@}
1.1       paf       255: 
                    256: #endif

E-mail: