|
|
|||
File indexing completed on 2026-08-06 09:38:30
0001 // -*- C++ -*- 0002 // 0003 // BaseRepository.h is a part of ThePEG - Toolkit for HEP Event Generation 0004 // Copyright (C) 1999-2019 Leif Lonnblad 0005 // 0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details. 0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 0008 // 0009 #ifndef ThePEG_BaseRepository_H 0010 #define ThePEG_BaseRepository_H 0011 // This is the declaration of the BaseRepository class. 0012 0013 #include "ThePEG/Config/ThePEG.h" 0014 #include "BaseRepository.xh" 0015 #include "ThePEG/Interface/InterfaceBase.fh" 0016 #include "ThePEG/Interface/ClassDocumentation.fh" 0017 #include "ThePEG/Interface/InterfacedBase.h" 0018 #include "ThePEG/Utilities/ClassDescription.fh" 0019 0020 namespace ThePEG { 0021 0022 /** 0023 * BaseRepository is a purely static class which keeps a set of 0024 * InterfacedBase objects indexed by their name. The objects and their 0025 * names are divided up in a tree-like structure inspired by the Unix 0026 * file system. 0027 * 0028 * The InterfacedBase objects may be manipulated using InterfaceBase 0029 * objects. This may be done directly or via a simple command 0030 * interface using the exec() method. 0031 * 0032 * RepositoryBase is closely related to the Repository sub-class. The 0033 * division may seem unnecessary, but the idea is that BaseRepository 0034 * is a general repository for administrating and manipulating a set 0035 * of InterfacedBase objects, while the Repository adds on utilites 0036 * which are special to ThePEG where the objects are Interfaced (a 0037 * sub-class of InterfacedBase). 0038 * 0039 * @see Repository 0040 * @see InterfacedBase 0041 * @see InterfaceBase 0042 * @see Interfaced 0043 * 0044 */ 0045 class BaseRepository { 0046 0047 public: 0048 0049 /** A set of strings. */ 0050 typedef StringSet DirectorySet; 0051 0052 /** A vector of character strings. */ 0053 typedef vector<string> StringVector; 0054 0055 /** A set of pointers to InterfaceBase objects. */ 0056 typedef set<const InterfaceBase *> InterfaceSet; 0057 0058 /** A map of sets of IterfaceBase objects indexed by pointers to 0059 ClassDescriptionBase objects. */ 0060 typedef map<const ClassDescriptionBase *, InterfaceSet> TypeInterfaceMap; 0061 0062 /** A map of ClassDocumentationBase objects indexed by pointers to 0063 ClassDescriptionBase objects. */ 0064 typedef map<const ClassDescriptionBase *, const ClassDocumentationBase *> 0065 TypeDocumentationMap; 0066 0067 public: 0068 0069 /** 0070 * Interpret the command in \a cmd and return possible 0071 * messages. This is the main function for the command-line 0072 * interface. The syntax is described elsewhere. The ostream 0073 * argument is currently unused. 0074 */ 0075 static string exec(string cmd, ostream &); 0076 0077 /** @name Functions for adding and deleting objects and interfaces. */ 0078 //@{ 0079 /** 0080 * Register an interface. This is called automatically in the 0081 * InterfaceBase constructor and should never be called explicitly. 0082 */ 0083 static void Register(const InterfaceBase &, const type_info &); 0084 0085 /** 0086 * Register a class documentation. This is called automatically in 0087 * the ClassDocumentationBase constructor and should never be called 0088 * explicitly. 0089 */ 0090 static void Register(const ClassDocumentationBase &, const type_info &); 0091 0092 /** 0093 * Register a new object using the its current name. If the object 0094 * is already in the repository, nothing happens. If another object 0095 * already exists with the same name, the new object will have 0096 * <code>#</code>'s appended to its name to make it unique. 0097 */ 0098 static void Register(IBPtr); 0099 0100 /** 0101 * Register a new object giving it a new \a name. If the object is 0102 * already in the repository, nothing happens. If another object 0103 * already exists with the same name, the new object will have 0104 * <code>#</code>'s appended to its name to make it unique. 0105 */ 0106 static void Register(IBPtr, string name); 0107 0108 /** 0109 * Remove the given object from the repository. If the object was 0110 * not present nothing will happen. 0111 */ 0112 static void remove(tIBPtr); 0113 0114 /** 0115 * Remove objects. Remove the objects in \a rmset if there are no 0116 * other objects in the repository referring to them, otherwise 0117 * return an error message and the names of the objects refering to 0118 * them separated by new-line characters. 0119 */ 0120 static string remove(const ObjectSet & rmset); 0121 0122 /** 0123 * Rename a given \a object. Syntacticly the same as 0124 * <code>remove(object); Register(object, newName);</code>. 0125 */ 0126 static void rename(tIBPtr object, string newName); 0127 //@} 0128 0129 /** @name Access the directory stack. */ 0130 //@{ 0131 /** 0132 * Create a new directory with the given name. If the given name 0133 * starts with a <code>/</code> the name is assumed to be an absolute 0134 * path, otherwise it is assumed to be a path relative to the 0135 * current directory. 0136 */ 0137 static void CreateDirectory(string); 0138 0139 /** 0140 * Check if directory exixts. Check if the name given as argument 0141 * corresponds to an existing directory. If the argument string does 0142 * not end in a <code>/</code> it is assumed to be the name of an 0143 * object in a directory, and only the directory part of the name is 0144 * checked. If the given name starts with a <code>/</code> the name 0145 * is assumed to be an absolute path, otherwise it is assumed to be 0146 * a path relative to the current directory. 0147 * 0148 * @throws RepositoryNoDirectory if the correspinding directory is 0149 * non-existent. 0150 */ 0151 static void CheckObjectDirectory(string); 0152 0153 /** 0154 * Check if directory exixts. Check if the name given as argument 0155 * corresponds to an existing directory. If the given name starts 0156 * with a <code>/</code> the name is assumed to be an absolute path, 0157 * otherwise it is assumed to be a path relative to the current 0158 * directory. 0159 * 0160 * @throws RepositoryNoDirectory if the correspinding directory is 0161 * non-existent. 0162 */ 0163 static void CheckDirectory(string); 0164 0165 /** 0166 * Return the absolute path. If the given name starts with a 0167 * <code>/</code> the name is assumed to be an absolute path already, 0168 * otherwise it is assumed to be a path relative to the current 0169 * directory, and the absolute path is constructed. 0170 */ 0171 static void DirectoryAppend(string &); 0172 0173 /** 0174 * Set the current directory to \a name. \a name can be aither a 0175 * relative or absolute path. The new directory replaces the 0176 * previous current directory on the directory stack. 0177 * 0178 * @throws RepositoryNoDirectory if the directory is non-existent. 0179 */ 0180 static void ChangeDirectory(string name); 0181 0182 /** 0183 * Set the current directory to \a name. \a name can be aither a 0184 * relative or absolute path. The new directory is pushed onto the 0185 * directory stack. 0186 * 0187 * @throws RepositoryNoDirectory if the directory is non-existent. 0188 */ 0189 static void PushDirectory(string name); 0190 0191 /** 0192 * Pop the directory stack. Leave the current directory and set the 0193 * directory which is on top of the popped directory stack. 0194 */ 0195 static void PopDirectory(); 0196 0197 /** 0198 * A list of all globally loaded libraries. 0199 */ 0200 static vector<string> & globalLibraries(); 0201 0202 //@} 0203 0204 /** @name Information on where to read input files. */ 0205 //@{ 0206 protected: 0207 0208 /** 0209 * The stack of directories used by the "read" command. 0210 */ 0211 static stack<string> & currentReadDirStack(); 0212 0213 /** 0214 * List of directories to search for files for the "read" command. 0215 */ 0216 static vector<string> & readDirs(); 0217 0218 public: 0219 0220 /** 0221 * Access to list of directories to search for files for the "read" command. 0222 */ 0223 static const vector<string> & listReadDirs(); 0224 0225 /** 0226 * Add a directory to readDirs(). 0227 */ 0228 static void prependReadDir(string); 0229 0230 /** 0231 * Add a string vector with directories to readDirs(). 0232 */ 0233 static void prependReadDir(const std::vector<std::string>& dirs); 0234 0235 /** 0236 * Add a directory to readDirs(). 0237 */ 0238 static void appendReadDir(string); 0239 0240 /** 0241 * Add a string vector with directories to readDirs(). 0242 */ 0243 static void appendReadDir(const std::vector<std::string>& dirs); 0244 0245 //@} 0246 0247 /** @name Access objects in the repository. */ 0248 //@{ 0249 /** 0250 * Return a reference counted pointer to the given object. This 0251 * currently not needed when ThePEG is used with the 0252 * ThePEG::Pointer::RCPtr class of pointers. 0253 */ 0254 template <typename T> 0255 static typename Ptr<T>::pointer GetPtr(const T &); 0256 0257 /** 0258 * Return a pointer of the specified type to an object with the 0259 * given name. If such an object does not exist, GetPtr will return 0260 * a null pointer. 0261 */ 0262 template <typename PtrType> 0263 static PtrType GetPtr(string); 0264 0265 /** 0266 * Return a pointer of the specified type to an object with the 0267 * given name. If such an object does not exist an exception will be 0268 * thrown. 0269 * @throws RepositoryNotFound if the object was not found. 0270 * @throws RepositoryClassMisMatch if the object exists but is of 0271 * the wrong class. 0272 */ 0273 template <typename PtrType> 0274 static PtrType GetObject(string); 0275 0276 /** 0277 * Return a pointer to an object with the given name or null if no 0278 * such object exists. 0279 */ 0280 static IBPtr GetPointer(string); 0281 0282 /** 0283 * Return all objects in the directory \a name. Optionally only return 0284 * objects of class \a className or of a sub-class thereof. 0285 */ 0286 static IVector SearchDirectory(string name, string className = ""); 0287 0288 /** 0289 * Find an object. If the \a name does not begin with '/', the 0290 * current directory is prepended. If the string is on the form 0291 * <code>object:interface</code> (or 0292 * <code>object:interface[i]</code>) and <code>interface</code> 0293 * corresponds to an Reference (or RefVector) interface, the 0294 * corresponding referenced object is returned. (also 0295 * <code>object:interface:interface</code> is allowed etc.) 0296 */ 0297 static IBPtr TraceObject(string name); 0298 0299 /** 0300 * Return a string containing the name of the given class 0301 * description and its base classes, one on each line. 0302 */ 0303 static string GetInterfacedBaseClasses(const ClassDescriptionBase * cdb); 0304 0305 /** 0306 * Get an object. Decompose a string of the form 0307 * <code>object:interface</code> or 0308 * <code>object:vector-interface[pos]</code>. Retrun a pointer to 0309 * the corresponding <code>object</code>. 0310 */ 0311 static IBPtr getObjectFromNoun(string noun); 0312 //@} 0313 0314 /** @name Access references between object in the repository. */ 0315 //@{ 0316 /** 0317 * Get referring objects. Return all object which refers to the 0318 * given object through a Reference of RefVector interface. 0319 */ 0320 static IVector GetObjectsReferringTo(IBPtr); 0321 0322 /** 0323 * Get direct references. Return all objects the given object refers 0324 * to directly through a Reference of RefVector interface. 0325 */ 0326 static IVector DirectReferences(IBPtr); 0327 0328 /** 0329 * Get all references. If \a obj contains references to other objects, 0330 * either through a Reference or RefVector interface or through the 0331 * virtual getReferences member function, add these to refs. Do the 0332 * same to the references recursively. 0333 */ 0334 static void addReferences(tIBPtr obj, ObjectSet & refs); 0335 //@} 0336 0337 /** @name Access the interfaces of the objects in the repository. */ 0338 //@{ 0339 /** 0340 * Get interfaces. Return the interfaces defined for the 0341 * InterfacedBase class with the given type_info, \a ti, mapped to 0342 * their name. If several interfaces with the same name exists only 0343 * the one which correspond to the most derived class will be given, 0344 * except if \a all is true in which case all interfaces are given 0345 * (prefixed by '+'s to become unique). 0346 */ 0347 static InterfaceMap getInterfaces(const type_info & ti, bool all = true); 0348 0349 /** 0350 * Return an interface with the given \a name to the given \a object. 0351 */ 0352 static const InterfaceBase * FindInterface(IBPtr object, string name); 0353 0354 /** 0355 * Get an interface name. Decompose a string of the form 0356 * <code>object:interface</code> or 0357 * <code>object:vector-interface[pos]</code>. Return the interface 0358 * name (without the <code>[pos]</code>). 0359 */ 0360 static string getInterfaceFromNoun(string noun); 0361 0362 /** 0363 * Get interface index. Decompose a string of the form 0364 * <code>object:interface</code> or 0365 * <code>object:vector-interface[pos]</code>. Return the 0366 * <code>pos</code> part or empty string if not present. 0367 */ 0368 static string getPosArgFromNoun(string noun); 0369 0370 /** 0371 * Return a list of the interfaces which do not have their default 0372 * values for the given objects. 0373 */ 0374 template <typename Cont> 0375 static vector< pair<IBPtr, const InterfaceBase *> > 0376 getNonDefaultInterfaces(const Cont &); 0377 0378 //@} 0379 0380 /** @name Manipulate objects in the repository. */ 0381 //@{ 0382 /** 0383 * Call the InterfacedBase::update() function of all objects. 0384 */ 0385 static void update(); 0386 0387 /** 0388 * Clear the InterfacedBase::touched() flag in all objects in the 0389 * given container. 0390 */ 0391 template<typename Cont> 0392 static void clearAll(const Cont & c) 0393 { 0394 for_each(c, mem_fn(&InterfacedBase::clear)); 0395 } 0396 0397 /** 0398 * Set the status of all objects in the given container to 0399 * InterfacedBase::uninitialized. 0400 */ 0401 template<typename Cont> 0402 static void resetAll(const Cont & c) 0403 { 0404 for_each(c, mem_fn(&InterfacedBase::reset)); 0405 } 0406 0407 /** 0408 * Setup an object. Execute the InterfacedBase::readSetup() method 0409 * of \a ip with the stream \a is as argument. 0410 */ 0411 static void readSetup(tIBPtr ip, istream & is); 0412 0413 /** 0414 * Lock the given object. Locked objects cannot be 0415 * changed through an interface. 0416 */ 0417 static void lock(tIBPtr ip) { ip->lock(); } 0418 0419 /** 0420 * Unlock the given object. Locked objects cannot be changed through 0421 * an interface. 0422 */ 0423 static void unlock(tIBPtr ip) { ip->unlock(); } 0424 //@} 0425 0426 /** @name Access the documentation of objects. */ 0427 //@{ 0428 /** 0429 * Return the class documentation of a given object 0430 */ 0431 static const ClassDocumentationBase * getDocumentation(tcIBPtr ip); 0432 0433 /** 0434 * Get the description for the model implemented in the class of the 0435 * given object. 0436 */ 0437 static string getModelDescription(tcIBPtr ip); 0438 0439 /** 0440 * Get the references for the model implemented in the class of the 0441 * given object. 0442 */ 0443 static string getModelReferences(tcIBPtr ip); 0444 //@} 0445 0446 /** @name Manipulate the output streams of the repository. */ 0447 //@{ 0448 /** 0449 * Set the standard output stream 0450 */ 0451 static void cout(ostream & os) { coutp() = &os; } 0452 0453 /** 0454 * Get the standard output stream 0455 */ 0456 static ostream & cout() { return *coutp(); } 0457 0458 /** 0459 * Set the standard error stream 0460 */ 0461 static void cerr(ostream & os) { cerrp() = &os; } 0462 0463 /** 0464 * Get the standard error stream 0465 */ 0466 static ostream & cerr() { return *cerrp(); } 0467 0468 /** 0469 * Set the standard log stream 0470 */ 0471 static void clog(ostream & os) { clogp() = &os; } 0472 0473 /** 0474 * Get the standard log stream 0475 */ 0476 static ostream & clog() { return *clogp(); } 0477 //@} 0478 0479 protected: 0480 0481 /** @name Access standard InterfacedBase functions. */ 0482 //@{ 0483 /** 0484 * Return a clone of the given object. Calls the 0485 * InterfacedBase::clone() function of \a t and casts the resulting 0486 * pointer to the correct type. 0487 */ 0488 template <typename T> 0489 static typename Ptr<T>::pointer clone(const T & t); 0490 0491 /** 0492 * Return a clone of the given object. Calls the 0493 * InterfacedBase::fullclone() function of \a t and casts the 0494 * resulting pointer to the correct type. 0495 */ 0496 template <typename T> 0497 static typename Ptr<T>::pointer fullclone(const T & t); 0498 0499 /** 0500 * Rebind references. For all objects directly referenced by \a obj, 0501 * replace them with the translation found in \a trans. If \a obj has a 0502 * Reference or a member of a RefVector interface which is null, and 0503 * the corresponding interface has the RefInterfaceBase::defaultIfNull() flag set, 0504 * translate the null pointer to the first acceptable object in 0505 * defaults. 0506 */ 0507 static void rebind(InterfacedBase & obj, const TranslationMap & trans, 0508 const IVector & defaults); 0509 //@} 0510 0511 0512 /** 0513 * Add interfaces to the given map for the class with the given 0514 * class description. Recursively do the same with the base classes. 0515 */ 0516 static void addInterfaces(const ClassDescriptionBase &, 0517 InterfaceMap &, bool all = true); 0518 0519 /** @name Functions containing the static instances of objects used 0520 by the repository. */ 0521 //@{ 0522 /** 0523 * All InterfacedBase objects mapped to their name. 0524 */ 0525 static ObjectMap & objects(); 0526 0527 /** 0528 * All InterfacedBase objects. 0529 */ 0530 static ObjectSet & allObjects(); 0531 0532 /** 0533 * Sets of InterfaceBase objects mapped to the class description of 0534 * the class for which they are defined. 0535 */ 0536 static TypeInterfaceMap & interfaces(); 0537 0538 /** 0539 * Sets of ClassDocumentationBase objects mapped to the class 0540 * description of the class for which they are defined. 0541 */ 0542 static TypeDocumentationMap & documentations(); 0543 0544 /** 0545 * All defined directories. 0546 */ 0547 static DirectorySet & directories(); 0548 0549 /** 0550 * The current directory stack. 0551 */ 0552 static StringVector & directoryStack(); 0553 0554 /** 0555 * Flag to say if we are in the middle of an update procedure. 0556 */ 0557 static bool & updating(); 0558 0559 /** 0560 * The current current standard output stream. 0561 */ 0562 static ostream *& coutp(); 0563 0564 /** 0565 * The current current standard error stream. 0566 */ 0567 static ostream *& cerrp(); 0568 /** 0569 * The current current standard log stream. 0570 */ 0571 static ostream *& clogp(); 0572 //@} 0573 0574 }; 0575 0576 0577 } 0578 0579 #ifndef ThePEG_TEMPLATES_IN_CC_FILE 0580 #include "BaseRepository.tcc" 0581 #endif 0582 0583 #endif /* ThePEG_BaseRepository_H */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|