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 170 : 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 374140 : 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 313602 : 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 :
102 : typedef UniqueIndex<SvPersistBase> SvPersistUIdx;
103 : typedef std::map<SvPersistBase*, sal_uIntPtr> PersistBaseMap;
104 :
105 : class SvStream;
106 :
107 : /** Persistent Stream
108 :
109 : This class provides accessor to storing and loading runtime objects.
110 : All dependent objects have to be stored as well.
111 : In order to load objects automatically, every object class must
112 : provide a Factory method to read an object from stream.
113 : The list of all classes is stored in a <SvClassManager> object
114 : and is sent to SvPersistStream upon initialization.
115 : By using the Method SvPersistStream::WriteCompressed and
116 : SvPersistStream::ReadCompressed, compressed sal_uInt32 values may be
117 : written to / read from the Stream.
118 : Several helper methods exists for writing and reading
119 : object lengths to the stream: SvPersistStream::WriteDummyLen,
120 : SvPersistStream::WriteLen and SvPersistStream::ReadLen.
121 :
122 : [Example]
123 :
124 : One example is described in the constructor.
125 : Assume a ring-like dependency, where A referenes B,
126 : B itself references C, and C references to both D and A.
127 :
128 : The order of the objects upon saving and loading does not matter,
129 : as long objects are loaded in the same order they were stored.
130 :
131 : Saving: Loading:
132 : A,B,C,D A,B,C,D correct
133 : B,A,C,D B,A,C,D correct
134 : C,A,B,D A,B,C,D wrong
135 : A,B,C,D A,B,C wrong
136 :
137 : @note The file formats DBG_UTIL and !DBG_UTIL differ, but we can read from
138 : both versions.
139 : */
140 : class TOOLS_DLLPUBLIC SvPersistStream : public SvStream
141 : {
142 : SvClassManager & rClassMgr;
143 : SvStream * pStm;
144 : PersistBaseMap aPTable; // reversed pointer and key
145 : SvPersistUIdx aPUIdx;
146 : sal_uIntPtr nStartIdx;
147 : const SvPersistStream * pRefStm;
148 : sal_uInt32 nFlags;
149 :
150 : virtual sal_uIntPtr GetData( void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
151 : virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize ) SAL_OVERRIDE;
152 : virtual sal_uInt64 SeekPos(sal_uInt64 nPos) SAL_OVERRIDE;
153 : virtual void FlushData() SAL_OVERRIDE;
154 :
155 : protected:
156 : void WriteObj( sal_uInt8 nHdr, SvPersistBase * pObj );
157 : sal_uInt32 ReadObj( SvPersistBase * & rpObj, bool bRegister );
158 :
159 : public:
160 : bool IsStreamed( SvPersistBase * pObj ) const
161 : { return 0 != GetIndex( pObj ); }
162 : virtual void ResetError() SAL_OVERRIDE;
163 :
164 : SvPersistStream( SvClassManager &, SvStream * pStream,
165 : sal_uInt32 nStartIdx = 1 );
166 : virtual ~SvPersistStream();
167 :
168 : void SetStream( SvStream * pStream );
169 0 : SvStream * GetStream() const { return pStm; }
170 :
171 : SvPersistBase * GetObject( sal_uIntPtr nIdx ) const;
172 : sal_uIntPtr GetIndex( SvPersistBase * ) const;
173 :
174 : void SetContextFlags( sal_uInt32 n ) { nFlags = n; }
175 : sal_uInt32 GetContextFlags() const { return nFlags; }
176 :
177 : static void WriteCompressed( SvStream & rStm, sal_uInt32 nVal );
178 : static sal_uInt32 ReadCompressed( SvStream & rStm );
179 :
180 : sal_uInt32 WriteDummyLen();
181 : void WriteLen( sal_uInt32 nLenPos );
182 : sal_uInt32 ReadLen( sal_uInt32 * pTestPos );
183 :
184 : SvPersistStream& WritePointer( SvPersistBase * pObj );
185 : SvPersistStream& ReadPointer( SvPersistBase * & rpObj );
186 : TOOLS_DLLPUBLIC friend SvPersistStream& WriteSvPersistBase(SvPersistStream &, SvPersistBase *);
187 : TOOLS_DLLPUBLIC friend SvPersistStream& operator >> (SvPersistStream &, SvPersistBase * &);
188 :
189 : // Objects maintain their IDs while storing and loading to/from stream
190 : friend SvStream& operator >> ( SvStream &, SvPersistStream & );
191 : friend SvStream& WriteSvPersistStream( SvStream &, SvPersistStream & );
192 : };
193 :
194 : #endif
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|