Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4GenericMessenger.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4GenericMessenger
27//
28// Class description:
29//
30// A generic messenger class.
31
32// Author: P.Mato, CERN - 27 September 2012
33// --------------------------------------------------------------------
34#ifndef G4GenericMessenger_hh
35#define G4GenericMessenger_hh 1
36
37#include "G4UImessenger.hh"
38#include "G4UIcommand.hh"
39#include "G4AnyType.hh"
40#include "G4AnyMethod.hh"
41#include "G4ApplicationState.hh"
42
43#include <map>
44#include <vector>
45
46class G4UIdirectory;
47
49{
50 public:
51
52 G4GenericMessenger(void* obj, const G4String& dir = "",
53 const G4String& doc = "");
54 // Contructor
55
56 ~G4GenericMessenger() override;
57 // Destructor
58
59 G4String GetCurrentValue(G4UIcommand* command) override;
60 // The concrete, but generic implementation of this method.
61
62 void SetNewValue(G4UIcommand* command, G4String newValue) override;
63 // The concrete, generic implementation of this method converts
64 // the string "newValue" to action.
65
66 public:
67
68 struct Command
69 {
71 {
74 };
75 Command(G4UIcommand* cmd, const std::type_info& ti)
76 : command(cmd)
77 , type(&ti)
78 {}
79 Command() = default;
80
82 {
84 return *this;
85 }
87 {
89 return *this;
90 }
93 {
94 command->AvailableForStates(s0, s1, s2);
95 return *this;
96 }
99 {
100 command->AvailableForStates(s0, s1, s2, s3);
101 return *this;
102 }
106 {
107 command->AvailableForStates(s0, s1, s2, s3, s4);
108 return *this;
109 }
110 Command& SetRange(const G4String& range)
111 {
112 command->SetRange(range.c_str());
113 return *this;
114 }
116 {
117 command->SetGuidance(s0);
118 return *this;
119 }
122 {
123 return SetUnit(u, UnitCategory);
124 }
126 {
127 return SetUnit(u, UnitDefault);
128 }
129 Command& SetParameterName(const G4String&, G4bool, G4bool = false);
130 Command& SetParameterName(G4int pIdx, const G4String&, G4bool, G4bool = false);
131 Command& SetParameterName(const G4String&, const G4String&, const G4String&,
132 G4bool, G4bool = false);
134 Command& SetDefaultValue(G4int pIdx, const G4String&);
136 Command& SetCandidates(G4int pIdx, const G4String&);
138 {
140 return *this;
141 }
143 {
145 return *this;
146 }
148 {
150 return *this;
151 }
152
154 const std::type_info* type = nullptr;
155 };
156
157 struct Property : public Command
158 {
160 : Command(cmd, var.TypeInfo())
161 , variable(var)
162 {}
163 Property() = default;
165 };
166
167 struct Method : public Command
168 {
169 Method(const G4AnyMethod& fun, void* obj, G4UIcommand* cmd)
170 : Command(cmd, fun.ArgType())
171 , method(fun)
172 , object(obj)
173 {}
174 Method() = default;
176 void* object = nullptr;
177 };
178
179 // Declare Methods
180
181 Command& DeclareProperty(const G4String& name, const G4AnyType& variable,
182 const G4String& doc = "");
184 const G4String& defaultUnit,
185 const G4AnyType& variable,
186 const G4String& doc = "");
187 Command& DeclareMethod(const G4String& name, const G4AnyMethod& fun,
188 const G4String& doc = "");
190 const G4String& defaultUnit,
191 const G4AnyMethod& fun,
192 const G4String& doc = "");
193 void SetDirectory(const G4String& dir) { directory = dir; }
194 void SetGuidance(const G4String& s);
195 void Sort(G4bool val = true)
196 {
197 if(dircmd != nullptr)
198 {
199 dircmd->Sort(val);
200 }
201 }
202
203 private:
204
205 std::map<G4String, Property> properties;
206 std::map<G4String, Method> methods;
207 G4UIdirectory* dircmd = nullptr;
208 G4String directory;
209 void* object = nullptr;
210};
211
212#endif
G4ApplicationState
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
Command & DeclareMethod(const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
void SetGuidance(const G4String &s)
~G4GenericMessenger() override
void Sort(G4bool val=true)
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Command & DeclareMethodWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyMethod &fun, const G4String &doc="")
void SetDirectory(const G4String &dir)
void SetNewValue(G4UIcommand *command, G4String newValue) override
G4String GetCurrentValue(G4UIcommand *command) override
Command & DeclarePropertyWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyType &variable, const G4String &doc="")
void SetToBeBroadcasted(G4bool val)
Definition: G4UIcommand.hh:172
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:157
void SetToBeFlushed(G4bool val)
Definition: G4UIcommand.hh:174
void SetRange(const char *rs)
Definition: G4UIcommand.hh:121
void SetWorkerThreadOnly(G4bool val=true)
Definition: G4UIcommand.hh:176
void AvailableForStates(G4ApplicationState s1)
Definition: G4UIcommand.cc:287
void Sort(G4bool val=true)
Command & SetUnitCategory(const G4String &u)
Command & SetParameterName(const G4String &, G4bool, G4bool=false)
Command & SetStates(G4ApplicationState s0)
Command & SetDefaultUnit(const G4String &u)
Command & SetRange(const G4String &range)
Command & SetToBeFlushed(G4bool s0)
Command & SetWorkerThreadOnly(G4bool s0)
Command & SetCandidates(const G4String &)
Command & SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2)
Command & SetGuidance(const G4String &s0)
Command & SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2, G4ApplicationState s3, G4ApplicationState s4)
Command & SetDefaultValue(const G4String &)
const std::type_info * type
Command(G4UIcommand *cmd, const std::type_info &ti)
Command & SetToBeBroadcasted(G4bool s0)
Command & SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2, G4ApplicationState s3)
Command & SetStates(G4ApplicationState s0, G4ApplicationState s1)
Command & SetUnit(const G4String &, UnitSpec=UnitDefault)
Method(const G4AnyMethod &fun, void *obj, G4UIcommand *cmd)
Property(const G4AnyType &var, G4UIcommand *cmd)