File: BaseRepository.h

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (568 lines) | stat: -rw-r--r-- 16,855 bytes parent folder | download
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// -*- C++ -*-
//
// BaseRepository.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef ThePEG_BaseRepository_H
#define ThePEG_BaseRepository_H
// This is the declaration of the BaseRepository class.

#include "ThePEG/Config/ThePEG.h"
#include "BaseRepository.xh"
#include "ThePEG/Interface/InterfaceBase.fh"
#include "ThePEG/Interface/ClassDocumentation.fh"
#include "ThePEG/Interface/InterfacedBase.h"
#include "ThePEG/Utilities/ClassDescription.fh"

namespace ThePEG {

/**
 * BaseRepository is a purely static class which keeps a set of
 * InterfacedBase objects indexed by their name. The objects and their
 * names are divided up in a tree-like structure inspired by the Unix
 * file system.
 *
 * The InterfacedBase objects may be manipulated using InterfaceBase
 * objects. This may be done directly or via a simple command
 * interface using the exec() method.
 *
 * RepositoryBase is closely related to the Repository sub-class. The
 * division may seem unnecessary, but the idea is that BaseRepository
 * is a general repository for administrating and manipulating a set
 * of InterfacedBase objects, while the Repository adds on utilites
 * which are special to ThePEG where the objects are Interfaced (a
 * sub-class of InterfacedBase).
 *
 * @see Repository
 * @see InterfacedBase
 * @see InterfaceBase
 * @see Interfaced
 * 
 */
class BaseRepository {

public:

  /** A set of strings. */
  typedef StringSet DirectorySet;

  /** A vector of character strings. */
  typedef vector<string> StringVector;

  /** A set of pointers to InterfaceBase objects. */
  typedef set<const InterfaceBase *> InterfaceSet;

  /** A map of sets of IterfaceBase objects indexed by pointers to
      ClassDescriptionBase objects. */
  typedef map<const ClassDescriptionBase *, InterfaceSet> TypeInterfaceMap;

  /** A map of ClassDocumentationBase objects indexed by pointers to
      ClassDescriptionBase objects. */
  typedef map<const ClassDescriptionBase *, const ClassDocumentationBase *>
    TypeDocumentationMap;
 
public:

  /**
   * Interpret the command in \a cmd and return possible
   * messages. This is the main function for the command-line
   * interface. The syntax is described elsewhere. The ostream
   * argument is currently unused.
   */
  static string exec(string cmd, ostream &);

  /** @name Functions for adding and deleting objects and interfaces. */
  //@{
  /**
   * Register an interface. This is called automatically in the
   * InterfaceBase constructor and should never be called explicitly.
   */
  static void Register(const InterfaceBase &, const type_info &);

  /**
   * Register a class documentation. This is called automatically in
   * the ClassDocumentationBase constructor and should never be called
   * explicitly.
   */
  static void Register(const ClassDocumentationBase &, const type_info &);

  /**
   * Register a new object using the its current name. If the object
   * is already in the repository, nothing happens. If another object
   * already exists with the same name, the new object will have
   * <code>#</code>'s appended to its name to make it unique.
   */
  static void Register(IBPtr);

  /**
   * Register a new object giving it a new \a name. If the object is
   * already in the repository, nothing happens. If another object
   * already exists with the same name, the new object will have
   * <code>#</code>'s appended to its name to make it unique.
   */
  static void Register(IBPtr, string name);

  /**
   * Remove the given object from the repository. If the object was
   * not present nothing will happen.
   */
  static void remove(tIBPtr);

  /**
   * Remove objects. Remove the objects in \a rmset if there are no
   * other objects in the repository referring to them, otherwise
   * return an error message and the names of the objects refering to
   * them separated by new-line characters.
   */
  static string remove(const ObjectSet & rmset);

  /**
   * Rename a given \a object. Syntacticly the same as
   * <code>remove(object); Register(object, newName);</code>.
   */
  static void rename(tIBPtr object, string newName);
  //@}

