source: NonGTP/Xerces/xercesc/internal/XSerializeEngine.hpp @ 188

Revision 188, 22.9 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2003 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/*
58 * $Log: XSerializeEngine.hpp,v $
59 * Revision 1.14  2004/02/11 20:38:50  peiyongz
60 * Fix to bug#26864, thanks to David Bertoni.
61 *
62 * Revision 1.13  2004/01/29 11:46:30  cargilld
63 * Code cleanup changes to get rid of various compiler diagnostic messages.
64 *
65 * Revision 1.12  2004/01/16 21:55:18  peiyongz
66 * maintain the same size on both 32/64 bit architecture
67 *
68 * Revision 1.11  2004/01/15 23:42:32  peiyongz
69 * proper allignment for built-in datatype read/write
70 *
71 * Revision 1.10  2003/12/17 00:18:34  cargilld
72 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
73 *
74 * Revision 1.9  2003/11/25 20:37:40  jberry
75 * Cleanup build errors/warnings from CodeWarrior
76 *
77 * Revision 1.8  2003/11/11 22:48:13  knoaman
78 * Serialization of XSAnnotation.
79 *
80 * Revision 1.7  2003/10/17 21:09:03  peiyongz
81 * renaming methods
82 *
83 * Revision 1.6  2003/10/14 15:18:20  peiyongz
84 * getMemoryManager()
85 *
86 * Revision 1.5  2003/10/09 19:11:53  peiyongz
87 * Fix to linkage error on Solaris
88 *
89 * Revision 1.4  2003/10/07 19:38:31  peiyongz
90 * API for Template_Class Object Serialization/Deserialization
91 *
92 * Revision 1.3  2003/09/25 22:22:00  peiyongz
93 * Introduction of readString/writeString
94 *
95 * Revision 1.2  2003/09/19 04:29:11  neilg
96 * fix compilation problems under GCC
97 *
98 * Revision 1.1  2003/09/18 18:31:24  peiyongz
99 * OSU: Object Serialization Utilities
100 *
101 * $Id: XSerializeEngine.hpp,v 1.14 2004/02/11 20:38:50 peiyongz Exp $
102 *
103 */
104
105#if !defined(XSERIALIZE_ENGINE_HPP)
106#define XSERIALIZE_ENGINE_HPP
107
108#include <xercesc/framework/BinOutputStream.hpp>
109#include <xercesc/util/BinInputStream.hpp>
110#include <xercesc/util/RefHashTableOf.hpp>
111#include <xercesc/util/ValueVectorOf.hpp>
112#include <xercesc/internal/XSerializationException.hpp>
113#include <xercesc/util/XMLExceptMsgs.hpp>
114
115XERCES_CPP_NAMESPACE_BEGIN
116
117class XSerializable;
118class XProtoType;
119class MemoryManager;
120class XSerializedObjectId;
121
122class XMLUTIL_EXPORT XSerializeEngine
123{
124public:
125
126    enum { mode_Store
127         , mode_Load
128    };
129   
130
131    static const bool toReadBufferLen;
132    static int defaultBufferLen;
133    static int defaultDataLen;
134
135
136    typedef unsigned int   XSerializedObjectId_t;
137
138    /***
139      *
140      *  Destructor
141      *
142      ***/
143    ~XSerializeEngine();
144
145    /***
146      *
147      *  Constructor for de-serialization(loading)
148      *
149      *  Application needs to make sure that the instance of
150      *  BinInputStream, persists beyond the life of this
151      *  SerializeEngine.
152      *
153      *  Param
154      *     inStream         input stream
155      *     manager          MemoryManager
156      *     bufSize          the size of the internal buffer
157      *
158      ***/
159    XSerializeEngine(BinInputStream*         inStream
160                   , MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
161                   , unsigned long           bufSize = 8192 );
162
163    /***
164      *
165      *  Constructor for serialization(storing)
166      *
167      *  Application needs to make sure that the instance of
168      *  BinOutputStream, persists beyond the life of this
169      *  SerializeEngine.
170      *
171      *  Param
172      *     outStream        output stream
173      *     manager          MemoryManager
174      *     bufSize          the size of the internal buffer
175      *
176      ***/
177    XSerializeEngine(BinOutputStream*        outStream
178                   , MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
179                   , unsigned long           bufSize = 8192 );
180
181    /***
182      *
183      *  When serialization, flush out the internal buffer
184      *
185      *  Return:
186      *
187      ***/
188    void flush();
189
190    /***
191      *
192      *  Checking if the serialize engine is doing serialization(storing)
193      *
194      *  Return: true, if it is
195      *          false, otherwise
196      *
197      ***/
198    inline bool isStoring() const;
199
200    /***
201      *
202      *  Checking if the serialize engine is doing de-serialization(loading)
203      *
204      *  Return: true, if it is
205      *          false, otherwise
206      *
207      ***/
208    inline bool isLoading() const;
209
210    /***
211      *
212      *  Get the embeded Memory Manager
213      *
214      *  Return: MemoryManager
215      *
216      ***/
217    inline MemoryManager* getMemoryManager() const;
218
219    /***
220      *
221      *  Write object to the internal buffer.
222      *
223      *  Param
224      *     objectToWrite:    the object to be serialized
225      *
226      *  Return:
227      *
228      ***/
229           void           write(XSerializable* const objectToWrite);
230
231    /***
232      *
233      *  Write prototype info to the internal buffer.
234      *
235      *  Param
236      *     protoType:    instance of prototype
237      *
238      *  Return:
239      *
240      ***/
241           void           write(XProtoType* const protoType);
242
243    /***
244      *
245      *  Write a stream of XMLByte to the internal buffer.
246      *
247      *  Param
248      *     toWrite:   the stream of XMLByte to write
249      *     writeLen:  the length of the stream
250      *
251      *  Return:
252      *
253      ***/
254           void           write(const XMLByte* const toWrite
255                               ,      int            writeLen);
256
257    /***
258      *
259      *  Write a stream of XMLCh to the internal buffer.
260      *
261      *  Param
262      *     toWrite:   the stream of XMLCh to write
263      *     writeLen:  the length of the stream
264      *
265      *  Return:
266      *
267      ***/
268           void           write(const XMLCh* const toWrite
269                               ,      int          writeLen);
270
271    /***
272      *
273      *  Write a stream of XMLCh to the internal buffer.
274      *
275      *  Write the bufferLen first if requested, then the length
276      *  of the stream followed by the stream.
277      *
278      *  Param
279      *     toWrite:        the stream of XMLCh to write
280      *     bufferLen:      the maximum size of the buffer
281      *     toWriteBufLen:  specify if the bufferLen need to be written or not
282      *
283      *  Return:
284      *
285      ***/
286           void           writeString(const XMLCh* const toWrite
287                                    , const int          bufferLen = 0
288                                    , bool               toWriteBufLen = false);
289
290    /***
291      *
292      *  Write a stream of XMLByte to the internal buffer.
293      *
294      *  Write the bufferLen first if requested, then the length
295      *  of the stream followed by the stream.
296      *
297      *  Param
298      *     toWrite:        the stream of XMLByte to write
299      *     bufferLen:      the maximum size of the buffer
300      *     toWriteBufLen:  specify if the bufferLen need to be written or not
301      *
302      *  Return:
303      *
304      ***/
305           void           writeString(const XMLByte* const toWrite
306                                    , const int            bufferLen = 0
307                                    , bool                 toWriteBufLen = false);
308
309    static const bool toWriteBufferLen;
310
311    /***
312      *
313      *  Read/Create object from the internal buffer.
314      *
315      *  Param
316      *     protoType:    an instance of prototype of the object anticipated
317      *
318      *  Return:          to object read/created
319      *
320      ***/
321               XSerializable* read(XProtoType* const protoType);
322
323    /***
324      *
325      *  Read prototype object from the internal buffer.
326      *  Verify if the same prototype object found in buffer.
327      *
328      *  Param
329      *     protoType:    an instance of prototype of the object anticipated
330      *     objTag:       the object Tag to an existing object
331      *
332      *  Return:          true  : if matching found
333      *                   false : otherwise
334      *
335      ***/
336           bool           read(XProtoType* const    protoType
337                                     , XSerializedObjectId_t*       objTag);
338
339    /***
340      *
341      *  Read XMLByte stream from the internal buffer.
342      *
343      *  Param
344      *     toRead:   the buffer to hold the XMLByte stream
345      *     readLen:  the length of the XMLByte to read in
346      *
347      *  Return:
348      *
349      ***/
350           void           read(XMLByte* const toRead
351                             , int            readLen);
352
353    /***
354      *
355      *  Read XMLCh stream from the internal buffer.
356      *
357      *  Param
358      *     toRead:   the buffer to hold the XMLCh stream
359      *     readLen:  the length of the XMLCh to read in
360      *
361      *  Return:
362      *
363      ***/
364           void           read(XMLCh* const toRead
365                             , int          readLen);
366
367    /***
368      *
369      *  Read a stream of XMLCh from the internal buffer.
370      *
371      *  Read the bufferLen first if requested, then the length
372      *  of the stream followed by the stream.
373      *
374      *  Param
375      *     toRead:       the pointer to the buffer to hold the XMLCh stream
376      *     bufferLen:    the size of the buffer created
377      *     dataLen:       the length of the stream
378      *     toReadBufLen: specify if the bufferLen need to be read or not
379      *
380      *  Return:
381      *
382      ***/
383           void           readString(XMLCh*&        toRead
384                                   , int&           bufferLen    = defaultBufferLen
385                                   , int&           dataLen      = defaultDataLen
386                                   , bool           toReadBufLen = false);
387
388    /***
389      *
390      *  Read a stream of XMLByte from the internal buffer.
391      *
392      *  Read the bufferLen first if requested, then the length
393      *  of the stream followed by the stream.
394      *
395      *  Param
396      *     toRead:       the pointer to the buffer to hold the XMLByte stream
397      *     bufferLen:    the size of the buffer created
398      *     dataLen:       the length of the stream
399      *     toReadBufLen: specify if the bufferLen need to be read or not
400      *
401      *  Return:
402      *
403      ***/
404           void           readString(XMLByte*&      toRead
405                                   , int&           bufferLen    = defaultBufferLen
406                                   , int&           dataLen      = defaultDataLen
407                                   , bool           toReadBufLen = false);
408
409
410    /***
411      *
412      *  Check if the template object has been stored or not
413      *
414      *  Param
415      *    objectPtr:     the template object pointer
416      *
417      *  Return:          true  : the object has NOT been stored yet
418      *                   false : otherwise
419      *
420      ***/
421           bool           needToStoreObject(void* const templateObjectToWrite);
422
423    /***
424      *
425      *  Check if the template object has been loaded or not
426      *
427      *  Param
428      *    objectPtr:     the address of the template object pointer
429      *
430      *  Return:          true  : the object has NOT been loaded yet
431      *                   false : otherwise
432      *
433      ***/
434           bool           needToLoadObject(void**       templateObjectToRead);
435
436    /***
437      *
438      *  In the case of needToLoadObject() return true, the client
439      *  application needs to instantiate an expected template object, and
440      *  register the address to the engine.
441      *
442      *  Param
443      *    objectPtr:     the template object pointer newly instantiated
444      *
445      *  Return: 
446      *
447      ***/
448           void           registerObject(void* const templateObjectToRegister);
449
450    /***
451      *
452      *  Insertion operator for serializable classes
453      *
454      ***/
455
456        friend XSerializeEngine& operator<<(XSerializeEngine&
457                                      , XSerializable* const );
458
459    /***
460      *
461      *  Insertion operators for
462      *     . basic Xerces data types
463      *     . built-in types
464      *
465      ***/
466           XSerializeEngine& operator<<(XMLByte);
467           XSerializeEngine& operator<<(XMLCh);
468
469           XSerializeEngine& operator<<(char);
470           XSerializeEngine& operator<<(short);
471           XSerializeEngine& operator<<(int);
472           XSerializeEngine& operator<<(unsigned int);
473           XSerializeEngine& operator<<(long);
474           XSerializeEngine& operator<<(unsigned long);
475           XSerializeEngine& operator<<(float);
476           XSerializeEngine& operator<<(double);
477           XSerializeEngine& operator<<(bool);
478
479    /***
480      *
481      *  Extraction operators for
482      *     . basic Xerces data types
483      *     . built-in types
484      *
485      ***/
486           XSerializeEngine& operator>>(XMLByte&);
487           XSerializeEngine& operator>>(XMLCh&);
488
489           XSerializeEngine& operator>>(char&);
490           XSerializeEngine& operator>>(short&);
491           XSerializeEngine& operator>>(int&);
492           XSerializeEngine& operator>>(unsigned int&);
493           XSerializeEngine& operator>>(long&);
494           XSerializeEngine& operator>>(unsigned long&);
495           XSerializeEngine& operator>>(float&);
496           XSerializeEngine& operator>>(double&);
497           XSerializeEngine& operator>>(bool&);
498
499private:
500    // -----------------------------------------------------------------------
501    //  Unimplemented constructors and operators
502    // -----------------------------------------------------------------------
503        XSerializeEngine();
504    XSerializeEngine(const XSerializeEngine&);
505        XSerializeEngine& operator=(const XSerializeEngine&);
506
507    /***
508      *
509      *   Store Pool Opertions
510      *
511      ***/
512           XSerializedObjectId_t  lookupStorePool(void* const objectPtr) const;
513           void                   addStorePool(void* const objectPtr);
514
515    /***
516      *
517      *   Load Pool Opertions
518      *
519      ***/
520           XSerializable* lookupLoadPool(XSerializedObjectId_t objectTag) const;
521           void           addLoadPool(void* const objectPtr);
522   
523    /***
524      *   
525      *    Intenal Buffer Operations
526      *
527      ***/
528    inline void           checkAndFillBuffer(int bytesNeedToRead);
529
530    inline void           checkAndFlushBuffer(int bytesNeedToWrite);
531
532           void           fillBuffer(int bytesNeedToRead);
533
534           void           flushBuffer();
535
536           void           pumpCount();
537
538    /***
539      *   
540      *    Helper
541      *
542      ***/
543    inline void            ensureStoring()                          const;
544
545    inline void            ensureLoading()                          const;
546
547    inline void            ensureStoreBuffer()                      const;
548
549    inline void            ensureLoadBuffer()                       const;
550
551    inline void            ensurePointer(void* const)               const;
552
553    inline void            ensureBufferLen(int  bufferLen)          const;
554
555    inline void            Assert(bool  toEval
556                                , const XMLExcepts::Codes toThrow)  const;
557
558    inline size_t          allignAdjust()                           const;
559
560    inline void            allignBufCur();
561
562    // Make XTemplateSerializer friend of XSerializeEngine so that
563    // we can call lookupStorePool and lookupLoadPool in the case of
564    // annotations.
565    friend class XTemplateSerializer;
566
567    // -------------------------------------------------------------------------------
568    //  data
569    //
570    //  fStoreLoad:
571    //               Indicator: storing(serialization) or loading(de-serialization)
572    //
573    //  fMemoryManager:
574    //               used to allocate memory for internal consumption and/or
575    //               allocate memory for object created in de-serialization.
576    //
577    //  fInputStream:
578    //               Binary stream to read from (de-serialization), provided
579    //               by client application, not owned.
580    //
581    //  fOutputStream:
582    //               Binary stream to write to (serialization), provided
583    //               by client application, not owned.
584    //
585    //  fBufSize:
586    //               The size of the internal buffer
587    //
588    //  fBufStart/fBufEnd:
589    //               
590    //               The internal buffer.
591    //  fBufEnd:
592    //               one beyond the last valid cell
593    //               fBufEnd === (fBufStart + fBufSize)
594    //
595    //  fBufCur:
596    //               The cursor of the buffer
597    //
598    //  fBufLoadMax:
599    //               Indicating the end of the valid content in the buffer
600    //
601    //  fStorePool:
602    //                Object collection for storing
603    //
604    //  fLoadPool:
605    //                Object collection for loading
606    //
607    //  fMapCount:
608    // -------------------------------------------------------------------------------
609    const short                            fStoreLoad;   
610    MemoryManager*   const                 fMemoryManager;
611    BinInputStream*  const                 fInputStream;
612    BinOutputStream* const                 fOutputStream;
613
614    //buffer
615    const unsigned long                    fBufSize;
616        XMLByte* const                         fBufStart;
617        XMLByte* const                         fBufEnd;
618    XMLByte*                               fBufCur;
619    XMLByte*                               fBufLoadMax;
620
621    /***
622     *   Map for storing object
623     *
624     *   key:   XSerializable*
625     *          XProtoType*
626     *
627     *   value: XMLInteger*, owned
628     *
629     ***/
630    RefHashTableOf<XSerializedObjectId>*   fStorePool; 
631
632    /***
633     *   Vector for loading object, objects are NOT owned
634     *
635     *   data:   XSerializable*
636     *           XProtoType*
637     *
638     ***/
639    ValueVectorOf<void*>*                  fLoadPool;   
640
641    /***
642     *   object counter
643     ***/
644        XSerializedObjectId_t                  fObjectCount;
645};
646
647inline bool XSerializeEngine::isStoring() const
648{
649    return (fStoreLoad == mode_Store);
650}
651
652inline bool XSerializeEngine::isLoading() const
653{
654    return (fStoreLoad == mode_Load);
655}
656
657inline MemoryManager* XSerializeEngine::getMemoryManager() const
658{
659    return fMemoryManager;
660}
661
662inline XSerializeEngine& operator<<(XSerializeEngine&       serEng
663                                  , XSerializable* const    serObj)
664{
665        serEng.write(serObj);
666    return serEng;
667}
668
669inline void XSerializeEngine::ensureStoring() const
670{
671        Assert(isStoring(), XMLExcepts::XSer_Storing_Violation);
672}
673
674inline void XSerializeEngine::ensureLoading() const
675{
676        Assert(isLoading(), XMLExcepts::XSer_Loading_Violation);
677}
678
679
680
681inline void XSerializeEngine::Assert(bool toEval
682                                   , const XMLExcepts::Codes toThrow) const
683{
684    if (!toEval)
685    {
686        ThrowXMLwithMemMgr(XSerializationException, toThrow, fMemoryManager); 
687    }
688
689}
690
691// For the following built-in datatype, we assume
692// the same allignment requirement
693//
694// short    unsigned short
695// int      unsigned long
696// long     unsigned long
697// float
698// double
699//
700// Based on the current position (fBufCur), calculated the needed size
701// to read/write
702//
703inline size_t XSerializeEngine::allignAdjust() const
704{
705        #ifdef XML_PLATFORM_NEW_BLOCK_ALIGNMENT
706                size_t alignment = XML_PLATFORM_NEW_BLOCK_ALIGNMENT;
707        #else
708                size_t alignment = (sizeof(void*) >= sizeof(double)) ? sizeof(void*) : sizeof(double);
709        #endif
710       
711        size_t remainder = (size_t) fBufCur % alignment;       
712        return (remainder == 0) ? 0 : (alignment - remainder);
713}
714
715// Adjust the fBufCur
716inline void XSerializeEngine::allignBufCur()
717{
718    fBufCur+=allignAdjust();
719}
720
721/***
722 *  Ought to be nested class
723 ***/
724class XSerializedObjectId : public XMemory
725{
726public:
727
728    ~XSerializedObjectId(){};
729
730private:
731
732    inline XSerializedObjectId(XSerializeEngine::XSerializedObjectId_t val):
733        fData(val) { };
734
735    inline XSerializeEngine::XSerializedObjectId_t getValue() const {return fData; };
736
737    friend class XSerializeEngine;
738
739private:
740    // -----------------------------------------------------------------------
741    //  Unimplemented constructors and operators
742    // -----------------------------------------------------------------------
743        XSerializedObjectId();
744    XSerializedObjectId(const XSerializedObjectId&);
745        XSerializedObjectId& operator=(const XSerializedObjectId&);
746
747    XSerializeEngine::XSerializedObjectId_t    fData;
748
749};
750
751
752XERCES_CPP_NAMESPACE_END
753
754#endif
Note: See TracBrowser for help on using the repository browser.