Skip to content

Commit 90954e2

Browse files
committed
[NFC][ESI Runtime] Ridding cosim backend of std namespace
Deleted `using namespace std` and fixing the resulting errors.
1 parent dac5c69 commit 90954e2

File tree

1 file changed

+51
-47
lines changed
  • lib/Dialect/ESI/runtime/cpp/lib/backends

1 file changed

+51
-47
lines changed

lib/Dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#include <iostream>
3030
#include <set>
3131

32-
using namespace std;
33-
3432
using namespace esi;
3533
using namespace esi::cosim;
3634
using namespace esi::services;
@@ -43,10 +41,11 @@ using grpc::ClientReaderWriter;
4341
using grpc::ClientWriter;
4442
using grpc::Status;
4543

46-
static void checkStatus(Status s, const string &msg) {
44+
static void checkStatus(Status s, const std::string &msg) {
4745
if (!s.ok())
48-
throw runtime_error(msg + ". Code " + to_string(s.error_code()) + ": " +
49-
s.error_message() + " (" + s.error_details() + ")");
46+
throw std::runtime_error(msg + ". Code " + to_string(s.error_code()) +
47+
": " + s.error_message() + " (" +
48+
s.error_details() + ")");
5049
}
5150

5251
/// Hack around C++ not having a way to forward declare a nested class.
@@ -56,25 +55,25 @@ struct esi::backends::cosim::CosimAccelerator::StubContainer {
5655
std::unique_ptr<ChannelServer::Stub> stub;
5756
};
5857

59-
/// Parse the connection string and instantiate the accelerator. Support the
60-
/// traditional 'host:port' syntax and a path to 'cosim.cfg' which is output by
61-
/// the cosimulation when it starts (which is useful when it chooses its own
58+
/// Parse the connection std::string and instantiate the accelerator. Support
59+
/// the traditional 'host:port' syntax and a path to 'cosim.cfg' which is output
60+
/// by the cosimulation when it starts (which is useful when it chooses its own
6261
/// port).
63-
unique_ptr<AcceleratorConnection>
64-
CosimAccelerator::connect(Context &ctxt, string connectionString) {
65-
string portStr;
66-
string host = "localhost";
62+
std::unique_ptr<AcceleratorConnection>
63+
CosimAccelerator::connect(Context &ctxt, std::string connectionString) {
64+
std::string portStr;
65+
std::string host = "localhost";
6766

6867
size_t colon;
69-
if ((colon = connectionString.find(':')) != string::npos) {
68+
if ((colon = connectionString.find(':')) != std::string::npos) {
7069
portStr = connectionString.substr(colon + 1);
7170
host = connectionString.substr(0, colon);
7271
} else if (connectionString.ends_with("cosim.cfg")) {
73-
ifstream cfg(connectionString);
74-
string line, key, value;
72+
std::ifstream cfg(connectionString);
73+
std::string line, key, value;
7574

7675
while (getline(cfg, line))
77-
if ((colon = line.find(":")) != string::npos) {
76+
if ((colon = line.find(":")) != std::string::npos) {
7877
key = line.substr(0, colon);
7978
value = line.substr(colon + 1);
8079
if (key == "port")
@@ -84,7 +83,7 @@ CosimAccelerator::connect(Context &ctxt, string connectionString) {
8483
}
8584

8685
if (portStr.size() == 0)
87-
throw runtime_error("port line not found in file");
86+
throw std::runtime_error("port line not found in file");
8887
} else if (connectionString == "env") {
8988
char *hostEnv = getenv("ESI_COSIM_HOST");
9089
if (hostEnv)
@@ -95,20 +94,21 @@ CosimAccelerator::connect(Context &ctxt, string connectionString) {
9594
if (portEnv)
9695
portStr = portEnv;
9796
else
98-
throw runtime_error("ESI_COSIM_PORT environment variable not set");
97+
throw std::runtime_error("ESI_COSIM_PORT environment variable not set");
9998
} else {
100-
throw runtime_error("Invalid connection string '" + connectionString + "'");
99+
throw std::runtime_error("Invalid connection string '" + connectionString +
100+
"'");
101101
}
102102
uint16_t port = stoul(portStr);
103103
return make_unique<CosimAccelerator>(ctxt, host, port);
104104
}
105105

106106
/// Construct and connect to a cosim server.
107-
CosimAccelerator::CosimAccelerator(Context &ctxt, string hostname,
107+
CosimAccelerator::CosimAccelerator(Context &ctxt, std::string hostname,
108108
uint16_t port)
109109
: AcceleratorConnection(ctxt) {
110110
// Connect to the simulation.
111-
auto channel = grpc::CreateChannel(hostname + ":" + to_string(port),
111+
auto channel = grpc::CreateChannel(hostname + ":" + std::to_string(port),
112112
grpc::InsecureChannelCredentials());
113113
rpcClient = new StubContainer(ChannelServer::NewStub(channel));
114114
}
@@ -165,7 +165,7 @@ class CosimSysInfo : public SysInfo {
165165
return response.esi_version();
166166
}
167167

168-
vector<uint8_t> getCompressedManifest() const override {
168+
std::vector<uint8_t> getCompressedManifest() const override {
169169
::esi::cosim::Manifest response = getManifest();
170170
std::string compressedManifestStr = response.compressed_manifest();
171171
return std::vector<uint8_t>(compressedManifestStr.begin(),
@@ -196,17 +196,19 @@ namespace {
196196
class WriteCosimChannelPort : public WriteChannelPort {
197197
public:
198198
WriteCosimChannelPort(ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
199-
const Type *type, string name)
199+
const Type *type, std::string name)
200200
: WriteChannelPort(type), rpcClient(rpcClient), desc(desc), name(name) {}
201201
~WriteCosimChannelPort() = default;
202202

203203
void connect() override {
204204
WriteChannelPort::connect();
205205
if (desc.type() != getType()->getID())
206-
throw runtime_error("Channel '" + name + "' has wrong type. Expected " +
207-
getType()->getID() + ", got " + desc.type());
206+
throw std::runtime_error("Channel '" + name +
207+
"' has wrong type. Expected " +
208+
getType()->getID() + ", got " + desc.type());
208209
if (desc.dir() != ChannelDesc::Direction::ChannelDesc_Direction_TO_SERVER)
209-
throw runtime_error("Channel '" + name + "' is not a to server channel");
210+
throw std::runtime_error("Channel '" + name +
211+
"' is not a to server channel");
210212
assert(desc.name() == name);
211213
}
212214

@@ -219,17 +221,17 @@ class WriteCosimChannelPort : public WriteChannelPort {
219221
VoidMessage response;
220222
grpc::Status sendStatus = rpcClient->SendToServer(&context, msg, &response);
221223
if (!sendStatus.ok())
222-
throw runtime_error("Failed to write to channel '" + name +
223-
"': " + sendStatus.error_message() +
224-
". Details: " + sendStatus.error_details());
224+
throw std::runtime_error("Failed to write to channel '" + name +
225+
"': " + sendStatus.error_message() +
226+
". Details: " + sendStatus.error_details());
225227
}
226228

227229
protected:
228230
ChannelServer::Stub *rpcClient;
229231
/// The channel description as provided by the server.
230232
ChannelDesc desc;
231233
/// The name of the channel from the manifest.
232-
string name;
234+
std::string name;
233235
};
234236
} // namespace
235237

@@ -241,7 +243,7 @@ class ReadCosimChannelPort
241243
public grpc::ClientReadReactor<esi::cosim::Message> {
242244
public:
243245
ReadCosimChannelPort(ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
244-
const Type *type, string name)
246+
const Type *type, std::string name)
245247
: ReadChannelPort(type), rpcClient(rpcClient), desc(desc), name(name),
246248
context(nullptr) {}
247249
virtual ~ReadCosimChannelPort() { disconnect(); }
@@ -250,10 +252,12 @@ class ReadCosimChannelPort
250252
// Sanity checking.
251253
ReadChannelPort::connect();
252254
if (desc.type() != getType()->getID())
253-
throw runtime_error("Channel '" + name + "' has wrong type. Expected " +
254-
getType()->getID() + ", got " + desc.type());
255+
throw std::runtime_error("Channel '" + name +
256+
"' has wrong type. Expected " +
257+
getType()->getID() + ", got " + desc.type());
255258
if (desc.dir() != ChannelDesc::Direction::ChannelDesc_Direction_TO_CLIENT)
256-
throw runtime_error("Channel '" + name + "' is not a to server channel");
259+
throw std::runtime_error("Channel '" + name +
260+
"' is not a to server channel");
257261
assert(desc.name() == name);
258262

259263
// Initiate a stream of messages from the server.
@@ -304,7 +308,7 @@ class ReadCosimChannelPort
304308
/// The channel description as provided by the server.
305309
ChannelDesc desc;
306310
/// The name of the channel from the manifest.
307-
string name;
311+
std::string name;
308312

309313
std::unique_ptr<ClientContext> context;
310314
/// Storage location for the incoming message.
@@ -315,31 +319,31 @@ class ReadCosimChannelPort
315319

316320
} // namespace
317321

318-
map<string, ChannelPort &>
322+
std::map<std::string, ChannelPort &>
319323
CosimAccelerator::requestChannelsFor(AppIDPath idPath,
320324
const BundleType *bundleType) {
321-
map<string, ChannelPort &> channelResults;
325+
std::map<std::string, ChannelPort &> channelResults;
322326

323327
// Find the client details for the port at 'fullPath'.
324328
auto f = clientChannelAssignments.find(idPath);
325329
if (f == clientChannelAssignments.end())
326330
return channelResults;
327-
const map<string, string> &channelAssignments = f->second;
331+
const std::map<std::string, std::string> &channelAssignments = f->second;
328332

329333
// Each channel in a bundle has a separate cosim endpoint. Find them all.
330334
for (auto [name, dir, type] : bundleType->getChannels()) {
331335
auto f = channelAssignments.find(name);
332336
if (f == channelAssignments.end())
333-
throw runtime_error("Could not find channel assignment for '" +
334-
idPath.toStr() + "." + name + "'");
335-
string channelName = f->second;
337+
throw std::runtime_error("Could not find channel assignment for '" +
338+
idPath.toStr() + "." + name + "'");
339+
std::string channelName = f->second;
336340

337341
// Get the endpoint, which may or may not exist. Construct the port.
338342
// Everything is validated when the client calls 'connect()' on the port.
339343
ChannelDesc chDesc;
340344
if (!getChannelDesc(channelName, chDesc))
341-
throw runtime_error("Could not find channel '" + channelName +
342-
"' in cosimulation");
345+
throw std::runtime_error("Could not find channel '" + channelName +
346+
"' in cosimulation");
343347

344348
ChannelPort *port;
345349
if (BundlePort::isWrite(dir)) {
@@ -358,7 +362,7 @@ CosimAccelerator::requestChannelsFor(AppIDPath idPath,
358362
/// Get the channel description for a channel name. Iterate through the list
359363
/// each time. Since this will only be called a small number of times on a small
360364
/// list, it's not worth doing anything fancy.
361-
bool CosimAccelerator::getChannelDesc(const string &channelName,
365+
bool CosimAccelerator::getChannelDesc(const std::string &channelName,
362366
ChannelDesc &desc) {
363367
ClientContext context;
364368
VoidMessage arg;
@@ -386,11 +390,11 @@ Service *CosimAccelerator::createService(Service::Type svcType,
386390
// Get the channel assignments for each client.
387391
for (auto client : clients) {
388392
AppIDPath fullClientPath = prefix + client.relPath;
389-
map<string, string> channelAssignments;
390-
for (auto assignment : any_cast<map<string, any>>(
393+
std::map<std::string, std::string> channelAssignments;
394+
for (auto assignment : any_cast<std::map<std::string, std::any>>(
391395
client.implOptions.at("channel_assignments")))
392396
channelAssignments[assignment.first] =
393-
any_cast<string>(assignment.second);
397+
std::any_cast<std::string>(assignment.second);
394398
clientChannelAssignments[fullClientPath] = std::move(channelAssignments);
395399
}
396400
}

0 commit comments

Comments
 (0)