clang-tidy fixes, documentation, refactoring.

This commit is contained in:
2023-10-21 02:07:59 -07:00
parent 4e83da345f
commit aee337f2e9
26 changed files with 325 additions and 238 deletions

View File

@@ -30,11 +30,6 @@
#include <scmp/geom/Vector.h>
// coord2d.cpp contains 2D geometric functions and data structures, such as
// cartesian and polar coordinates and rotations.
// TODO: deprecate Point2D in favour of Vector
namespace scmp {
namespace geom {
@@ -60,7 +55,7 @@ Point2D::Point2D(const Polar2D &pol)
int
Point2D::X() const
{
return this->At(0);
return this->At(BasisX);
}
@@ -74,21 +69,21 @@ Point2D::X(int _x)
int
Point2D::Y() const
{
return this->At(1);
return this->At(BasisY);
}
void
Point2D::Y(int _y)
{
this->Set(1, _y);
this->Set(BasisY, _y);
}
std::ostream &
operator<<(std::ostream &outs, const Point2D &pt)
{
outs << "(" << std::to_string(pt[0]) << ", " << std::to_string(pt[1]) << ")";
outs << "(" << std::to_string(pt.X()) << ", " << std::to_string(pt.Y()) << ")";
return outs;
}
@@ -192,8 +187,8 @@ Polar2D::Theta(const double _theta)
void
Polar2D::ToPoint(Point2D &point)
{
point.Y(std::rint(std::sin(this->Theta()) * this->R()));
point.X(std::rint(std::cos(this->Theta()) * this->R()));
point.Y(static_cast<int>(std::rint(std::sin(this->Theta()) * this->R())));
point.X(static_cast<int>(std::rint(std::cos(this->Theta()) * this->R())));
}
@@ -232,4 +227,4 @@ operator<<(std::ostream &outs, const Polar2D &pol)
} // end namespace geom
} // end namespace math
} // end namespace scmp

View File

@@ -1,19 +0,0 @@
#include <cmath>
#include <scmp/Motion2D.h>
namespace scmp {
namespace basic {
scmp::geom::Vector2D
Acceleration(double speed, double heading)
{
auto dx = std::cos(heading) * speed;
auto dy = std::sin(heading) * speed;
return scmp::geom::Vector2D({dx, dy});
}
} // namespace basic
} // namespace phys

View File

@@ -1,3 +1,26 @@
///
/// \file src/scmp/geom/Orientation.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2017-06-05
/// \brief Orientation of vectors w.r.t. a reference plane, assumed to
/// be the Earth.
///
/// Copyright 2017 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <scmp/geom/Vector.h>
#include <scmp/geom/Orientation.h>
@@ -14,25 +37,25 @@ Heading2F(Vector2F vec)
float
Heading3f(Vector3F vec)
Heading3F(Vector3F vec)
{
Vector2F vec2f {vec[0], vec[1]};
const Vector2F vec2f {vec.At(BasisX), vec.At(BasisY)};
return Heading2F(vec2f);
}
double
Heading2d(Vector2D vec)
Heading2D(Vector2D vec)
{
return vec.Angle(Basis2D[BasisX]);
}
double
Heading3d(Vector3D vec)
Heading3D(Vector3D vec)
{
Vector2D vec2d {vec[0], vec[1]};
return Heading2d(vec2d);
const Vector2D vec2d {vec.At(BasisX), vec.At(BasisY)};
return Heading2D(vec2d);
}

View File

