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 _PSTM_HXX
20 : #define _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 34 : 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 67300 : class TOOLS_DLLPUBLIC SvRttiBase : public SvRefBase
50 : {
51 : public:
52 : TYPEINFO();
53 : };
54 : SV_DECL_IMPL_REF(SvRttiBase)
55 :
56 : #define SV_DECL_PERSIST( Class, CLASS_ID ) \
57 : TYPEINFO(); \
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; \
63 : virtual void Load( SvPersistStream & ); \
64 : virtual void Save( SvPersistStream & );
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 67300 : 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 : SV_DECL_IMPL_REF(SvPersistBase)
102 :
103 3712 : class SvPersistListWriteable
104 : {
105 : public:
106 603 : virtual ~SvPersistListWriteable() {}
107 : virtual size_t size() const = 0;
108 : virtual SvPersistBase* GetPersistBase(size_t idx) const = 0;
109 : };
110 :
111 3712 : class SvPersistListReadable
112 : {
113 : public:
114 603 : 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 4315 : class SvDeclPersistList : public SvRefMemberList<T>,
125 : public SvPersistListWriteable,
126 : public SvPersistListReadable
127 : {
128 : public:
129 : // implement the reader/writer adapter methods
130 7553804 : size_t size() const { return SvRefMemberList<T>::size(); }
131 29933 : SvPersistBase* GetPersistBase(size_t idx) const { return SvRefMemberList<T>::operator[](idx); }
132 32184 : void push_back(SvPersistBase* p) { 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 2181 : SvPersistStream& operator << (SvPersistStream &rStm, const SvDeclPersistList<T> &rLst)
138 : {
139 2181 : WritePersistListObjects( rLst, rStm );
140 2181 : 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 );
199 : virtual sal_uIntPtr PutData( const void* pData, sal_uIntPtr nSize );
200 : virtual sal_uIntPtr SeekPos( sal_uIntPtr nPos );
201 : virtual void FlushData();
202 :
203 : protected:
204 : void WriteObj( sal_uInt8 nHdr, SvPersistBase * pObj );
205 : sal_uInt32 ReadObj( SvPersistBase * & rpObj, sal_Bool bRegister );
206 :
207 : public:
208 : sal_Bool IsStreamed( SvPersistBase * pObj ) const
209 : { return 0 != GetIndex( pObj ); }
210 : virtual void ResetError();
211 :
212 : SvPersistStream( SvClassManager &, SvStream * pStream,
213 : sal_uInt32 nStartIdx = 1 );
214 : ~SvPersistStream();
215 :
216 : void SetStream( SvStream * pStream );
217 : SvStream * GetStream() const { return pStm; }
218 : virtual sal_uInt16 IsA() const;
219 :
220 : SvPersistBase * GetObject( sal_uIntPtr nIdx ) const;
221 : sal_uIntPtr GetIndex( SvPersistBase * ) const;
222 :
223 8 : void SetContextFlags( sal_uInt32 n ) { nFlags = n; }
224 : sal_uInt32 GetContextFlags() const { return nFlags; }
225 :
226 : static void WriteCompressed( SvStream & rStm, sal_uInt32 nVal );
227 : static sal_uInt32 ReadCompressed( SvStream & rStm );
228 :
229 : sal_uInt32 WriteDummyLen();
230 : void WriteLen( sal_uInt32 nLenPos );
231 : sal_uInt32 ReadLen( sal_uInt32 * pTestPos );
232 :
233 : SvPersistStream& WritePointer( SvPersistBase * pObj );
234 : SvPersistStream& ReadPointer( SvPersistBase * & rpObj );
235 : TOOLS_DLLPUBLIC friend SvPersistStream& operator << (SvPersistStream &, SvPersistBase *);
236 : TOOLS_DLLPUBLIC friend SvPersistStream& operator >> (SvPersistStream &, SvPersistBase * &);
237 :
238 : // Objects maintain their IDs while storing and loading to/from stream
239 : friend SvStream& operator >> ( SvStream &, SvPersistStream & );
240 : friend SvStream& operator << ( SvStream &, SvPersistStream & );
241 : };
242 :
243 : #endif
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|