  /** @name Access the directory stack. */
  //@{
  /**
   * Create a new directory with the given name. If the given name
   * starts with a <code>/</code> the name is assumed to be an absolute
   * path, otherwise it is assumed to be a path relative to the
   * current directory.
   */
  static void CreateDirectory(string);

  /**
   * Check if directory exixts. Check if the name given as argument
   * corresponds to an existing directory. If the argument string does
   * not end in a <code>/</code> it is assumed to be the name of an
   * object in a directory, and only the directory part of the name is
   * checked. If the given name starts with a <code>/</code> the name
   * is assumed to be an absolute path, otherwise it is assumed to be
   * a path relative to the current directory.
   *
   * @throws RepositoryNoDirectory if the correspinding directory is
   * non-existent.
   */
  static void CheckObjectDirectory(string);

  /**
   * Check if directory exixts. Check if the name given as argument
   * corresponds to an existing directory. If the given name starts
   * with a <code>/</code> the name is assumed to be an absolute path,
   * otherwise it is assumed to be a path relative to the current
   * directory.
   *
   * @throws RepositoryNoDirectory if the correspinding directory is
   * non-existent.
   */
  static void CheckDirectory(string);

  /**
   * Return the absolute path.  If the given name starts with a
   * <code>/</code> the name is assumed to be an absolute path already,
   * otherwise it is assumed to be a path relative to the current
   * directory, and the absolute path is constructed.
   */
  static void DirectoryAppend(string &);

  /**
   * Set the current directory to \a name. \a name can be aither a
   * relative or absolute path. The new directory replaces the
   * previous current directory on the directory stack.
   *
   * @throws RepositoryNoDirectory if the directory is non-existent.
   */
  static void ChangeDirectory(string name);

  /**
   * Set the current directory to \a name. \a name can be aither a
   * relative or absolute path. The new directory is pushed onto the
   * directory stack.
   *
   * @throws RepositoryNoDirectory if the directory is non-existent.
   */
  static void PushDirectory(string name);

  /**
   * Pop the directory stack. Leave the current directory and set the
   * directory which is on top of the popped directory stack.
   */
  static void PopDirectory();

  /**
   * A list of all globally loaded libraries.
   */
  static vector<string> & globalLibraries();

  //@}

  /** @name Information on where to read input files. */
  //@{
protected:

  /**
   * The stack of directories used by the "read" command.
   */
  static stack<string> & currentReadDirStack();

  /**
   * List of directories to search for files for the "read" command.
   */
  static vector<string> & readDirs();

public:

  /**
   * Add a directory to readDirs().
   */
  static void prependReadDir(string);

  /**
   * Add a directory to readDirs().
   */
  static void appendReadDir(string);

  //@}

  /** @name Access objects in the repository. */
  //@{
  /**
   * Return a reference counted pointer to the given object. This
   * currently not needed when ThePEG is used with the
   * ThePEG::Pointer::RCPtr class of pointers.
   */
  template <typename T>
  static typename Ptr<T>::pointer GetPtr(const T &);

  /**
   * Return a pointer of the specified type to an object with the
   * given name. If such an object does not exist, GetPtr will return
   * a null pointer.
   */
  template <typename PtrType>
  static PtrType GetPtr(string);

  /**
   * Return a pointer of the specified type to an object with the
   * given name. If such an object does not exist an exception will be
   * thrown.
   * @throws RepositoryNotFound if the object was not found.
   * @throws RepositoryClassMisMatch if the object exists but is of
   * the wrong class.
   */
  template <typename PtrType>
  static PtrType GetObject(string);

  /**
   * Return a pointer to an object with the given name or null if no
   * such object exists.
   */
  static IBPtr GetPointer(string);

  /**
   * Return all objects in the directory \a name. Optionally only return
   * objects of class \a className or of a sub-class thereof.
   */
  static IVector SearchDirectory(string name, string className = "");

