-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Element.cpp
224 lines (178 loc) · 5.72 KB
/
Element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//==============================================================================
//
// Element.cpp
//
// Copyright (C) 2013-2022 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "Element.h"
#include <ostream>
#include "CfgBoolParm.h"
#include "CfgParmRegistry.h"
#include "CfgStrParm.h"
#include "Debug.h"
#include "Formatters.h"
#include "MainArgs.h"
#include "Singleton.h"
#include "SystemTime.h"
#include "SysTypes.h"
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
fixed_string DefaultElementName = "UnnamedElement";
//------------------------------------------------------------------------------
Element::Element()
{
Debug::ft("Element.ctor");
// Create our configuration parameters.
//
auto reg = Singleton<CfgParmRegistry>::Instance();
nameCfg_.reset(new CfgStrParm
("ElementName", DefaultElementName, "element's name"));
reg->BindParm(*nameCfg_);
runningInLabCfg_.reset(new CfgBoolParm
("RunningInLab", "T", "set if running in lab"));
reg->BindParm(*runningInLabCfg_);
}
//------------------------------------------------------------------------------
fn_name Element_dtor = "Element.dtor";
Element::~Element()
{
Debug::ftnt(Element_dtor);
Debug::SwLog(Element_dtor, UnexpectedInvocation, 0);
}
//------------------------------------------------------------------------------
const string Element::ConsoleFileName()
{
return "console" + to_string(SystemTime::TimeZero(), FullNumeric) + ".txt";
}
//------------------------------------------------------------------------------
void Element::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Protected::Display(stream, prefix, options);
stream << prefix << "name : " << Name() << CRLF;
stream << prefix << "RunningInLab : " << RunningInLab() << CRLF;
stream << prefix << "RscPath : " << RscPath() << CRLF;
stream << prefix << "HelpPath : " << HelpPath() << CRLF;
stream << prefix << "InputPath : " << InputPath() << CRLF;
stream << prefix << "OutputPath : " << OutputPath() << CRLF;
stream << prefix << "ConsoleFileName : " << ConsoleFileName() << CRLF;
stream << prefix << "nameCfg : " << strObj(nameCfg_.get()) << CRLF;
stream << prefix << "runningInLabCfg : "
<< strObj(runningInLabCfg_.get()) << CRLF;
}
//------------------------------------------------------------------------------
const string Element::HelpPath()
{
auto path = RscPath();
if(!path.empty())
{
path.push_back(PATH_SEPARATOR);
path.append("help");
}
return path;
}
//------------------------------------------------------------------------------
const string Element::InputPath()
{
auto path = RscPath();
if(!path.empty())
{
path.push_back(PATH_SEPARATOR);
path.append("input");
}
return path;
}
//------------------------------------------------------------------------------
bool Element::IsUnnamed()
{
Debug::ft("Element.IsUnnamed");
return (Name() == DefaultElementName);
}
//------------------------------------------------------------------------------
string Element::Name()
{
auto element = Singleton<Element>::Extant();
if(element == nullptr) return DefaultElementName;
return element->nameCfg_->CurrValue();
}
//------------------------------------------------------------------------------
const string Element::OutputPath()
{
auto path = RscPath();
if(!path.empty())
{
path.push_back(PATH_SEPARATOR);
path.append("excluded/output");
}
return path;
}
//------------------------------------------------------------------------------
void Element::Patch(sel_t selector, void* arguments)
{
Protected::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
const string Element::RscPath()
{
// Return the directory above the "src" directory on the path to the
// executable.
//
string path(MainArgs::At(0));
string dir("src");
dir.push_back(PATH_SEPARATOR);
auto pos = path.rfind(dir);
if(pos != string::npos)
{
path.erase(pos);
}
else
{
// A "src" directory was not found. Set RscDir to the executable's
// directory, though this is unlikely to work.
//
pos = path.rfind(PATH_SEPARATOR);
path.erase(pos);
}
return path;
}
//------------------------------------------------------------------------------
bool Element::RunningInLab()
{
// This might be invoked very early during initialization, so don't
// create this class until it is required for another purpose.
//
auto element = Singleton<Element>::Extant();
if((element != nullptr) && (element->runningInLabCfg_ != nullptr))
{
return element->runningInLabCfg_->CurrValue();
}
#ifndef FIELD_LOAD
return true;
#else
return false;
#endif
}
//------------------------------------------------------------------------------
string Element::strTimePlace()
{
return to_string(SystemTime::Now(), FullAlpha) + " on " + Name();
}
}