@@ -58,7 +58,7 @@ Flags::ParseStatusToString(ParseStatus status)
Flag *
NewFlag(std::string fName, FlagType fType, std::string fDescription)
{
auto flag = new Flag;
auto *flag = new Flag;
flag->Type = fType;
flag->WasSet = false;
@@ -71,9 +71,8 @@ NewFlag(std::string fName, FlagType fType, std::string fDescription)
Flags::Flags(std::string fName)
: name(std::move(fName)), description("")
{
}
: name(std::move(fName))
{}
Flags::Flags(std::string fName, std::string fDescription)
@@ -104,7 +103,7 @@ Flags::Register(std::string fName, FlagType fType, std::string fDescription)
return false;
}
auto flag = NewFlag(fName, fType, std::move(fDescription));
auto *flag = NewFlag(fName, fType, std::move(fDescription));
assert(flag != nullptr);
this->flags[fName] = flag;
return true;
@@ -194,7 +193,7 @@ Flags::Lookup(std::string fName)
bool
Flags::ValueOf(std::string fName, FlagValue &value)
{
if (this->flags.count(fName)) {
if (this->flags.count(fName) != 0U) {
return false;
}
@@ -207,7 +206,7 @@ Flags::ParseStatus
Flags::parseArg(int argc, char **argv, int &index)
{
std::string arg(argv[index]);
U::S::TrimWhitespace(arg);
string::TrimWhitespace(arg);
index++;
if (!std::regex_search(arg, isFlag)) {
@@ -221,7 +220,7 @@ Flags::parseArg(int argc, char **argv, int &index)
return ParseStatus::NotRegistered;
}
auto flag = flags[arg];
auto *flag = flags[arg];
if ((flag->Type != FlagType::Boolean) && index == argc) {
return ParseStatus::NotEnoughArgs;
}
@@ -233,11 +232,11 @@ Flags::parseArg(int argc, char **argv, int &index)
return ParseStatus::OK;
case FlagType::Integer:
flag->WasSet = true;
flag->Value.i = std::stoi(argv[++index], 0, 0);
flag->Value.i = std::stoi(argv[++index], nullptr, 0);
return ParseStatus::OK;
case FlagType::UnsignedInteger:
flag->WasSet = true;
flag->Value.u = static_cast<unsigned int>(std::stoi(argv[index++], 0, 0));
flag->Value.u = static_cast<unsigned int>(std::stoi(argv[index++], nullptr, 0));
return ParseStatus::OK;
case FlagType::String:
flag->WasSet = true;
@@ -275,7 +274,7 @@ Flags::Parse(int argc, char **argv, bool skipFirst)
case ParseStatus::EndOfFlags:
while (index < argc) {
this->args.push_back(std::string(argv[index]));
this->args.emplace_back(argv[index]);
index++;
}
continue;
@@ -303,7 +302,7 @@ Flags::Usage(std::ostream &os, int exitCode)
os << this->name << ":\t";
auto indent = this->name.size() + 7;
U::S::WriteTabIndented(os, description, 72 - indent, indent / 8, false);
string::WriteTabIndented(os, description, 72 - indent, indent / 8, false);
os << "\n\n";
for (const auto &pair : this->flags) {
@@ -337,7 +336,7 @@ Flags::Usage(std::ostream &os, int exitCode)
os << argLine;
indent = argLine.size();
U::S::WriteTabIndented(os, pair.second->Description,
string::WriteTabIndented(os, pair.second->Description,
72-indent, (indent/8)+2, false);
}
@@ -374,11 +373,11 @@ Flags::Arg(size_t i)
Flag *
Flags::checkGetArg(std::string& fName, FlagType eType)
{
if (this->flags[fName] == 0) {
if (this->flags[fName] == nullptr) {
return nullptr;
}
auto flag = this->flags[fName];
auto *flag = this->flags[fName];
if (flag == nullptr) {
return nullptr;
}
@@ -394,7 +393,7 @@ Flags::checkGetArg(std::string& fName, FlagType eType)
bool
Flags::GetBool(std::string fName, bool &flagValue)
{
auto flag = this->checkGetArg(fName, FlagType::Boolean);
auto *flag = this->checkGetArg(fName, FlagType::Boolean);
flagValue = flag->Value.b;
return flag->WasSet;
@@ -404,7 +403,7 @@ Flags::GetBool(std::string fName, bool &flagValue)
bool
Flags::GetInteger(std::string fName, int &flagValue)
{
auto flag = this->checkGetArg(fName, FlagType::Integer);
auto *flag = this->checkGetArg(fName, FlagType::Integer);
flagValue = flag->Value.i;
return flag->WasSet;
@@ -414,7 +413,7 @@ Flags::GetInteger(std::string fName, int &flagValue)
bool
Flags::GetUnsignedInteger(std::string fName, unsigned int &flagValue)
{
auto flag = this->checkGetArg(fName, FlagType::UnsignedInteger);
auto *flag = this->checkGetArg(fName, FlagType::UnsignedInteger);
flagValue = flag->Value.u;
return flag->WasSet;
@@ -424,7 +423,7 @@ Flags::GetUnsignedInteger(std::string fName, unsigned int &flagValue)
bool
Flags::GetSizeT(std::string fName, std::size_t &flagValue)
{
auto flag = this->checkGetArg(fName, FlagType::SizeT);
auto *flag = this->checkGetArg(fName, FlagType::SizeT);
flagValue = flag->Value.size;
return flag->WasSet;
@@ -434,7 +433,7 @@ Flags::GetSizeT(std::string fName, std::size_t &flagValue)
bool
Flags::GetString(std::string fName, std::string &flagValue)
{
auto flag = this->checkGetArg(fName, FlagType::String);
auto *flag = this->checkGetArg(fName, FlagType::String);
if (flag->Value.s == nullptr) {
return false;

View File

@@ -28,11 +28,7 @@
namespace scsl {
/// namespace U contains utilities.
namespace U {
/// namespace S contains string-related functions.
namespace S {
namespace string {
std::vector<std::string>
@@ -40,9 +36,11 @@ SplitKeyValuePair(std::string line, std::string delimiter)
{
auto pair = SplitN(std::move(line), std::move(delimiter), 2);
if (pair.size() == 0) {
if (pair.empty()) {
return {"", ""};
} else if (pair.size() == 1) {
}
if (pair.size() == 1) {
return {pair[0], ""};
}
@@ -61,7 +59,7 @@ SplitKeyValuePair(std::string line, char delimiter)
{
std::string sDelim;
sDelim.push_back(std::move(delimiter));
sDelim.push_back(delimiter);
return SplitKeyValuePair(std::move(line), sDelim);
}
@@ -72,7 +70,7 @@ TrimLeadingWhitespace(std::string &s)
s.erase(s.begin(),
std::find_if(s.begin(), s.end(),
[](unsigned char ch) {
return !std::isspace(ch);
return std::isspace(ch) == 0;
}));
}
@@ -81,7 +79,7 @@ void
TrimTrailingWhitespace(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
return std::isspace(ch) == 0;
}).base(), s.end());
}
@@ -125,9 +123,9 @@ SplitN(std::string s, std::string delim, size_t maxCount)
size_t ss = 0;
size_t se = 0;
for (ss = 0; s.size() != 0 && ss < s.size(); ss++) {
for (ss = 0; !s.empty() && ss < s.size(); ss++) {
se = s.find(delim, ss);
if ((maxCount > 0) && (parts.size() == maxCount - 1)) {
if ((maxCount > 0) && (parts.size() == (maxCount - 1))) {
se = s.size();
} else if (se == std::string::npos) {
se = s.size();
@@ -155,7 +153,7 @@ WrapText(std::string& line, size_t lineLength)
std::string wLine;
for (auto &word: parts) {
if (word.size() == 0) {
if (word.empty()) {
continue;
}
@@ -164,13 +162,13 @@ WrapText(std::string& line, size_t lineLength)
wLine.clear();
}
if (wLine.size() > 0) {
if (!wLine.empty()) {
wLine += " ";
}
wLine += word;
}
if (wLine.size() > 0) {
if (!wLine.empty()) {
wrapped.push_back(wLine);
}
@@ -182,7 +180,7 @@ void
WriteTabIndented(std::ostream &os, std::vector<std::string> lines,
int tabStop, bool indentFirst)
{
std::string indent(tabStop, '\t');
std::string const indent(tabStop, '\t');
for (size_t i = 0; i < lines.size(); i++) {
if (i > 0 || indentFirst) {
@@ -230,6 +228,5 @@ VectorToString(const std::vector<std::string> &svec)
}
} // namespace S
} // namespace U
} // namespace string
} // namespace scsl