  /**
   * Find an object. If the \a name does not begin with '/', the
   * current directory is prepended. If the string is on the form
   * <code>object:interface</code> (or
   * <code>object:interface[i]</code>) and <code>interface</code>
   * corresponds to an Reference (or RefVector) interface, the
   * corresponding referenced object is returned. (also
   * <code>object:interface:interface</code> is allowed etc.)
   */
  static IBPtr TraceObject(string name);

  /**
   * Return a string containing the name of the given class
   * description and its base classes, one on each line.
   */
  static string GetInterfacedBaseClasses(const ClassDescriptionBase * cdb);

  /**
   * Get an object. Decompose a string of the form
   * <code>object:interface</code> or
   * <code>object:vector-interface[pos]</code>. Retrun a pointer to
   * the corresponding <code>object</code>.
   */
  static IBPtr getObjectFromNoun(string noun);
  //@}

  /** @name Access references between object in the repository. */
  //@{
  /**
   * Get referring objects. Return all object which refers to the
   * given object through a Reference of RefVector interface.
   */
  static IVector GetObjectsReferringTo(IBPtr);

  /**
   * Get direct references. Return all objects the given object refers
   * to directly through a Reference of RefVector interface.
   */
  static IVector DirectReferences(IBPtr);

  /**
   * Get all references. If \a obj contains references to other objects,
   * either through a Reference or RefVector interface or through the
   * virtual getReferences member function, add these to refs. Do the
   * same to the references recursively.
   */
  static void addReferences(tIBPtr obj, ObjectSet & refs);
  //@}

  /** @name Access the interfaces of the objects in the repository. */
  //@{
  /**
   * Get interfaces. Return the interfaces defined for the
   * InterfacedBase class with the given type_info, \a ti, mapped to
   * their name. If several interfaces with the same name exists only
   * the one which correspond to the most derived class will be given,
   * except if \a all is true in which case all interfaces are given
   * (prefixed by '+'s to become unique).
   */
  static InterfaceMap getInterfaces(const type_info & ti, bool all = true);

  /**
   * Return an interface with the given \a name to the given \a object.
   */
  static const InterfaceBase * FindInterface(IBPtr object, string name);

  /**
   * Get an interface name. Decompose a string of the form
   * <code>object:interface</code> or
   * <code>object:vector-interface[pos]</code>. Return the interface
   * name (without the <code>[pos]</code>).
   */
  static string getInterfaceFromNoun(string noun);

  /**
   * Get interface index. Decompose a string of the form
   * <code>object:interface</code> or
   * <code>object:vector-interface[pos]</code>. Return the
   * <code>pos</code> part or empty string if not present.
   */
  static string getPosArgFromNoun(string noun);

  /**
   * Return a list of the interfaces which do not have their default
   * values for the given objects.
   */
  template <typename Cont>
  static vector< pair<IBPtr, const InterfaceBase *> >
  getNonDefaultInterfaces(const Cont &);

  //@}

  /** @name Manipulate objects in the repository. */
  //@{
  /**
   * Call the InterfacedBase::update() function of all objects.
   */
  static void update();

  /**
   * Clear the InterfacedBase::touched() flag in all objects in the
   * given container.
   */
  template<typename Cont>
  static void clearAll(const Cont & c) 
  {  
    for_each(c, mem_fun(&InterfacedBase::clear));
  }

  /**
   * Set the status of all objects in the given container to
   * InterfacedBase::uninitialized.
   */
  template<typename Cont>
  static void resetAll(const Cont & c) 
  {  
    for_each(c, mem_fun(&InterfacedBase::reset));
  }

  /**
   * Setup an object. Execute the InterfacedBase::readSetup() method
   * of \a ip with the stream \a is as argument.
   */
  static void readSetup(tIBPtr ip, istream & is);

