EngineId.h
Go to the documentation of this file.
1 /**
2  * @file EngineId.h
3  * @brief An identifier for random engines
4  * @author Rob Kutschke (kutschke@fnal.gov)
5  *
6  * An identifier may consist of simply a module label or a module label plus an
7  * instance name.
8  */
9 
10 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_ENGINEID_H
11 #define NUTOOLS_RANDOMUTILS_PROVIDERS_ENGINEID_H 1
12 
13 #include <string>
14 #include <ostream>
15 
16 namespace rndm {
17 
18  /// Namespace for implementation details of SeedMaster
19  namespace SeedMasterHelper {
20 
21  /// Identifier for a engine, made of module name and optional instance name
22  struct EngineId {
23 
24  /// structure to identify a "global" flavour constructor
25  struct Global_t {};
26 
27  /// A constant to select a "global" flavour constructor
28  static Global_t global;
29 
30  /// Constructor (module name is required)
31  EngineId(std::string const& mod, std::string const& inst = std::string()):
32  moduleLabel(mod),
33  instanceName(inst)
34  {}
35 
36  /// Constructor (module name is required)
37  EngineId(std::string const& inst, Global_t):
38  moduleLabel(),
39  instanceName(inst)
40  {}
41 
42  // Accept compiler written d'tor, copy c'tor, copy and move assignments.
43 
44  /// Returns whether the label is "global" (no module context)
45  bool isGlobal() const { return moduleLabel.empty(); }
46 
47  /// Returns whether the instance label is defined
48  bool hasInstanceName() const { return !instanceName.empty(); }
49 
50  /// Sets this ID to the specified global instance
52  { moduleLabel.clear(); instanceName = inst; }
53 
54 
55  /// Returns true if both module and instance names match
56  bool operator== (EngineId const& rhs) const
57  {
58  if ( moduleLabel != rhs.moduleLabel ) return false;
59  if ( instanceName != rhs.instanceName ) return false;
60  return true;
61  } // operator== ()
62 
63  /// Lexicographic sort (module name first, then instance name)
64  bool operator< (EngineId const& rhs) const
65  {
66  if (moduleLabel < rhs.moduleLabel) return true;
67  if (moduleLabel == rhs.moduleLabel) {
68  if (instanceName < rhs.instanceName) return true;
69  }
70  return false;
71  } // operator< ()
72 
73  /// Converts the information in a module_name[.instance_name] string
74  operator std::string() const
75  {
77  if (hasInstanceName()) id.append(1, '.').append(instanceName);
78  return id;
79  } // operator std::string()
80 
81  /// Converts the information in a module_name:instance_name string
82  std::string artName() const { return moduleLabel + ':' + instanceName; }
83 
84  std::string moduleLabel; ///< module label
85  std::string instanceName; ///< instance name
86 
87  }; // end class EngineId
88 
89  inline std::ostream& operator<<
90  (std::ostream& ost, const EngineId& id )
91  { return ost << std::string(id); }
92 
93  } // end namespace SeedMasterHelper
94 
95 } // namespace sim
96 
97 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_ENGINEID_H
std::string artName() const
Converts the information in a module_name:instance_name string.
Definition: EngineId.h:82
std::string string
Definition: nybbler.cc:12
bool hasInstanceName() const
Returns whether the instance label is defined.
Definition: EngineId.h:48
std::string moduleLabel
module label
Definition: EngineId.h:84
structure to identify a "global" flavour constructor
Definition: EngineId.h:25
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
bool operator<(EngineId const &rhs) const
Lexicographic sort (module name first, then instance name)
Definition: EngineId.h:64
EngineId(std::string const &inst, Global_t)
Constructor (module name is required)
Definition: EngineId.h:37
EngineId(std::string const &mod, std::string const &inst=std::string())
Constructor (module name is required)
Definition: EngineId.h:31
void setGlobal(std::string inst)
Sets this ID to the specified global instance.
Definition: EngineId.h:51
std::string instanceName
instance name
Definition: EngineId.h:85
static Global_t global
A constant to select a "global" flavour constructor.
Definition: EngineId.h:28
bool isGlobal() const
Returns whether the label is "global" (no module context)
Definition: EngineId.h:45
bool operator==(EngineId const &rhs) const
Returns true if both module and instance names match.
Definition: EngineId.h:56