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

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.63    ! parser      8:        $Id: pa_pool.h,v 1.62 2001/10/11 14:57:53 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>
1.59      parser     19: #endif
1.55      parser     20: 
                     21: // forwards
                     22: 
1.21      paf        23: class Exception;
1.25      paf        24: class Temp_exception;
1.55      parser     25: class String;
                     26: 
1.35      paf        27: /** 
1.34      paf        28:        Pool mechanizm allows users not to free up allocated memory,
                     29:        leaving that problem to 'pools'.
                     30: 
                     31:        Also holds Exception object, which can be temporary set using 
                     32:        Temp_exception auto-object.
1.38      paf        33: 
                     34:        @see Pooled
1.34      paf        35: */
1.47      paf        36: 
1.1       paf        37: class Pool {
1.25      paf        38:        friend Temp_exception;
1.1       paf        39: public:
1.18      paf        40: 
1.55      parser     41:        Pool(void *astorage);
1.56      parser     42:        ~Pool();
1.18      paf        43: 
1.40      paf        44:        void set_context(void *acontext) { fcontext=acontext; }
                     45:        void *context() { return fcontext; }
1.39      paf        46: 
1.47      paf        47:        void set_tag(void *atag) { ftag=atag; }
                     48:        void *tag() { return ftag; }
1.44      paf        49: 
1.34      paf        50:        /// allocates some bytes on pool
1.51      parser     51:        void *malloc(size_t size/*, int place=0*/) {
                     52:                return check(real_malloc(size/*, place*/), size);
1.18      paf        53:        }
1.34      paf        54:        /// allocates some bytes clearing them with zeros
1.18      paf        55:        void *calloc(size_t size) {
                     56:                return check(real_calloc(size), size);
                     57:        }
1.39      paf        58: 
1.53      parser     59:        /// registers a routine to clean up non-pooled allocations
1.54      parser     60:        void register_cleanup(void (*cleanup) (void *), void *data) {
                     61:                if(!real_register_cleanup(cleanup, data))
                     62:                        fail_register_cleanup();
                     63:        }
1.53      parser     64: 
                     65:        /// current exception object of the pool
                     66:        Exception& exception() const { return *fexception; }
                     67: 
1.60      parser     68:        /// resets transcoder if they change charset 
                     69:        void set_charset(const String &charset);
                     70:        /// returns current charset
                     71:        const String& get_charset() { return *charset; }
                     72: 
1.39      paf        73: private:
                     74: 
                     75:        void *fstorage;
1.40      paf        76:        void *fcontext;
1.47      paf        77:        void *ftag;
1.60      parser     78:        const String *charset;
1.8       paf        79: 
1.50      parser     80: private: 
                     81:        
                     82:        //{
                     83:        /// @name implementation defined
1.51      parser     84:     void *real_malloc(size_t size/*, int place*/);
1.20      paf        85:     void *real_calloc(size_t size);
1.54      parser     86:        bool real_register_cleanup(void (*cleanup) (void *), void *data);
1.50      parser     87:        //}
1.17      paf        88: 
1.23      paf        89: private: 
1.17      paf        90: 
1.34      paf        91:        /// checks whether mem allocated OK. throws exception otherwise
1.23      paf        92:        void *check(void *ptr, size_t size) {
                     93:                if(ptr)
                     94:                        return ptr;
                     95: 
1.54      parser     96:                fail_alloc(size);
1.23      paf        97: 
                     98:                // never reached
                     99:                return 0;
                    100:        }
1.54      parser    101:        /// throws allocation exception
                    102:        void fail_alloc(size_t size) const;
                    103: 
                    104:        /// throws register cleanup exception
                    105:        void fail_register_cleanup() const;
1.23      paf       106: 
1.34      paf       107: private: // exception handling
1.17      paf       108: 
1.45      paf       109:        // exception replacement mechanism is 'private'zed from direct usage
1.34      paf       110:        // Temp_exception object enforces paired set/restore
1.23      paf       111:        Exception *set_exception(Exception *e){
1.31      paf       112:                Exception *r=fexception;
                    113:                fexception=e;
1.23      paf       114:                return r;
                    115:        }
                    116:        void restore_exception(Exception *e) {
1.31      paf       117:                fexception=e;
1.23      paf       118:        }
                    119: 
                    120: private:
                    121: 
                    122:        // current request's exception object
1.31      paf       123:        Exception *fexception;
1.17      paf       124: 
1.59      parser    125: #ifdef XML
                    126: 
                    127: public:
                    128:        /// converts Xalan string to char *
                    129:        const char *transcode_cstr(const XalanDOMString& s);
                    130:        /// converts Xalan string to parser String
                    131:        String& transcode(const XalanDOMString& s);
1.63    ! parser    132:        /// converts char * to Xalan string
        !           133:        XalanDOMString& Pool::transcode_buf(const char *buf, size_t buf_size);
        !           134:        /// converts parser String to Xalan string
        !           135:        XalanDOMString& Pool::transcode(const String& s);
1.59      parser    136: 
                    137: private:
                    138: 
                    139:        void set_charset(const char *new_scharset);
                    140:        void update_transcoder();
                    141: 
                    142: private:
                    143: 
                    144:        XMLTranscoder *transcoder;
                    145: 
                    146: #endif
                    147: 
1.8       paf       148: private: //disabled
                    149: 
1.18      paf       150:        // Pool(const Pool&) {}
1.16      paf       151:        Pool& operator = (const Pool&) { return *this; }
1.21      paf       152: };
                    153: 
1.35      paf       154: /** 
1.34      paf       155:        Base for all classes that are allocated in 'pools'.
                    156: 
                    157:        Holds Pool object. Contains useful wrappers to it's methods.
1.38      paf       158: 
                    159:        @see NEW, Temp_exception
1.34      paf       160: */
1.21      paf       161: class Pooled {
1.23      paf       162:        // the pool i'm allocated on
1.49      paf       163:        Pool *fpool;
1.21      paf       164: public:
1.37      paf       165: 
                    166:        /// the Pooled-sole: Pooled instances can be allocated in Pool rather then on heap
1.21      paf       167:        static void *operator new(size_t size, Pool& apool) { 
1.51      parser    168:                return apool.malloc(size/*, 1*/);
1.21      paf       169:        }
                    170: 
1.49      paf       171:        Pooled(Pool& apool) : fpool(&apool) {}
1.23      paf       172: 
1.34      paf       173:        /// my pool
1.49      paf       174:        Pool& pool() const { return *fpool; }
                    175: 
                    176:        /** used for moving objects from one pool to another. 
                    177:                in between object can have no pool and can not be used
                    178:                @see SQL_Driver_manager
                    179:        */
                    180:        void set_pool(Pool *apool) { fpool=apool; }
1.21      paf       181: 
1.57      parser    182:        //{
                    183:        /// @name useful wrapper around pool
1.49      paf       184:        void *malloc(size_t size) const { return fpool->malloc(size); }
                    185:        void *calloc(size_t size) const { return fpool->calloc(size); }
1.57      parser    186:        void register_cleanup(void (*cleanup) (void *), void *data) { fpool->register_cleanup(cleanup, data); }
1.49      paf       187:        Exception& exception() const { return fpool->exception(); }
1.59      parser    188: #ifdef XML
1.57      parser    189:        const char *transcode_cstr(const XalanDOMString& s) { return fpool->transcode_cstr(s); }
                    190:        String& transcode(const XalanDOMString& s) { return fpool->transcode(s); }
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: