Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4UIcmdWithADoubleAndUnit.cc
이 파일의 문서화 페이지로 가기
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 //
27 // $Id: G4UIcmdWithADoubleAndUnit.cc 77308 2013-11-22 11:22:28Z gcosmo $
28 //
29 //
30 
32 #include "G4Tokenizer.hh"
33 #include "G4UnitsTable.hh"
34 #include "G4UIcommandStatus.hh"
35 #include <sstream>
36 #include <vector>
37 
39 (const char * theCommandPath,G4UImessenger * theMessenger)
40 :G4UIcommand(theCommandPath,theMessenger)
41 {
42  G4UIparameter * dblParam = new G4UIparameter('d');
43  SetParameter(dblParam);
44  G4UIparameter * untParam = new G4UIparameter('s');
45  SetParameter(untParam);
46  untParam->SetParameterName("Unit");
47 }
48 
50 {
51  std::vector<G4String> token_vector;
52  G4Tokenizer tkn(parameterList);
53  G4String str;
54  while( (str = tkn()) != "" ) {
55  token_vector.push_back(str);
56  }
57 
58  // convert a value in default unit
59  G4String converted_parameter;
60  G4String default_unit = GetParameter(1)-> GetDefaultValue();
61  if (default_unit != "" && token_vector.size() >= 2) {
62  if(CategoryOf(token_vector[1])!=CategoryOf(default_unit))
63  { return fParameterOutOfCandidates+1; }
64  G4double value_given = ValueOf(token_vector[1]);
65  G4double value_default = ValueOf(default_unit);
66  G4double value = ConvertToDouble(token_vector[0])
67  * value_given / value_default;
68  // reconstruct parameter list
69  converted_parameter += ConvertToString(value);
70  converted_parameter += " ";
71  converted_parameter += default_unit;
72  for ( size_t i=2 ; i< token_vector.size(); i++) {
73  converted_parameter += " ";
74  converted_parameter += token_vector[i];
75  }
76  } else {
77  converted_parameter = parameterList;
78  }
79 
80  return G4UIcommand::DoIt(converted_parameter);
81 }
82 
84 {
85  return ConvertToDimensionedDouble(paramString);
86 }
87 
89 {
90  G4double vl;
91  char unts[30];
92 
93  std::istringstream is(paramString);
94  is >> vl >> unts;
95 
96  return vl;
97 }
98 
100 {
101  G4double vl;
102  char unts[30];
103 
104  std::istringstream is(paramString);
105  is >> vl >> unts;
106  G4String unt = unts;
107 
108  return ValueOf(unt);
109 }
110 
112 {
113  G4UIparameter* unitParam = GetParameter(1);
114  G4String canList = unitParam->GetParameterCandidates();
115  G4Tokenizer candidateTokenizer(canList);
116  G4String aToken = candidateTokenizer();
117  std::ostringstream os;
118  os << G4BestUnit(val,CategoryOf(aToken));
119 
120  G4String st = os.str();
121  return st;
122 }
123 
125 {
126  G4UIparameter* unitParam = GetParameter(1);
127  G4String st;
128  if(unitParam->IsOmittable())
129  { st = ConvertToString(val,unitParam->GetDefaultValue()); }
130  else
131  { st = ConvertToStringWithBestUnit(val); }
132  return st;
133 }
134 
136 (const char * theName,G4bool omittable,G4bool currentAsDefault)
137 {
138  G4UIparameter * theParam = GetParameter(0);
139  theParam->SetParameterName(theName);
140  theParam->SetOmittable(omittable);
141  theParam->SetCurrentAsDefault(currentAsDefault);
142 }
143 
145 {
146  G4UIparameter * theParam = GetParameter(0);
147  theParam->SetDefaultValue(defVal);
148 }
149 
150 void G4UIcmdWithADoubleAndUnit::SetUnitCategory(const char * unitCategory)
151 {
152  SetUnitCandidates(UnitsList(unitCategory));
153 }
154 
155 void G4UIcmdWithADoubleAndUnit::SetUnitCandidates(const char * candidateList)
156 {
157  G4UIparameter * untParam = GetParameter(1);
158  G4String canList = candidateList;
159  untParam->SetParameterCandidates(canList);
160 }
161 
163 {
164  G4UIparameter * untParam = GetParameter(1);
165  untParam->SetOmittable(true);
166  untParam->SetDefaultValue(defUnit);
167  SetUnitCategory(CategoryOf(defUnit));
168 }
169 
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:311
static G4String CategoryOf(const char *unitName)
Definition: G4UIcommand.cc:318
void SetCurrentAsDefault(G4bool val)
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:457
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
virtual G4int DoIt(G4String parameterList)
Definition: G4UIcommand.cc:125
G4String GetDefaultValue() const
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
G4String GetParameterCandidates() const
void SetDefaultValue(const char *theDefaultValue)
static G4double GetNewDoubleValue(const char *paramString)
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual G4int DoIt(G4String parameterList)
void SetUnitCategory(const char *unitCategory)
static G4double GetNewDoubleRawValue(const char *paramString)
G4String ConvertToStringWithDefaultUnit(G4double val)
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:374
int G4int
Definition: G4Types.hh:78
void SetParameterName(const char *theName)
G4bool IsOmittable() const
static G4String UnitsList(const char *unitCategory)
Definition: G4UIcommand.cc:323
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:465
G4String ConvertToStringWithBestUnit(G4double val)
void SetParameterCandidates(const char *theString)
G4UIcmdWithADoubleAndUnit(const char *theCommandPath, G4UImessenger *theMessenger)
void SetOmittable(G4bool om)
void SetUnitCandidates(const char *candidateList)
void SetDefaultUnit(const char *defUnit)
static G4double GetNewUnitValue(const char *paramString)