File indexing completed on 2025-01-18 09:58:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifndef G4GenericMessenger_hh
0035 #define G4GenericMessenger_hh 1
0036
0037 #include "G4AnyMethod.hh"
0038 #include "G4AnyType.hh"
0039 #include "G4ApplicationState.hh"
0040 #include "G4UIcommand.hh"
0041 #include "G4UImessenger.hh"
0042
0043 #include <map>
0044 #include <vector>
0045
0046 class G4UIdirectory;
0047
0048 class G4GenericMessenger : public G4UImessenger
0049 {
0050 public:
0051
0052 G4GenericMessenger(void* obj, const G4String& dir = "", const G4String& doc = "");
0053
0054
0055 ~G4GenericMessenger() override;
0056
0057
0058 G4String GetCurrentValue(G4UIcommand* command) override;
0059
0060
0061
0062 void SetNewValue(G4UIcommand* command, G4String newValue) override;
0063
0064 public:
0065 struct Command
0066 {
0067 enum UnitSpec
0068 {
0069 UnitCategory,
0070 UnitDefault
0071 };
0072 Command(G4UIcommand* cmd, const std::type_info& ti) : command(cmd), type(&ti) {}
0073 Command() = default;
0074
0075 Command& SetStates(G4ApplicationState s0)
0076 {
0077 command->AvailableForStates(s0);
0078 return *this;
0079 }
0080 Command& SetStates(G4ApplicationState s0, G4ApplicationState s1)
0081 {
0082 command->AvailableForStates(s0, s1);
0083 return *this;
0084 }
0085 Command& SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2)
0086 {
0087 command->AvailableForStates(s0, s1, s2);
0088 return *this;
0089 }
0090 Command& SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2,
0091 G4ApplicationState s3)
0092 {
0093 command->AvailableForStates(s0, s1, s2, s3);
0094 return *this;
0095 }
0096 Command& SetStates(G4ApplicationState s0, G4ApplicationState s1, G4ApplicationState s2,
0097 G4ApplicationState s3, G4ApplicationState s4)
0098 {
0099 command->AvailableForStates(s0, s1, s2, s3, s4);
0100 return *this;
0101 }
0102 Command& SetRange(const G4String& range)
0103 {
0104 command->SetRange(range.c_str());
0105 return *this;
0106 }
0107 Command& SetGuidance(const G4String& s0)
0108 {
0109 command->SetGuidance(s0);
0110 return *this;
0111 }
0112 Command& SetUnit(const G4String&, UnitSpec = UnitDefault);
0113 Command& SetUnitCategory(const G4String& u) { return SetUnit(u, UnitCategory); }
0114 Command& SetDefaultUnit(const G4String& u) { return SetUnit(u, UnitDefault); }
0115 Command& SetParameterName(const G4String&, G4bool, G4bool = false);
0116 Command& SetParameterName(G4int pIdx, const G4String&, G4bool, G4bool = false);
0117 Command& SetParameterName(const G4String&, const G4String&, const G4String&, G4bool,
0118 G4bool = false);
0119 Command& SetDefaultValue(const G4String&);
0120 Command& SetDefaultValue(G4int pIdx, const G4String&);
0121 Command& SetCandidates(const G4String&);
0122 Command& SetCandidates(G4int pIdx, const G4String&);
0123 Command& SetToBeBroadcasted(G4bool s0)
0124 {
0125 command->SetToBeBroadcasted(s0);
0126 return *this;
0127 }
0128 Command& SetToBeFlushed(G4bool s0)
0129 {
0130 command->SetToBeFlushed(s0);
0131 return *this;
0132 }
0133 Command& SetWorkerThreadOnly(G4bool s0)
0134 {
0135 command->SetWorkerThreadOnly(s0);
0136 return *this;
0137 }
0138
0139 G4UIcommand* command = nullptr;
0140 const std::type_info* type = nullptr;
0141 };
0142
0143 struct Property : public Command
0144 {
0145 Property(const G4AnyType& var, G4UIcommand* cmd)
0146 : Command(cmd, var.TypeInfo()), variable(var)
0147 {}
0148 Property() = default;
0149 G4AnyType variable;
0150 };
0151
0152 struct Method : public Command
0153 {
0154 Method(const G4AnyMethod& fun, void* obj, G4UIcommand* cmd)
0155 : Command(cmd, fun.ArgType()), method(fun), object(obj)
0156 {}
0157 Method() = default;
0158 G4AnyMethod method;
0159 void* object = nullptr;
0160 };
0161
0162
0163
0164 Command& DeclareProperty(const G4String& name, const G4AnyType& variable,
0165 const G4String& doc = "");
0166 Command& DeclarePropertyWithUnit(const G4String& name, const G4String& defaultUnit,
0167 const G4AnyType& variable, const G4String& doc = "");
0168 Command& DeclareMethod(const G4String& name, const G4AnyMethod& fun, const G4String& doc = "");
0169 Command& DeclareMethodWithUnit(const G4String& name, const G4String& defaultUnit,
0170 const G4AnyMethod& fun, const G4String& doc = "");
0171 void SetDirectory(const G4String& dir) { directory = dir; }
0172 void SetGuidance(const G4String& s);
0173 void Sort(G4bool val = true)
0174 {
0175 if (dircmd != nullptr) {
0176 dircmd->Sort(val);
0177 }
0178 }
0179
0180 private:
0181 std::map<G4String, Property> properties;
0182 std::map<G4String, Method> methods;
0183 G4UIdirectory* dircmd = nullptr;
0184 G4String directory;
0185 void* object = nullptr;
0186 };
0187
0188 #endif