From 699e2a478411f71ee7c5083aa6ec1f9a9037cfcb Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Fri, 9 Sep 2022 20:10:38 +0200 Subject: [PATCH] Consts and references --- file/include/logbook.h | 11 +++-- file/include/logbookEntry.h | 29 ++++--------- file/include/pathRecording.h | 6 +-- file/include/pathSegment.h | 4 +- file/logbook.cpp | 37 +++++----------- file/pathRecording.cpp | 2 +- simdata/gate.cpp | 2 +- simdata/include/gate.h | 10 ++--- simdata/include/runway.h | 6 +-- simdata/include/simdata.h | 16 ++++--- simdata/include/simulatorDatabase.hpp | 4 +- simdata/simdata.cpp | 59 ++++++++++++++----------- utilities/include/util.hpp | 3 +- websocket/include/types.h | 33 ++++++-------- websocket/websocket.cpp | 51 +++++++++++----------- xplugin/main.cpp | 62 +++++++++++++-------------- 16 files changed, 156 insertions(+), 179 deletions(-) diff --git a/file/include/logbook.h b/file/include/logbook.h index 4f483ff..290acf0 100644 --- a/file/include/logbook.h +++ b/file/include/logbook.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "constants.h" @@ -24,15 +25,19 @@ namespace germanairlinesva_logbook class Logbook { private: - std::vector file; + std::vector entries; void fromFile(const std::string &file); void readVersion1(std::ifstream &in); public: Logbook(); - void addEntry(LogbookEntry entry); - void toFile(); + + template inline void addEntry(Args &&...args) + { + this->entries.emplace_back(std::forward(args)...); + } + void toFile() const; }; } // namespace germanairlinesva_logbook diff --git a/file/include/logbookEntry.h b/file/include/logbookEntry.h index b7a6602..219a1b3 100644 --- a/file/include/logbookEntry.h +++ b/file/include/logbookEntry.h @@ -71,14 +71,14 @@ namespace germanairlinesva_logbook * STRLEN | STRING * Postamble (5) - * FLOAT32 | BITFIELD - * --------------+--- - * POINTS | FLAGS + * FLOAT | BITFIELD + * -------+--------- + * POINTS | FLAGS * Flags Bitfield - * 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 - * ---+---+---+---+---+---+---+----- - * NIL|NIL|NIL|NIL|NIL|NIL|NIL|FILED + * 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 + * ----+-----+-----+-----+-----+-----+-----+------ + * NIL | NIL | NIL | NIL | NIL | NIL | NIL | FILED */ class LogbookEntry { @@ -145,21 +145,8 @@ namespace germanairlinesva_logbook float points, std::uint8_t flags); - inline std::uint8_t *getBinaryData() { return file.data(); } - inline std::size_t getBinaryLength() { return file.size(); } - - std::string to_string() const - { - std::ostringstream str; - // TO STR - return str.str(); - } - - friend inline std::ostream &operator<<(std::ostream &os, - const LogbookEntry &gate) - { - return os << gate.to_string(); - } + inline const std::uint8_t *getBinaryData() const { return file.data(); } + inline std::size_t getBinaryLength() const { return file.size(); } }; } // namespace germanairlinesva_logbook diff --git a/file/include/pathRecording.h b/file/include/pathRecording.h index db21fd1..5243d33 100644 --- a/file/include/pathRecording.h +++ b/file/include/pathRecording.h @@ -28,10 +28,10 @@ namespace germanairlinesva_recording std::vector file{'V', 'G', 'A', 'R', '\0', 1}; public: - void addSegment(PathSegment segment); + void addSegment(const PathSegment &segment); - inline std::uint8_t *getBinaryData() { return file.data(); } - inline std::size_t getBinaryLength() { return file.size(); } + inline const std::uint8_t *getBinaryData() const { return file.data(); } + inline std::size_t getBinaryLength() const { return file.size(); } }; } // namespace germanairlinesva_recording diff --git a/file/include/pathSegment.h b/file/include/pathSegment.h index 0c7b09f..be07ec7 100644 --- a/file/include/pathSegment.h +++ b/file/include/pathSegment.h @@ -31,8 +31,8 @@ namespace germanairlinesva_recording std::uint16_t groundSpeed, struct germanairlinesva_geodata::point coordinates); - inline std::uint8_t *getBinaryData() { return file.data(); } - inline std::size_t getBinaryLength() { return file.size(); } + inline const std::uint8_t *getBinaryData() const { return file.data(); } + inline std::size_t getBinaryLength() const { return file.size(); } friend inline bool operator==(const PathSegment &lhs, const PathSegment &rhs) diff --git a/file/logbook.cpp b/file/logbook.cpp index cacb4cf..b6b8132 100644 --- a/file/logbook.cpp +++ b/file/logbook.cpp @@ -4,13 +4,8 @@ namespace germanairlinesva_logbook { Logbook::Logbook() { - if (germanairlinesva_util::fileExists( - XPLANE_PLUGIN_DIRECTORY LOGBOOK)) { + if (germanairlinesva_util::fileExists(XPLANE_PLUGIN_DIRECTORY LOGBOOK)) { this->fromFile(XPLANE_PLUGIN_DIRECTORY LOGBOOK); - } else { - char header[] = {'V', 'G', 'A', 'L', '\0', 1}; - this->file.resize(6); - std::memcpy(file.data(), &header, 6); } } @@ -32,30 +27,18 @@ namespace germanairlinesva_logbook } } - void Logbook::readVersion1(std::ifstream &in) - { - in.seekg(0, std::fstream::end); - std::streampos fileSize = in.tellg(); - in.seekg(0, std::ios::beg); + void Logbook::readVersion1(std::ifstream &in) {} - this->file.resize(fileSize); - in.read(reinterpret_cast(this->file.data()), fileSize); - in.close(); - } - void Logbook::addEntry(LogbookEntry entry) + void Logbook::toFile() const { - std::size_t size = file.size(); - file.resize(file.size() + entry.getBinaryLength()); - std::uint8_t *bufPtr = file.data() + size; - std::memcpy(bufPtr, entry.getBinaryData(), entry.getBinaryLength()); - } - - void Logbook::toFile() - { - std::ofstream out(XPLANE_PLUGIN_DIRECTORY LOGBOOK, - std::fstream::binary); - out.write(reinterpret_cast(this->file.data()), this->file.size()); + std::ofstream out(XPLANE_PLUGIN_DIRECTORY LOGBOOK, std::fstream::binary); + char header[] = {'V', 'G', 'A', 'L', '\0', 1}; + out.write(header, 6); + for (const LogbookEntry &entry : this->entries) { + out.write(reinterpret_cast(entry.getBinaryData()), + entry.getBinaryLength()); + } out.close(); } } // namespace germanairlinesva_logbook \ No newline at end of file diff --git a/file/pathRecording.cpp b/file/pathRecording.cpp index 22c5215..7de4d0e 100644 --- a/file/pathRecording.cpp +++ b/file/pathRecording.cpp @@ -2,7 +2,7 @@ namespace germanairlinesva_recording { - void PathRecording::addSegment(PathSegment segment) + void PathRecording::addSegment(const PathSegment &segment) { file.resize(file.size() + segment.getBinaryLength()); std::uint8_t *bufPtr = 6 + file.data() + count * segment.getBinaryLength(); diff --git a/simdata/gate.cpp b/simdata/gate.cpp index 72e81e3..67fae6a 100644 --- a/simdata/gate.cpp +++ b/simdata/gate.cpp @@ -28,7 +28,7 @@ namespace germanairlinesva_simdata // From database Gate::Gate(std::string designator, - germanairlinesva_geodata::point center, + struct germanairlinesva_geodata::point center, std::uint8_t radius) { this->designator = designator; diff --git a/simdata/include/gate.h b/simdata/include/gate.h index 4e8aab7..323d9e0 100644 --- a/simdata/include/gate.h +++ b/simdata/include/gate.h @@ -29,7 +29,7 @@ namespace germanairlinesva_simdata { private: std::string designator; - germanairlinesva_geodata::point center; + struct germanairlinesva_geodata::point center; std::uint8_t radius; std::vector file; @@ -41,13 +41,13 @@ namespace germanairlinesva_simdata std::uint8_t radius); // From database Gate(std::string designator, - germanairlinesva_geodata::point center, + struct germanairlinesva_geodata::point center, std::uint8_t radius); - inline std::uint8_t *getBinaryData() { return file.data(); } - inline std::size_t getBinaryLength() { return file.size(); } + inline const std::uint8_t *getBinaryData() const { return file.data(); } + inline std::size_t getBinaryLength() const { return file.size(); } - std::string to_string() const + inline const std::string to_string() const { std::ostringstream str; str << "Gate " << this->designator << " at " << this->center.latitude diff --git a/simdata/include/runway.h b/simdata/include/runway.h index 58d2163..fc8aa59 100644 --- a/simdata/include/runway.h +++ b/simdata/include/runway.h @@ -49,10 +49,10 @@ namespace germanairlinesva_simdata std::uint16_t length, std::uint16_t trueHeading); - inline std::uint8_t *getBinaryData() { return file.data(); } - inline std::size_t getBinaryLength() { return file.size(); } + inline const std::uint8_t *getBinaryData() const { return file.data(); } + inline std::size_t getBinaryLength() const { return file.size(); } - std::string to_string() const + inline const std::string to_string() const { std::ostringstream str; str << "Runway " << this->designator << " with bounds " diff --git a/simdata/include/simdata.h b/simdata/include/simdata.h index 9170e86..0db01d3 100644 --- a/simdata/include/simdata.h +++ b/simdata/include/simdata.h @@ -12,9 +12,9 @@ namespace germanairlinesva_simdata { int scan( - const char *defaultFile, - const char *sceneryPack, - const char *logFile, + const std::string defaultFile, + const std::string sceneryPack, + const std::string logFile, std::map, std::vector>> &airports); @@ -24,10 +24,12 @@ namespace germanairlinesva_simdata std::map, std::vector>> &airports, std::ofstream &logfile); - void makeGate15(std::vector *gates, std::vector fields); - void makeRunway(std::vector *runways, - std::vector fields); - void makeGate1300(std::vector *gates, std::vector fields); + void makeGate15(std::vector &gates, + const std::vector &fields); + void makeRunway(std::vector &runways, + const std::vector &fields); + void makeGate1300(std::vector &gates, + const std::vector &fields); } // namespace germanairlinesva_simdata diff --git a/simdata/include/simulatorDatabase.hpp b/simdata/include/simulatorDatabase.hpp index f167707..a1faa46 100644 --- a/simdata/include/simulatorDatabase.hpp +++ b/simdata/include/simulatorDatabase.hpp @@ -113,7 +113,7 @@ namespace germanairlinesva_simdata static_cast(calloc(designatorLength + 1, sizeof(char))); in.read(designator, designatorLength + 1); // Center - germanairlinesva_geodata::point center; + struct germanairlinesva_geodata::point center; in.read(reinterpret_cast(¢er), sizeof(center)); // Radius std::uint8_t radius; @@ -133,7 +133,7 @@ namespace germanairlinesva_simdata static_cast(calloc(designatorLength + 1, sizeof(char))); in.read(designator, designatorLength + 1); // Bounds - germanairlinesva_geodata::box bounds; + struct germanairlinesva_geodata::box bounds; in.read(reinterpret_cast(&bounds), sizeof(bounds)); // Width std::uint8_t width; diff --git a/simdata/simdata.cpp b/simdata/simdata.cpp index 2bc7396..7052306 100644 --- a/simdata/simdata.cpp +++ b/simdata/simdata.cpp @@ -3,9 +3,9 @@ namespace germanairlinesva_simdata { int scan( - const char *defaultFile, - const char *sceneryPack, - const char *logFile, + const std::string defaultFile, + const std::string sceneryPack, + const std::string logFile, std::map, std::vector>> &airports) { @@ -38,7 +38,7 @@ namespace germanairlinesva_simdata std::string path = germanairlinesva_util::rtrim_copy(line.substr(pos + 13)) + "Earth nav data/apt.dat"; - packs.push_back(path); + packs.emplace_back(path); } } std::reverse(packs.begin(), packs.end()); @@ -104,7 +104,7 @@ namespace germanairlinesva_simdata apCount += 1; logfile << "\t<" << kind << "> " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "15") { - makeGate15(&tmpGates, fields); + makeGate15(tmpGates, fields); logfile << "\t\t " << line << std::endl; } else if (fields[0] == "16" || fields[0] == "17") { // Write to file if ICAO is valid, and we have gates and runways @@ -122,10 +122,10 @@ namespace germanairlinesva_simdata currentIcao = nullptr; logfile << "\t<" << kind << " SKIPPED> " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "100") { - makeRunway(&tmpRunways, fields); + makeRunway(tmpRunways, fields); logfile << "\t\t " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "1300") { - makeGate1300(&tmpGates, fields); + makeGate1300(tmpGates, fields); logfile << "\t\t " << line << std::endl; } } @@ -139,7 +139,8 @@ namespace germanairlinesva_simdata << validCount << " are valid" << std::endl; } - void makeGate15(std::vector *gates, std::vector fields) + void makeGate15(std::vector &gates, + const std::vector &fields) { std::string gateName; for (size_t j = 4; j < fields.size() - 1; j++) { @@ -148,27 +149,31 @@ namespace germanairlinesva_simdata gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); - gates->push_back( - Gate{gateName, std::stod(fields[1]), std::stod(fields[2]), 40}); + gates.emplace_back(gateName, + std::stod(fields[1]), + std::stod(fields[2]), + 40); } - void makeRunway(std::vector *runways, std::vector fields) + void makeRunway(std::vector &runways, + const std::vector &fields) { - runways->push_back(Runway{fields[8], - std::stod(fields[9]), - std::stod(fields[10]), - std::stod(fields[18]), - std::stod(fields[19]), - std::stod(fields[1])}); - runways->push_back(Runway{fields[17], - std::stod(fields[18]), - std::stod(fields[19]), - std::stod(fields[9]), - std::stod(fields[10]), - std::stod(fields[1])}); + runways.emplace_back(fields[8], + std::stod(fields[9]), + std::stod(fields[10]), + std::stod(fields[18]), + std::stod(fields[19]), + std::stod(fields[1])); + runways.emplace_back(fields[17], + std::stod(fields[18]), + std::stod(fields[19]), + std::stod(fields[9]), + std::stod(fields[10]), + std::stod(fields[1])); } - void makeGate1300(std::vector *gates, std::vector fields) + void makeGate1300(std::vector &gates, + const std::vector &fields) { std::string gateName; for (size_t j = 6; j < fields.size() - 1; j++) { @@ -177,7 +182,9 @@ namespace germanairlinesva_simdata gateName += fields.back(); gateName = std::regex_replace(gateName, std::regex{","}, "0"); - gates->push_back( - Gate{gateName, std::stod(fields[1]), std::stod(fields[2]), 40}); + gates.emplace_back(gateName, + std::stod(fields[1]), + std::stod(fields[2]), + 40); } } // namespace germanairlinesva_simdata \ No newline at end of file diff --git a/utilities/include/util.hpp b/utilities/include/util.hpp index 0eda4a2..b5e431b 100644 --- a/utilities/include/util.hpp +++ b/utilities/include/util.hpp @@ -39,7 +39,6 @@ #include - namespace germanairlinesva_util { template @@ -293,7 +292,7 @@ namespace germanairlinesva_util std::string item; while (getline(ss, item, delim)) { - result.push_back(item); + result.emplace_back(item); } return result; diff --git a/websocket/include/types.h b/websocket/include/types.h index 7665eb5..b8bd8b4 100644 --- a/websocket/include/types.h +++ b/websocket/include/types.h @@ -3,12 +3,14 @@ #include -#pragma pack(push) /* push current alignment to stack */ -#pragma pack(1) /* set alignment to 1 byte boundary */ + namespace germanairlinesva_websocket { +#pragma pack(push) /* push current alignment to stack */ +#pragma pack(1) /* set alignment to 1 byte boundary */ + /* Structures and enums */ - typedef struct data { + struct data { std::int32_t pause = 0; float pBrake = 0; std::int32_t onGrnd = 0; @@ -27,30 +29,19 @@ namespace germanairlinesva_websocket float magHeading = 0; float payloadKg = 0; float totalWeightKg = 0; - } data; + }; - typedef enum commands { - PROCESS, - SAVE, - LOAD, - TEXT, - TIME, - UNPAUSE, - PAUSE, - PORT, - END - } commands; + enum commands { PROCESS, SAVE, LOAD, TEXT, TIME, UNPAUSE, PAUSE, PORT, END }; - typedef struct command_base { - commands type; - } command_base; + struct command_base { + enum commands type; + }; - - typedef struct command_port { + struct command_port { double latitude; double longitude; float trueHeading; - } command_port; + }; #pragma pack(pop) /* restore original alignment from stack */ } // namespace germanairlinesva_websocket diff --git a/websocket/websocket.cpp b/websocket/websocket.cpp index b3bac49..102ea55 100644 --- a/websocket/websocket.cpp +++ b/websocket/websocket.cpp @@ -12,19 +12,20 @@ namespace germanairlinesva_websocket ix::initNetSystem(); #endif - webSocket = new ix::WebSocket(); - webSocket->enableAutomaticReconnection(); - webSocket->setUrl(host); - webSocket->setOnMessageCallback([this](const ix::WebSocketMessagePtr &msg) { - this->onClientMessageCallback(msg); - }); - webSocket->start(); + this->webSocket = new ix::WebSocket(); + this->webSocket->enableAutomaticReconnection(); + this->webSocket->setUrl(host); + this->webSocket->setOnMessageCallback( + [this](const ix::WebSocketMessagePtr &msg) { + this->onClientMessageCallback(msg); + }); + this->webSocket->start(); } Websocket::~Websocket() { - webSocket->stop(); - toLog("WebSocket stopped"); + this->webSocket->stop(); + this->toLog("WebSocket stopped"); #ifdef IBM // Required on Windows ix::uninitNetSystem(); @@ -43,15 +44,15 @@ namespace germanairlinesva_websocket debug_msg << it.first << ": " << it.second << std::endl; } - toLog(debug_msg.str()); + this->toLog(debug_msg.str()); - webSocket->send("MASTER:" + user); - toLog("Connecting as " + user); + this->webSocket->send("MASTER:" + user); + this->toLog("Connecting as " + user); } else if (msg->type == ix::WebSocketMessageType::Close) { if (msg->closeInfo.reason.compare("DUPLICATE")) { - webSocket->disableAutomaticReconnection(); + this->webSocket->disableAutomaticReconnection(); - toLog("Disconnected due to beeing a duplicate simualtor"); + this->toLog("Disconnected due to beeing a duplicate simualtor"); } else { std::stringstream debug_msg; @@ -60,7 +61,7 @@ namespace germanairlinesva_websocket debug_msg << "Reason: " << msg->closeInfo.reason << std::endl; debug_msg << "Remote: " << msg->closeInfo.remote << std::endl; - toLog(debug_msg.str()); + this->toLog(debug_msg.str()); } } else if (msg->type == ix::WebSocketMessageType::Error) { std::stringstream debug_msg; @@ -73,20 +74,22 @@ namespace germanairlinesva_websocket debug_msg << "Retries: " << msg->errorInfo.retries << std::endl; debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl; - toLog(debug_msg.str()); + this->toLog(debug_msg.str()); } else if (msg->type == ix::WebSocketMessageType::Message) { if (!msg->str.empty()) { - toLog(msg->str); + this->toLog(msg->str); } } } void Websocket::sendData(data &d) { - if (strcmp(d.path, lastPath) != 0) { - strcpy(lastPath, d.path); - if (germanairlinesva_util::generateMD5(d.path, lastHash, toLog)) { - strcpy(lastHash, "NOT SET"); + if (strcmp(d.path, this->lastPath) != 0) { + strcpy(this->lastPath, d.path); + if (germanairlinesva_util::generateMD5(d.path, + this->lastHash, + this->toLog)) { + strcpy(this->lastHash, "NOT SET"); } } @@ -98,13 +101,13 @@ namespace germanairlinesva_websocket {"truHdg", d.truHdg}, {"totFuel", d.totFuelKg}, {"fuelFlow", d.ff}, - {"hash", lastHash}, + {"hash", this->lastHash}, }; - if (webSocket != nullptr) { + if (this->webSocket != nullptr) { std::ostringstream msg; msg << "SEND:" << user << ":" << json.dump(); - webSocket->send(msg.str(), false); + this->webSocket->send(msg.str(), false); } } } // namespace germanairlinesva_websocket \ No newline at end of file diff --git a/xplugin/main.cpp b/xplugin/main.cpp index 8a5280b..722ec18 100644 --- a/xplugin/main.cpp +++ b/xplugin/main.cpp @@ -46,7 +46,7 @@ XPLMDataRef pitch; XPLMDataRef roll; XPLMDataRef quaternion; -germanairlinesva_websocket::data toSend; +struct germanairlinesva_websocket::data toSend; germanairlinesva_recording::PathRecording p; /* @@ -171,35 +171,35 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) toLog("Logbook Test"); germanairlinesva_logbook::Logbook logbook; - logbook.addEntry({"08.09.2022", - "1000", - "L049", - "D-ALFA", - "John F. Kennedy International Aiport / EDDF", - "A1", - "14L", - "Gander International Airport / CYQX", - "10", - "03", - "10:00", - "10:20", - "13:20", - "13:30", - 210.5, - 20.1, - 5012.4156, - 8.87, - 5041.3856, - 7.1, - 971.14, - 2.41, - 980.65, - -165.23, - 1, - 1.2012, - "2022-09-08_VGA1000", - 5.5, - 1}); + logbook.addEntry("08.09.2022", + "1000", + "L049", + "D-ALFA", + "John F. Kennedy International Aiport / EDDF", + "A1", + "14L", + "Gander International Airport / CYQX", + "10", + "03", + "10:00", + "10:20", + "13:20", + "13:30", + 210.5, + 20.1, + 5012.4156, + 8.87, + 5041.3856, + 7.1, + 971.14, + 2.41, + 980.65, + -165.23, + 1, + 1.2012, + "2022-09-08_VGA1000", + 5.5, + 1); logbook.toFile(); return 1; @@ -281,7 +281,7 @@ void serverWorker() germanairlinesva_util::setThreadName("GAServerWorker"); while (!wantsExit) { - germanairlinesva_websocket::data copy; + struct germanairlinesva_websocket::data copy; { const std::lock_guard lock(mutex);