File: SaveLoadManager.h

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (214 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2013-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef SAVELOADMANAGER_H
#define SAVELOADMANAGER_H

#include <string>
#include <iostream>
#include <vector>
#include <map>

#include "globaldef.h"
#include "eFPPSaveLoad.h"

class TNamedEntity;
class TNamedEntry;
class TaxonomyVertex;

class SaveLoadManager
{
protected:	// types
		// maps pointers and numbers
	template<class T>
	class PointerMap
	{
	protected:	// types
			/// map int->pointer type
		typedef std::vector<const T*> I2PMap;
			/// map pointer->int type
		typedef std::map<const T*, unsigned int> P2IMap;

	protected:	// members
			/// map i -> pointer
		I2PMap i2p;
			/// map pointer -> i
		P2IMap p2i;
			/// ID of the last recorded NE
		unsigned int last;

	protected:	// methods
			/// @return true if given pointer present in the map
		bool in ( const T* p ) const { return p2i.find(p) != p2i.end(); }
			/// @return true if given index present in the map
		bool in ( unsigned int i ) const { return i < last; }
			/// @throw an exception if P is not registered
		void ensure ( const T* p ) const { if ( !in(p) ) throw EFPPSaveLoad("Cannot save unregistered pointer"); }
			/// @throw an exception if I is not registered
		void ensure ( unsigned int i ) const { if ( !in(i) ) throw EFPPSaveLoad("Cannot load unregistered index"); }

	public:		// interface
			/// empty c'tor
		PointerMap ( void ) : last(0) {}
			/// empty d'tor
		~PointerMap ( void ) {}
			/// clear the maps
		void clear ( void )
		{
			i2p.clear();
			p2i.clear();
			last = 0;
		}

		// populate the map

			/// add an entry
		void add ( T* p )
		{
			if ( in(p) )
				return;
			i2p.push_back(p);
			p2i[p] = last++;
		}

		// access to the mapped element

			/// get the NE by index I
		T* getP ( unsigned int i ) { ensure(i); return const_cast<T*>(i2p[i]); }
			/// get the index by NE P
		unsigned int getI ( const T* p ) { ensure(p); return p2i[p]; }
	}; // PointerMap

protected:	// members
		/// name of S/L dir
	std::string dirname;
		/// file name
	std::string filename;
		/// input stream pointer
	std::istream* ip;
		/// output stream pointer
	std::ostream* op;

		// uint <-> named entity map for the current taxonomy
	PointerMap<TNamedEntity> eMap;
		// uint <-> named entry map for the current taxonomy
	PointerMap<TNamedEntry> neMap;
		// uint <-> TaxonomyVertex map to update the taxonomy
	PointerMap<TaxonomyVertex> tvMap;

public:		// methods
		/// init c'tor: remember the S/L name
	SaveLoadManager ( const std::string& name ) : dirname(name), ip(NULL), op(NULL) { filename = name+".fpp.state"; }
		/// empty d'tor
	~SaveLoadManager ( void )
	{
		delete ip;
		delete op;
	}

	// context information

		/// @return true if there is some S/L content
	bool existsContent ( void ) const;
		/// clear all the content corresponding to the manager
	void clearContent ( void ) const;

	// set up stream

		/// prepare stream according to INPUT value
	void prepare ( bool input );
		/// get an input stream
	std::istream& i ( void ) { return *ip; }
		/// get an output stream
	std::ostream& o ( void ) { return *op; }
		/// check whether stream is in a good shape
	void checkStream ( void ) const
	{
		if ( ip && unlikely(!ip->good()) )
			throw EFPPSaveLoad ( filename, /*save=*/false);
		if ( op && unlikely(!op->good()) )
			throw EFPPSaveLoad ( filename, /*save=*/true);
	}

	// save/load primitives

		/// load a single char from input, throw an exception if it is not a given one
	inline void expectChar ( const char C )
	{
		char c;
		i() >> c;
		if ( c != C )
			throw EFPPSaveLoad(C);
	}

	// save/load integers

		/// save unsigned integer
	inline void saveUInt ( unsigned int n ) { o() << "(" << n << ")"; }
		/// save signed integer
	inline void saveSInt ( int n ) { o() << "(" << n << ")"; }
		/// load unsigned integer
	inline unsigned int loadUInt ( void )
	{
		unsigned int ret;
		expectChar('(');
		i() >> ret;
		expectChar(')');
		return ret;
	}
		/// load signed integer
	inline int loadSInt ( void )
	{
		int ret;
		expectChar('(');
		i() >> ret;
		expectChar(')');
		return ret;
	}

	// pointer <-> int related methods

		/// clear all maps
	void clearPointerMaps ( void )
	{
		neMap.clear();
		eMap.clear();
		tvMap.clear();
	}
		/// register named entry together with entity (if available)
	void registerE ( const TNamedEntry* p );
		/// register taxonomy vertex
	void registerV ( TaxonomyVertex* v ) { tvMap.add(v); }

		/// save Entry pointer
	void savePointer ( const TNamedEntry* p ) { saveUInt(neMap.getI(p)); }
		/// save Entity pointer
	void savePointer ( const TNamedEntity* p ) { saveUInt(eMap.getI(const_cast<TNamedEntity*>(p))); }
		/// save Vertex pointer
	void savePointer ( const TaxonomyVertex* p ) { saveUInt(tvMap.getI(const_cast<TaxonomyVertex*>(p))); }

		/// load Entry pointer
	TNamedEntry* loadEntry ( void ) { return neMap.getP(loadUInt()); }
		/// load Entity pointer
	TNamedEntity* loadEntity ( void ) { return eMap.getP(loadUInt()); }
		/// load Vetrex pointer
	TaxonomyVertex* loadVertex ( void ) { return tvMap.getP(loadUInt()); }
}; // SaveLoadManager

#endif