LCOV - code coverage report
Current view: top level - include/tools - pstm.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 3 4 75.0 %
Date: 2015-06-13 12:38:46 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : #ifndef INCLUDED_TOOLS_PSTM_HXX
      20             : #define INCLUDED_TOOLS_PSTM_HXX
      21             : 
      22             : #include <tools/toolsdllapi.h>
      23             : #include <tools/unqidx.hxx>
      24             : #include <tools/ref.hxx>
      25             : #include <tools/rtti.hxx>
      26             : #include <tools/stream.hxx>
      27             : #include <map>
      28             : #include <unordered_map>
      29             : 
      30             : #define ERRCODE_IO_NOFACTORY ERRCODE_IO_WRONGFORMAT
      31             : 
      32             : class SvPersistBase;
      33             : 
      34             : typedef void * (*SvCreateInstancePersist)( SvPersistBase ** );
      35             : 
      36             : #define SV_CLASS_REGISTER( Class )                          \
      37             :     Register( Class::StaticClassId(), Class::CreateInstance )
      38             : 
      39         114 : class TOOLS_DLLPUBLIC SvClassManager
      40             : {
      41             :     typedef std::unordered_map<sal_Int32, SvCreateInstancePersist> Map;
      42             :     Map aAssocTable;
      43             : 
      44             : public:
      45             :     void Register( sal_Int32 nClassId, SvCreateInstancePersist pFunc );
      46             :     SvCreateInstancePersist Get( sal_Int32 nClassId );
      47             : };
      48             : 
      49      173455 : class TOOLS_DLLPUBLIC SvRttiBase : public SvRefBase
      50             : {
      51             : public:
      52             :             TYPEINFO();
      53             : };
      54             : 
      55             : #define SV_DECL_PERSIST( Class, CLASS_ID )                          \
      56             :     TYPEINFO_OVERRIDE();                                                     \
      57             :     static  sal_Int32  StaticClassId() { return CLASS_ID; }            \
      58             :     static  void *  CreateInstance( SvPersistBase ** ppBase );      \
      59             :     friend SvPersistStream& operator >> ( SvPersistStream & rStm,   \
      60             :                                           Class *& rpObj);          \
      61             :     virtual sal_Int32  GetClassId() const SAL_OVERRIDE;                             \
      62             :     virtual void    Load( SvPersistStream & ) SAL_OVERRIDE;                      \
      63             :     virtual void    Save( SvPersistStream & ) SAL_OVERRIDE;
      64             : 
      65             : #define SV_DECL_PERSIST1( Class, Super1, CLASS_ID )                 \
      66             :     SV_DECL_PERSIST( Class, CLASS_ID )
      67             : 
      68             : #define PRV_SV_IMPL_PERSIST( Class )                                \
      69             :     void *          Class::CreateInstance( SvPersistBase ** ppBase )\
      70             :                     {                                               \
      71             :                         Class * p = new Class();                    \
      72             :                         *ppBase = p;                                \
      73             :                         return p;                                   \
      74             :                     }                                               \
      75             :     sal_Int32          Class::GetClassId() const                       \
      76             :                     { return StaticClassId(); }                     \
      77             :     SvPersistStream& operator >> (SvPersistStream & rStm, Class *& rpObj)\
      78             :                     {                                               \
      79             :                         SvPersistBase * pObj;                       \
      80             :                         rStm >> pObj;                               \
      81             :                         rpObj = PTR_CAST( Class, pObj );            \
      82             :                         return rStm;                                \
      83             :                     }
      84             : 
      85             : #define SV_IMPL_PERSIST1( Class, Super1 )                           \
      86             :     TYPEINIT1( Class, Super1 )                                      \
      87             :     PRV_SV_IMPL_PERSIST( Class )
      88             : 
      89             : class SvPersistStream;
      90             : 
      91      112607 : class SvPersistBase : public SvRttiBase
      92             : {
      93             : public:
      94             :     virtual sal_Int32  GetClassId() const = 0;
      95             :     virtual void    Load( SvPersistStream & ) = 0;
      96             :     virtual void    Save( SvPersistStream & ) = 0;
      97             :     TOOLS_DLLPUBLIC friend SvPersistStream& operator >> ( SvPersistStream & rStm,
      98             :                                           SvPersistBase *& rpObj );
      99             : };
     100             : 
     101             : typedef UniqueIndex<SvPersistBase> SvPersistUIdx;
     102             : typedef std::map<SvPersistBase*, sal_uIntPtr> PersistBaseMap;
     103             : 
     104             : class SvStream;
     105             : 
     106             : /** Persistent Stream
     107             : 
     108             :     This class provides accessor to storing and loading runtime objects.
     109             :     All dependent objects have to be stored as well.
     110             :     In order to load objects automatically, every object class must
     111             :     provide a Factory method to read an object from stream.
     112             :     The list of all classes is stored in a <SvClassManager> object
     113             :     and is sent to SvPersistStream upon initialization.
     114             :     By using the Method SvPersistStream::WriteCompressed and
     115             :     SvPersistStream::ReadCompressed, compressed sal_uInt32 values may be
     116             :     written to / read from the Stream.
     117             :     Several helper methods exists for writing and reading
     118             :     object lengths to the stream: SvPersistStream::WriteDummyLen,
     119             :     SvPersistStream::WriteLen and SvPersistStream::ReadLen.
     120             : 
     121             :     [Example]
     122             : 
     123             :     One example is described in the constructor.
     124             :     Assume a ring-like dependency, where A referenes B,
     125             :     B itself references C, and C references to both D and A.
     126             : 
     127             :     The order of the objects upon saving and loading does not matter,
     128             :     as long objects are loaded in the same order they were stored.
     129             : 
     130             :     Saving:         Loading:
     131             :     A,B,C,D         A,B,C,D     correct
     132             :     B,A,C,D         B,A,C,D     correct
     133             :     C,A,B,D         A,B,C,D     wrong
     134             :     A,B,C,D         A,B,C       wrong
     135             : 
     136             :     @note The file formats DBG_UTIL and !DBG_UTIL differ, but we can read from
     137             :           both versions.
     138             : */
     139             : class TOOLS_DLLPUBLIC SvPersistStream : public SvStream
     140             : {
     141             :     SvClassManager &    rClassMgr;
     142             :     SvStream *          pStm;
     143             :     PersistBaseMap      aPTable; // reversed pointer and key
     144             :     SvPersistUIdx       aPUIdx;
     145             :     sal_uIntPtr         nStartIdx;
     146             :     const SvPersistStream * pRefStm;
     147             :     sal_uInt32          nFlags;
     148             : 
     149             :     virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
     150             :     virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
     151             :     virtual sal_uInt64  SeekPos(sal_uInt64 nPos) SAL_OVERRIDE;
     152             :     virtual void        FlushData() SAL_OVERRIDE;
     153             : 
     154             : protected:
     155             :     void                WriteObj( sal_uInt8 nHdr, SvPersistBase * pObj );
     156             :     sal_uInt32          ReadObj( SvPersistBase * & rpObj, bool bRegister );
     157             : 
     158             : public:
     159             :     bool                IsStreamed( SvPersistBase * pObj ) const
     160             :                             { return 0 != GetIndex( pObj ); }
     161             :     virtual void        ResetError() SAL_OVERRIDE;
     162             : 
     163             :                         SvPersistStream( SvClassManager &, SvStream * pStream,
     164             :                                          sal_uInt32 nStartIdx = 1 );
     165             :                         virtual ~SvPersistStream();
     166             : 
     167             :     void                SetStream( SvStream * pStream );
     168           0 :     SvStream *          GetStream() const { return pStm; }
     169             : 
     170             :     SvPersistBase *     GetObject( sal_uIntPtr nIdx ) const;
     171             :     sal_uIntPtr         GetIndex( SvPersistBase * ) const;
     172             : 
     173             :     void                SetContextFlags( sal_uInt32 n ) { nFlags = n; }
     174             :     sal_uInt32          GetContextFlags() const { return nFlags; }
     175             : 
     176             :     static void         WriteCompressed( SvStream & rStm, sal_uInt32 nVal );
     177             :     static sal_uInt32   ReadCompressed( SvStream & rStm );
     178             : 
     179             :     sal_uInt32          WriteDummyLen();
     180             :     void                WriteLen( sal_uInt32 nLenPos );
     181             :     sal_uInt32          ReadLen( sal_uInt32 * pTestPos );
     182             : 
     183             :     SvPersistStream&    WritePointer( SvPersistBase * pObj );
     184             :     SvPersistStream&    ReadPointer( SvPersistBase * & rpObj );
     185             :     TOOLS_DLLPUBLIC friend SvPersistStream& WriteSvPersistBase(SvPersistStream &, SvPersistBase *);
     186             :     TOOLS_DLLPUBLIC friend SvPersistStream& operator >> (SvPersistStream &, SvPersistBase * &);
     187             : 
     188             :     // Objects maintain their IDs while storing and loading to/from stream
     189             :     friend SvStream& operator >> ( SvStream &, SvPersistStream & );
     190             :     friend SvStream& WriteSvPersistStream( SvStream &, SvPersistStream & );
     191             : };
     192             : 
     193             : #endif
     194             : 
     195             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11