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