  /**
   * Lock the given object. Locked objects cannot be
   * changed through an interface.
   */
  static void lock(tIBPtr ip) { ip->lock(); }

  /**
   * Unlock the given object. Locked objects cannot be changed through
   * an interface.
   */
  static void unlock(tIBPtr ip) { ip->unlock(); }
  //@}

  /** @name Access the documentation of objects. */
  //@{
  /**
   * Return the class documentation of a given object
   */
  static const ClassDocumentationBase * getDocumentation(tcIBPtr ip);

  /**
   * Get the description for the model implemented in the class of the
   * given object.
   */
  static string getModelDescription(tcIBPtr ip);

  /**
   * Get the references for the model implemented in the class of the
   * given object.
   */
  static string getModelReferences(tcIBPtr ip);
  //@}

  /** @name Manipulate the output streams of the repository. */
  //@{
  /**
   * Set the standard output stream
   */
  static void cout(ostream & os) { coutp() = &os; }

  /**
   * Get the standard output stream
   */
  static ostream & cout() { return *coutp(); }

  /**
   * Set the standard error stream
   */
  static void cerr(ostream & os) { cerrp() = &os; }

  /**
   * Get the standard error stream
   */
  static ostream & cerr() { return *cerrp(); }

  /**
   * Set the standard log stream
   */
  static void clog(ostream & os) { clogp() = &os; }

  /**
   * Get the standard log stream
   */
  static ostream & clog() { return *clogp(); }
  //@}

protected:

  /** @name Access standard InterfacedBase functions. */
  //@{
  /**
   * Return a clone of the given object. Calls the
   * InterfacedBase::clone() function of \a t and casts the resulting
   * pointer to the correct type.
   */
  template <typename T>
  static typename Ptr<T>::pointer clone(const T & t);

  /**
   * Return a clone of the given object. Calls the
   * InterfacedBase::fullclone() function of \a t and casts the
   * resulting pointer to the correct type.
   */
  template <typename T>
  static typename Ptr<T>::pointer fullclone(const T & t);

  /**
   * Rebind references. For all objects directly referenced by \a obj,
   * replace them with the translation found in \a trans. If \a obj has a
   * Reference or a member of a RefVector interface which is null, and
   * the corresponding interface has the RefInterfaceBase::defaultIfNull() flag set,
   * translate the null pointer to the first acceptable object in
   * defaults.
   */
  static void rebind(InterfacedBase & obj, const TranslationMap & trans,
		     const IVector & defaults);
  //@}
  

  /**
   * Add interfaces to the given map for the class with the given
   * class description. Recursively do the same with the base classes.
   */
  static void addInterfaces(const ClassDescriptionBase &,
			    InterfaceMap &, bool all = true);

  /** @name Functions containing the static instances of objects used
      by the repository. */
  //@{
  /**
   * All InterfacedBase objects mapped to their name.
   */
  static ObjectMap & objects();

  /**
   * All InterfacedBase objects.
   */
  static ObjectSet & allObjects();

  /**
   * Sets of InterfaceBase objects mapped to the class description of
   * the class for which they are defined.
   */
  static TypeInterfaceMap & interfaces();

  /**
   * Sets of ClassDocumentationBase objects mapped to the class
   * description of the class for which they are defined.
   */
  static TypeDocumentationMap & documentations();

  /**
   * All defined directories.
   */
  static DirectorySet & directories();

  /**
   * The current directory stack.
   */
  static StringVector & directoryStack();

  /**
   * Flag to say if we are in the middle of an update procedure.
   */
  static bool & updating();

  /**
   * The current current standard output stream.
   */
  static ostream *& coutp();

  /**
   * The current current standard error stream.
   */
  static ostream *& cerrp();
  /**
   * The current current standard log stream.
   */
  static ostream *& clogp();
  //@}

};


}

#ifndef ThePEG_TEMPLATES_IN_CC_FILE
#include "BaseRepository.tcc"
#endif

#endif /* ThePEG_BaseRepository_H */