-
Notifications
You must be signed in to change notification settings - Fork 0
/
_gr_inip.h
422 lines (359 loc) · 13.4 KB
/
_gr_inip.h
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#ifndef __GR_INIP_H
#define __GR_INIP_H
// Copyright David Lawrence Bien 1997 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt).
// _gr_inip.h
// This module implements the stream iteration portion of an inplace
// input iterator for an existing graph.
// No care is taken ( currently, in this implementation ) to ensure that
// the element being read is of the correct type. The current working
// state is such that either:
// a) Any graph element must be able to read any other graph element.
// b) ( and this is implied by (a) ) No check is made to ensure that
// the type of element being read into is of the type located currently
// on the input stream.
// c) If (a) is not the case then the caller needs to know that each element
// will be the same at each point in the iteration.
// A simple templatization can implement (b) above.
__DGRAPH_BEGIN_NAMESPACE
// This object implements those operations that need have no knowledge of the full type specifications
// of the graph elements.
template < class t_TyInputStreamBase,
class t_TyGraphNodeBase, class t_TyGraphLinkBase,
bool t_fUseSeek = true, // In case our input stream does not support _Tell()/_Seek().
// Setting this to true allows the writer to write unconstucted links
// if it is false and the writer encounters an unconstucted link it will throw
// a bad_graph exception.
bool t_fAllowUnconstructedLinks = false >
struct _graph_inplace_input_iter_base
{
private:
typedef _graph_inplace_input_iter_base< t_TyInputStreamBase, t_TyGraphNodeBase, t_TyGraphLinkBase,
t_fUseSeek, t_fAllowUnconstructedLinks > _TyThis;
public:
typedef t_TyGraphNodeBase _TyGraphNodeBase;
typedef t_TyGraphLinkBase _TyGraphLinkBase;
typedef typename t_TyInputStreamBase::_TyGraphNodeBaseReadPtr _TyGraphNodeBaseReadPtr;
typedef typename t_TyInputStreamBase::_TyGraphLinkBaseReadPtr _TyGraphLinkBaseReadPtr;
// Input stream object:
typedef typename t_TyInputStreamBase::_TyStreamPos _TyStreamPos;
t_TyInputStreamBase & m_ris;
bool m_fProcessedGraphFooter;// Processed graph footer ?
// Read and construct nodes/links:
typedef void ( _TyThis :: * _TyPMFnReadNode )( _TyGraphNodeBase * );
_TyPMFnReadNode m_pmfnReadNode;
typedef void ( _TyThis :: * _TyPMFnReadLink )( _TyGraphLinkBase * );
_TyPMFnReadLink m_pmfnReadLink;
// These should not be destroyed - they are status:
_TyGraphNodeBase * m_pgnbCur; // The current link and node.
_TyGraphLinkBase * m_pglbCur;
// to seek or not to seek
void _Seek( _TyStreamPos _sp )
{
if ( t_fUseSeek )
{
m_ris._Seek( _sp );
}
}
void _Tell( _TyStreamPos & _sp )
{
if ( t_fUseSeek )
{
_sp = m_ris._Tell( );
}
}
explicit _graph_inplace_input_iter_base( t_TyInputStreamBase & _ris ) :
m_ris( _ris ),
m_pgnbCur( 0 ),
m_pglbCur( 0 ),
m_fProcessedGraphFooter( false )
{
__THROWPT( e_ttMemory );
}
~_graph_inplace_input_iter_base()
{
}
bool FAtBeg() const _BIEN_NOTHROW
{
return !PGNBCur() && !PGLBCur() && !m_fProcessedGraphFooter;
}
bool FAtEnd() const _BIEN_NOTHROW
{
return !PGNBCur() && !PGLBCur() && m_fProcessedGraphFooter;
}
_TyGraphNodeBase * PGNBCur() const _BIEN_NOTHROW { return m_pgnbCur; }
_TyGraphLinkBase * PGLBCur() const _BIEN_NOTHROW { return m_pglbCur; }
protected:
void SetPGNBCur( _TyGraphNodeBase * _pgnb ) _BIEN_NOTHROW
{
m_pgnbCur = _pgnb;
}
void SetPGLBCur( _TyGraphLinkBase * _pglb ) _BIEN_NOTHROW
{
m_pglbCur = _pglb;
}
// This method does the work - read the next record - create the appropriate graph objects
// - throw on error.
// Read the next graph element into the object that is non-zero.
void _Next( _TyGraphNodeBase * _pgnb,
_TyGraphLinkBase * _pglb )
{
Assert( !_pgnb ^ !_pglb );
// Save stream position for possible throw - this is also good for debugging - if we encounter
// an error in the stream we can go back and re-examine:
_TyStreamPos sp;
_Tell( sp );
SetPGNBCur( _pgnb );
SetPGLBCur( _pglb );
_BIEN_TRY
{
// We check for both context and direction switches so that an input iterator can be kept
// "inline" with a forward iterator - this could allow some iteresting transformations:
while ( _FReadOne() )
{
_Tell( sp ); // Each successful read of a context or direction advances the state.
}
}
_BIEN_UNWIND( _Seek( sp ) );
}
bool _FReadOne()
{
typename _binary_rep_tokens< std::false_type >::_TyToken uc;
m_ris._ReadToken( &uc );
switch( uc )
{
case _binary_rep_tokens< std::false_type >::ms_ucDirectionUp:
case _binary_rep_tokens< std::false_type >::ms_ucDirectionDown:
{
return true;
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucContextPush:
case _binary_rep_tokens< std::false_type >::ms_ucContextPop:
{
return true;
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucNode:
{
_ReadNode();
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucUnfinishedNode:
{
_ReadUnfinishedNode();
}
break;
#ifdef __GR_BINARY_WRITENODEFOOTER
case _binary_rep_tokens< std::false_type >::ms_ucNodeFooter:
{
throw bad_graph_stream( "_FReadOne(): Encountered a node footer token at top level." );
}
break;
#endif //__GR_BINARY_WRITENODEFOOTER
case _binary_rep_tokens< std::false_type >::ms_ucLink:
{
_ReadLink();
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucLinkFromUnfinished:
{
_ReadLinkFromUnfinished();
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucNormalLinkFooter:
{
throw bad_graph_stream( "_FReadOne(): Encountered a normal link footer token at top level." );
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucUnfinishedLinkFooter:
{
throw bad_graph_stream( "_FReadOne(): Encountered an unfinished link footer token at top level." );
}
break;
case _binary_rep_tokens< std::false_type >::ms_ucGraphFooter:
{
_ProcessGraphFooter();
}
break;
default:
{
char cpError[256];
sprintf( cpError, "_FReadOne(): Encountered bogus token [%d] at top level.", uc );
}
break;
}
return false;
}
void _ReadElement()
{
if ( m_pgnbCur )
{
(this->*m_pmfnReadNode)( m_pgnbCur );
}
else
{
(this->*m_pmfnReadLink)( m_pglbCur );
}
}
// Read a normal node from the stream:
void _ReadNode()
{
_TyGraphNodeBaseReadPtr pgnbrNode;
m_ris._ReadNodeHeaderData( &pgnbrNode ); // May be information before the node element. // throws.
_ReadElement();
// Now read any node footer before adding to the graph ( state safe ):
m_ris._ReadNodeFooter();
}
void _ReadUnfinishedNode()
{
// Read the node and link names:
_TyGraphLinkBaseReadPtr pglbrLink;
_TyGraphNodeBaseReadPtr pgnbrNode;
m_ris._ReadUnfinishedHeaderData( &pgnbrNode, &pglbrLink );
_ReadElement();
// Read the link names:
_TyGraphLinkBaseReadPtr pglbrRead;
while( m_ris._ReadLinkName( &pglbrRead ), pglbrRead )
;
}
void _ReadLink()
{
// Allow this method to potentially read the name of the link:
_TyGraphLinkBaseReadPtr pglbrLink;
m_ris._ReadLinkHeaderData( &pglbrLink );
if ( !t_fAllowUnconstructedLinks || m_ris._FReadLinkConstructed() )
{
_ReadElement();
}
_ReadLinkFooter();
}
void _ReadLinkFromUnfinished()
{
_TyGraphLinkBaseReadPtr pglbrLink;
_TyGraphNodeBaseReadPtr pgnbrNode;
m_ris._ReadLinkFromUnfinishedHeaderData( &pglbrLink, &pgnbrNode );
if ( !t_fAllowUnconstructedLinks || m_ris._FReadLinkConstructed() )
{
_ReadElement();
}
_ReadLinkFooter();
}
void _ReadLinkFooter( )
{
_binary_rep_tokens< std::false_type >::_TyToken uc;
m_ris._ReadLinkFooter( &uc );
if ( _binary_rep_tokens< std::false_type >::ms_ucNormalLinkFooter == uc )
{
// nothing.
}
else
{
if ( _binary_rep_tokens< std::false_type >::ms_ucUnfinishedLinkFooter != uc )
{
throw bad_graph_stream( "_ReadLinkFooter(): Found bad link footer token." );
}
// Possibly read the link and node names:
_TyGraphLinkBaseReadPtr pglbrLink;
_TyGraphNodeBaseReadPtr pgnbrNode;
m_ris._ReadUnfinishedLinkFooterData( &pglbrLink, &pgnbrNode );
}
}
void _ProcessGraphFooter()
{
SetPGNBCur( 0 );
SetPGLBCur( 0 );
m_fProcessedGraphFooter = true;
};
};
template < class t_TyGraphNode, class t_TyGraphLink, class t_TyInputStream,
class t_TyBaseInputIter, class t_TyFIsConstIterator >
struct _graph_inplace_input_iterator
: public t_TyBaseInputIter,
public _graph_iter_const_base< t_TyGraphNode, t_TyGraphLink, t_TyFIsConstIterator >
{
private:
typedef _graph_inplace_input_iterator< t_TyGraphNode,
t_TyGraphLink, t_TyInputStream,
t_TyBaseInputIter, t_TyFIsConstIterator > _TyThis;
typedef t_TyBaseInputIter _TyBase;
typedef _graph_iter_const_base< t_TyGraphNode, t_TyGraphLink, t_TyFIsConstIterator > _tyConstIterBase;
public:
typedef _TyBase _TyIterBase; // This type supported by all graph iterators.
typedef _graph_inplace_input_iterator< t_TyGraphNode, t_TyGraphLink, t_TyInputStream,
t_TyBaseInputIter, std::false_type > iterator;
typedef _graph_inplace_input_iterator< t_TyGraphNode, t_TyGraphLink, t_TyInputStream,
t_TyBaseInputIter, std::true_type > const_iterator;
typedef t_TyInputStream _TyInputStream;
typedef typename _TyInputStream::_TyInitArg _TyInitArg;
typedef typename _TyInputStream::_TyIONodeEl _TyIONodeEl;
typedef typename _TyInputStream::_TyIOLinkEl _TyIOLinkEl;
typedef typename _TyBase::_TyGraphNodeCQ _TyGraphNodeCQ;
typedef typename _TyBase::_TyGraphLinkCQ _TyGraphLinkCQ;
typedef typename _tyConstIterBase::node_reference node_reference;
typedef typename _tyConstIterBase::link_reference link_reference;
typedef typename _tyConstIterBase::node_pointer node_pointer;
typedef typename _tyConstIterBase::link_pointer link_pointer;
typedef typename _tyConstIterBase::_TyGraphNode _TyGraphNode;
typedef typename _tyConstIterBase::_TyGraphLink _TyGraphLink;
typedef typename _tyConstIterBase::_TyGraphNodeBaseBase _TyGraphNodeBaseBase;
typedef typename _tyConstIterBase::_TyGraphLinkBaseBase _TyGraphLinkBaseBase;
_TyInputStream m_is; // The input stream object.
_graph_inplace_input_iterator( _TyInitArg _iia,
_TyIONodeEl const & _rione = _TyIONodeEl(),
_TyIOLinkEl const & _riole = _TyIOLinkEl() )
: _TyBase( m_is ),
m_is( _iia, _rione, _riole )
{
// Set up callbacks in base:
_TyBase::m_pmfnReadNode = static_cast< typename _TyBase::_TyPMFnReadNode >
( &_TyThis::_ReadNode );
_TyBase::m_pmfnReadLink = static_cast< typename _TyBase::_TyPMFnReadLink >
( &_TyThis::_ReadLink );
}
~_graph_inplace_input_iterator()
{
}
_TyGraphNodeCQ * PGNCur() const _BIEN_NOTHROW
{
return const_cast< _TyGraphNodeCQ * >( static_cast< _TyGraphNode * >( _TyBase::PGNBCur() ) );
}
_TyGraphLinkCQ * PGLCur() const _BIEN_NOTHROW
{
return const_cast< _TyGraphLinkCQ * >( static_cast< _TyGraphLink * >( _TyBase::PGLBCur() ) );
}
// note: may not has a node ( may be at end of iteration ).
node_reference RNodeEl() const _BIEN_NOTHROW
{
return const_cast< node_reference >( *static_cast< _TyGraphNode * >( _TyBase::PGNBCur() ) );
}
// note: may not have a link!
link_reference RLinkEl() const _BIEN_NOTHROW
{
return const_cast< link_reference >( *static_cast< _TyGraphLink * >( _TyBase::PGLBCur() ) );
}
// The way this works: if the link_pointer is non-null then the iteration is currently
// at a link. Otherwise the iteration either at node_pointer or at the end() if node_pointer null.
pair< link_pointer, node_pointer > PairCur() const _BIEN_NOTHROW
{
return pair< link_pointer, node_pointer >
( _TyBase::PGLBCur() ? &RLinkEl() : 0, _TyBase::PGNBCur() ? &RNodeEl() : 0 );
}
void Next( _TyGraphNode * _pgnbReadInto, _TyGraphLink * _pglbReadInto )
{
_Next( _pgnbReadInto, _pglbReadInto );
}
protected:
void _ReadNode( _TyGraphNodeBaseBase * _pgnb )
{
m_is._ReadNode( static_cast< _TyGraphNode* >( _pgnb ) );
}
void _ReadLink( _TyGraphLinkBaseBase * _pglb )
{
m_is._ReadLink( static_cast< _TyGraphLink* >( _pglb ) );
}
};
__DGRAPH_END_NAMESPACE
#endif //__GR_INIP_H