LCOV - code coverage report
Current view: top level - include/tools - pstm.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 16 22 72.7 %
Date: 2014-04-11 Functions: 54 77 70.1 %
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 <boost/unordered_map.hpp>
      23             : #include <tools/toolsdllapi.h>
      24             : #include <tools/unqidx.hxx>
      25             : #include <tools/ref.hxx>
      26             : #include <tools/rtti.hxx>
      27             : #include <tools/stream.hxx>
      28             : #include <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          90 : class TOOLS_DLLPUBLIC SvClassManager
      40             : {
      41             :     typedef boost::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      126204 : class TOOLS_DLLPUBLIC SvRttiBase : public SvRefBase
      50             : {
      51             : public:
      52             :             TYPEINFO();
      53             : };
      54             : typedef tools::SvRef<SvRttiBase> SvRttiBaseRef;
      55             : 
      56             : #define SV_DECL_PERSIST( Class, CLASS_ID )                          \
      57             :     TYPEINFO_OVERRIDE();                                                     \
      58             :     static  sal_Int32  StaticClassId() { return CLASS_ID; }            \
      59             :     static  void *  CreateInstance( SvPersistBase ** ppBase );      \
      60             :     friend SvPersistStream& operator >> ( SvPersistStream & rStm,   \
      61             :                                           Class *& rpObj);          \
      62             :     virtual sal_Int32  GetClassId() const SAL_OVERRIDE;                             \
      63             :     virtual void    Load( SvPersistStream & ) SAL_OVERRIDE;                      \
      64             :     virtual void    Save( SvPersistStream & ) SAL_OVERRIDE;
      65             : 
      66             : #define SV_DECL_PERSIST1( Class, Super1, CLASS_ID )                 \
      67             :     SV_DECL_PERSIST( Class, CLASS_ID )
      68             : 
      69             : #define PRV_SV_IMPL_PERSIST( Class )                                \
      70             :     void *          Class::CreateInstance( SvPersistBase ** ppBase )\
      71             :                     {                                               \
      72             :                         Class * p = new Class();                    \
      73             :                         *ppBase = p;                                \
      74             :                         return p;                                   \
      75             :                     }                                               \
      76             :     sal_Int32          Class::GetClassId() const                       \
      77             :                     { return StaticClassId(); }                     \
      78             :     SvPersistStream& operator >> (SvPersistStream & rStm, Class *& rpObj)\
      79             :                     {                                               \
      80             :                         SvPersistBase * pObj;                       \
      81             :                         rStm >> pObj;                               \
      82             :                         rpObj = PTR_CAST( Class, pObj );            \
      83             :                         return rStm;                                \
      84             :                     }
      85             : 
      86             : #define SV_IMPL_PERSIST1( Class, Super1 )                           \
      87             :     TYPEINIT1( Class, Super1 )                                      \
      88             :     PRV_SV_IMPL_PERSIST( Class )
      89             : 
      90             : class SvPersistStream;
      91             : 
      92      126204 : class SvPersistBase : public SvRttiBase
      93             : {
      94             : public:
      95             :     virtual sal_Int32  GetClassId() const = 0;
      96             :     virtual void    Load( SvPersistStream & ) = 0;
      97             :     virtual void    Save( SvPersistStream & ) = 0;
      98             :     TOOLS_DLLPUBLIC friend SvPersistStream& operator >> ( SvPersistStream & rStm,
      99             :                                           SvPersistBase *& rpObj );
     100             : };
     101             : typedef tools::SvRef<SvPersistBase> SvPersistBaseRef;
     102             : 
     103        3768 : class SvPersistListWriteable
     104             : {
     105             : public:
     106         609 :     virtual ~SvPersistListWriteable() {}
     107             :     virtual size_t size() const = 0;
     108             :     virtual SvPersistBase* GetPersistBase(size_t idx) const = 0;
     109             : };
     110             : 
     111        3768 : class SvPersistListReadable
     112             : {
     113             : public:
     114         609 :     virtual ~SvPersistListReadable() {}
     115             :     virtual void push_back(SvPersistBase* p) = 0;
     116             : };
     117             : 
     118             : void TOOLS_DLLPUBLIC WritePersistListObjects(const SvPersistListWriteable& rList, SvPersistStream & rStm, bool bOnlyStreamed = false );
     119             : 
     120             : void TOOLS_DLLPUBLIC ReadObjects( SvPersistListReadable& rLst, SvPersistStream & rStm);
     121             : 
     122             : // <T> has to be a subtype of "SvPersistBase*"
     123             : template<typename T>
     124        4377 : class SvDeclPersistList : public SvRefMemberList<T>,
     125             :                           public SvPersistListWriteable,
     126             :                           public SvPersistListReadable
     127             : {
     128             : public:
     129             :    // implement the reader/writer adapter methods
     130     8153471 :     size_t size() const SAL_OVERRIDE { return SvRefMemberList<T>::size(); }
     131       30872 :     SvPersistBase* GetPersistBase(size_t idx) const SAL_OVERRIDE { return SvRefMemberList<T>::operator[](idx); }
     132       33183 :     void push_back(SvPersistBase* p) SAL_OVERRIDE { SvRefMemberList<T>::push_back(static_cast<T>(p)); }
     133          24 :     void WriteObjects(SvPersistStream & rStm, bool bOnlyStreamed ) const { WritePersistListObjects(*this, rStm, bOnlyStreamed); }
     134             : };
     135             : 
     136             : template<typename T>
     137        2217 : SvPersistStream& WriteSvDeclPersistList(SvPersistStream &rStm, const SvDeclPersistList<T> &rLst)
     138             : {
     139        2217 :     WritePersistListObjects( rLst, rStm );
     140        2217 :     return rStm;
     141             : };
     142             : 
     143             : template<typename T>
     144           0 : SvPersistStream& operator >> (SvPersistStream &rStm, SvDeclPersistList<T> &rLst)
     145             : {
     146           0 :     ReadObjects( rLst, rStm );
     147           0 :     return rStm;
     148             : };
     149             : 
     150             : typedef UniqueIndex<SvPersistBase> SvPersistUIdx;
     151             : typedef std::map<SvPersistBase*, sal_uIntPtr> PersistBaseMap;
     152             : 
     153             : class SvStream;
     154             : 
     155             : /** Persistent Stream
     156             : 
     157             :     This class provides accessor to storing and loading runtime objects.
     158             :     All dependent objects have to be stored as well.
     159             :     In order to load objects automatically, every object class must
     160             :     provide a Factory method to read an object from stream.
     161             :     The list of all classes is stored in a <SvClassManager> object
     162             :     and is sent to SvPersistStream upon initialization.
     163             :     By using the Method SvPersistStream::WriteCompressed and
     164             :     SvPersistStream::ReadCompressed, compressed sal_uInt32 values may be
     165             :     written to / read from the Stream.
     166             :     Several helper methods exists for writing and reading
     167             :     object lengths to the stream: SvPersistStream::WriteDummyLen,
     168             :     SvPersistStream::WriteLen and SvPersistStream::ReadLen.
     169             : 
     170             :     [Example]
     171             : 
     172             :     One example is described in the constructor.
     173             :     Assume a ring-like dependency, where A referenes B,
     174             :     B itself references C, and C references to both D and A.
     175             : 
     176             :     The order of the objects upon saving and loading does not matter,
     177             :     as long objects are loaded in the same order they were stored.
     178             : 
     179             :     Saving:         Loading:
     180             :     A,B,C,D         A,B,C,D     correct
     181             :     B,A,C,D         B,A,C,D     correct
     182             :     C,A,B,D         A,B,C,D     wrong
     183             :     A,B,C,D         A,B,C       wrong
     184             : 
     185             :     @note The file formats DBG_UTIL and !DBG_UTIL differ, but we can read from
     186             :           both versions.
     187             : */
     188             : class TOOLS_DLLPUBLIC SvPersistStream : public SvStream
     189             : {
     190             :     SvClassManager &    rClassMgr;
     191             :     SvStream *          pStm;
     192             :     PersistBaseMap      aPTable; // reversed pointer and key
     193             :     SvPersistUIdx       aPUIdx;
     194             :     sal_uIntPtr         nStartIdx;
     195             :     const SvPersistStream * pRefStm;
     196             :     sal_uInt32          nFlags;
     197             : 
     198             :     virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
     199             :     virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
     200             :     virtual sal_uInt64  SeekPos(sal_uInt64 nPos) SAL_OVERRIDE;
     201             :     virtual void        FlushData() SAL_OVERRIDE;
     202             : 
     203             : protected:
     204             :     void                WriteObj( sal_uInt8 nHdr, SvPersistBase * pObj );
     205             :     sal_uInt32          ReadObj( SvPersistBase * & rpObj, bool bRegister );
     206             : 
     207             : public:
     208           0 :     bool                IsStreamed( SvPersistBase * pObj ) const
     209           0 :                             { return 0 != GetIndex( pObj ); }
     210             :     virtual void        ResetError() SAL_OVERRIDE;
     211             : 
     212             :                         SvPersistStream( SvClassManager &, SvStream * pStream,
     213             :                                          sal_uInt32 nStartIdx = 1 );
     214             :                         virtual ~SvPersistStream();
     215             : 
     216             :     void                SetStream( SvStream * pStream );
     217           0 :     SvStream *          GetStream() const { return pStm; }
     218             : 
     219             :     SvPersistBase *     GetObject( sal_uIntPtr nIdx ) const;
     220             :     sal_uIntPtr         GetIndex( SvPersistBase * ) const;
     221             : 
     222           8 :     void                SetContextFlags( sal_uInt32 n ) { nFlags = n; }
     223             :     sal_uInt32          GetContextFlags() const { return nFlags; }
     224             : 
     225             :     static void         WriteCompressed( SvStream & rStm, sal_uInt32 nVal );
     226             :     static sal_uInt32   ReadCompressed( SvStream & rStm );
     227             : 
     228             :     sal_uInt32          WriteDummyLen();
     229             :     void                WriteLen( sal_uInt32 nLenPos );
     230             :     sal_uInt32          ReadLen( sal_uInt32 * pTestPos );
     231             : 
     232             :     SvPersistStream&    WritePointer( SvPersistBase * pObj );
     233             :     SvPersistStream&    ReadPointer( SvPersistBase * & rpObj );
     234             :     TOOLS_DLLPUBLIC friend SvPersistStream& WriteSvPersistBase(SvPersistStream &, SvPersistBase *);
     235             :     TOOLS_DLLPUBLIC friend SvPersistStream& operator >> (SvPersistStream &, SvPersistBase * &);
     236             : 
     237             :     // Objects maintain their IDs while storing and loading to/from stream
     238             :     friend SvStream& operator >> ( SvStream &, SvPersistStream & );
     239             :     friend SvStream& WriteSvPersistStream( SvStream &, SvPersistStream & );
     240             : };
     241             : 
     242             : #endif
     243             : 
     244             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10