Skip to content

Commit 881e26b

Browse files
committed
Started using Result to replace std::string.
1 parent 0044843 commit 881e26b

File tree

8 files changed

+141
-27
lines changed

8 files changed

+141
-27
lines changed

.idea/editor.xml

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/factors.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace steppable::__internals::numUtils
4646
* @param[in] base The base of the root.
4747
* @return A result object containing the largest root factor of the number.
4848
*/
49-
ResultBool getRootFactor(const std::string& _number, const std::string& base = "2");
49+
ResultBool<std::string> getRootFactor(const std::string& _number, const std::string& base = "2");
5050

5151
/**
5252
* @brief Get the greatest root number less than or equal to the given number.
@@ -73,5 +73,5 @@ namespace steppable::__internals::numUtils
7373
* @return StatusBool::CALCULATED_SIMPLIFIED_YES if the number is a root number,
7474
* StatusBool::CALCULATED_SIMPLIFIED_NO otherwise.
7575
*/
76-
ResultBool isRoot(const std::string& _number, const std::string& base);
76+
ResultBool<std::string> isRoot(const std::string& _number, const std::string& base);
7777
} // namespace steppable::__internals::numUtils

include/types/data.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "util.hpp"
6+
7+
using namespace std::literals;
8+
using namespace steppable::__internals::utils;
9+
10+
namespace steppable
11+
{
12+
template<typename BaseT, StringLiteral BaseTName>
13+
class Data
14+
{
15+
BaseT value;
16+
17+
public:
18+
std::string getDataType() { return BaseTName.value; }
19+
20+
Data(const BaseT object) : value(object) {} // NOLINT(*-explicit-constructor)
21+
Data() : value() {}
22+
23+
Data& operator=(const BaseT& object)
24+
{
25+
value = object;
26+
return *this;
27+
}
28+
};
29+
30+
enum class _Weekday : std::uint8_t
31+
{
32+
Sunday = 0,
33+
Monday = 1,
34+
Tuesday = 2,
35+
Wednesday = 3,
36+
Thursday = 4,
37+
Friday = 5,
38+
Saturday = 6,
39+
};
40+
41+
using Weekday = Data<_Weekday, StringLiteral{"Weekday"}>;
42+
} // namespace steppable

include/types/result.hpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222

2323
#pragma once
2424

25+
#include <output.hpp>
26+
#include <platform.hpp>
2527
#include <string>
28+
#include <util.hpp>
2629
#include <utility>
2730
#include <vector>
2831

32+
using namespace std::literals;
33+
using namespace steppable::__internals::utils;
34+
2935
/**
3036
* @namespace steppable::types
3137
* @brief The namespace containing types used in the steppable calculator.
@@ -35,7 +41,7 @@ namespace steppable::types
3541
/**
3642
* @brief The status of the calculation.
3743
*/
38-
enum class Status
44+
enum class Status : std::uint8_t
3945
{
4046
CALCULATED_SIMPLIFIED,
4147
CALCULATED_UNSIMPLIFIED,
@@ -45,7 +51,7 @@ namespace steppable::types
4551
/**
4652
* @brief The status of a boolean calculation.
4753
*/
48-
enum class StatusBool
54+
enum class StatusBool : std::uint8_t
4955
{
5056
CALCULATED_SIMPLIFIED_YES,
5157
CALCULATED_SIMPLIFIED_NO,
@@ -60,18 +66,21 @@ namespace steppable::types
6066
*
6167
* @tparam StatusType The type of the status of the calculation.
6268
*/
63-
template<typename StatusType>
69+
template<typename StatusType, typename ResultT, StringLiteral ResultTName>
6470
class ResultBase
6571
{
66-
private:
6772
/// @brief Whether the calculation is done.
6873
StatusType done;
6974

7075
/// @brief The inputs to the calculation.
7176
std::vector<std::string> inputs;
7277

7378
/// @brief The output of the calculation.
74-
std::string out;
79+
std::vector<std::string> outputs;
80+
81+
/// @brief The result of the calculation
82+
/// @attention This is different from `inputs`, as it stores only the result of the operation.
83+
ResultT result;
7584

7685
public:
7786
ResultBase() = delete;
@@ -81,26 +90,42 @@ namespace steppable::types
8190
*
8291
* @param[in] _inputs The inputs to the calculation.
8392
* @param[in] _out The output of the calculation.
93+
* @param result The result of the calculation.
8494
* @param[in] _done A flag indicating how the calculation is done.
8595
*/
86-
ResultBase(const std::vector<std::string>& _inputs, std::string _out, StatusType _done) :
87-
done(_done), inputs(_inputs), out(std::move(_out))
96+
ResultBase(const std::vector<std::string>& _inputs,
97+
std::vector<std::string> _out,
98+
ResultT result,
99+
StatusType _done) :
100+
done(_done), inputs(_inputs), outputs(std::move(_out)), result(result)
88101
{
89102
}
90103

91104
/// @brief Gets how the calculation is done.
92105
StatusType getStatus() const { return done; }
93106

107+
ResultT getResult() const { return result; }
108+
94109
/// @brief Gets the output of the calculation.
95-
std::string getOutput() const { return out; }
110+
[[nodiscard("Output should be used")]] std::string getOutput(size_t idx = 0) const
111+
{
112+
if (idx >= outputs.size())
113+
{
114+
output::error("getOutput"s, "Output index out of range"s);
115+
programSafeExit(1);
116+
}
117+
return outputs[idx];
118+
}
96119

97120
/// @brief Gets the inputs to the calculation.
98-
std::vector<std::string> getInputs() const { return inputs; }
121+
[[nodiscard("Inputs should be used")]] std::vector<std::string> getInputs() const { return inputs; }
99122
};
100123

101124
/// @brief An alias for a result of a calculation. This represents a calculation with a `Status` status.
102-
using Result = ResultBase<Status>;
125+
template<typename ResultT>
126+
using Result = ResultBase<Status, ResultT, StringLiteral{ "str" }>;
103127

104128
/// @brief An alias for a result of a boolean calculation.
105-
using ResultBool = ResultBase<StatusBool>;
129+
template<typename ResultT>
130+
using ResultBool = ResultBase<StatusBool, ResultT, StringLiteral{ "bool" }>;
106131
} // namespace steppable::types

include/util.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ namespace steppable::__internals::utils
213213
programSafeExit(1);
214214
}
215215
}
216+
217+
/**
218+
* @brief String literal workaround for templates.
219+
* @tparam N The length of the string
220+
*/
221+
template<size_t N>
222+
struct StringLiteral
223+
{
224+
/**
225+
* @brief Initializes the wrapper class.
226+
* @param str The string literal for the template.
227+
*/
228+
constexpr StringLiteral(const char (&str)[N]) // NOLINT(*-pro-type-member-init, *-explicit-constructor)
229+
{ // NOLINT(*-avoid-c-arrays)
230+
std::copy_n(str, N, value);
231+
}
232+
233+
/**
234+
* @brief The value of the wrapper class. Stores the string object.
235+
*/
236+
char value[N]; // NOLINT(*-avoid-c-arrays)
237+
};
216238
} // namespace steppable::__internals::utils
217239

218240
/**
@@ -550,4 +572,18 @@ namespace steppable::__internals::stringUtils
550572
* @return A vector of strings containing the duplicates.
551573
*/
552574
std::vector<std::string> duplicates(const std::vector<std::string>& vector);
575+
576+
template<typename ValueType>
577+
std::string vectorToString(const std::vector<ValueType>& vector)
578+
{
579+
std::ostringstream out;
580+
out << "[";
581+
for (size_t i = 0; i < vector.size(); ++i)
582+
{
583+
out << vector[i];
584+
if (i != vector.size() - 1)
585+
out << ", ";
586+
}
587+
return out.str();
588+
}
553589
} // namespace steppable::__internals::stringUtils

src/calc/abs/abs.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,25 @@
3535

3636
#include <fn/calc.hpp>
3737
#include <iostream>
38+
#include <number.hpp>
3839
#include <string>
40+
#include <types/result.hpp>
3941

4042
using namespace steppable::__internals::utils;
4143
using namespace steppable::__internals::calc;
4244
using namespace steppable::localization;
4345

4446
namespace steppable::__internals::calc
4547
{
46-
std::string abs(const std::string& _number, const int steps)
48+
types::Result<Number> _abs(const std::string& _number)
4749
{
48-
std::string number = static_cast<std::string>(_number);
49-
return reportAbs(number, steps);
50+
const auto result = reportAbs(_number, 0);
51+
const std::vector outputs = { result, reportAbs(_number, 1), reportAbs(_number, 2) };
52+
const std::vector inputs = { _number };
53+
return { inputs, outputs, Number(result), types::Status::CALCULATED_UNSIMPLIFIED };
5054
}
55+
56+
std::string abs(const std::string& _number, const int steps) { return _abs(_number).getOutput(steps); }
5157
} // namespace steppable::__internals::calc
5258

5359
#ifndef NO_MAIN

src/factors.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ namespace steppable::__internals::numUtils
5454
return factors;
5555
}
5656

57-
ResultBool getRootFactor(const std::string& _number, const std::string& base)
57+
ResultBool<std::string> getRootFactor(const std::string& _number, const std::string& base)
5858
{
5959
auto factors = getFactors(_number);
6060
// Reverse the factors
61-
std::reverse(factors.begin(), factors.end());
61+
std::ranges::reverse(factors);
6262
// Get the largest root factor
6363
for (const auto& factor : factors)
6464
{
6565
auto rootResult = isRoot(factor, base);
6666
if (rootResult.getStatus() == StatusBool::CALCULATED_SIMPLIFIED_YES)
67-
return { { _number, base, rootResult.getOutput() }, factor, StatusBool::CALCULATED_SIMPLIFIED_YES };
67+
return {
68+
{ _number, base, rootResult.getOutput() }, { factor }, factor, StatusBool::CALCULATED_SIMPLIFIED_YES
69+
};
6870
}
6971
// The number has no root factors
70-
return { { _number, base }, "1", StatusBool::CALCULATED_SIMPLIFIED_YES };
72+
return { { _number, base }, { "1" }, { "1" }, StatusBool::CALCULATED_SIMPLIFIED_YES };
7173
}
7274

7375
std::string getGreatestRootNum(const std::string& _number, const std::string& base)
@@ -82,12 +84,12 @@ namespace steppable::__internals::numUtils
8284
return factors.size() == 2; // Only 1 and the number itself are factors ==> prime!
8385
}
8486

85-
ResultBool isRoot(const std::string& _number, const std::string& base)
87+
ResultBool<std::string> isRoot(const std::string& _number, const std::string& base)
8688
{
8789
auto iRoot = rootIntPart(_number, base);
8890
auto rootNum = power(iRoot, base, 0);
8991
if (compare(rootNum, _number, 0) == "2")
90-
return { { _number, base }, iRoot, StatusBool::CALCULATED_SIMPLIFIED_YES };
91-
return { { _number, base }, "1", StatusBool::CALCULATED_SIMPLIFIED_NO };
92+
return { { _number, base }, { iRoot }, { iRoot }, StatusBool::CALCULATED_SIMPLIFIED_YES };
93+
return { { _number, base }, { "1" }, { "1" }, StatusBool::CALCULATED_SIMPLIFIED_NO };
9294
}
9395
} // namespace steppable::__internals::numUtils

0 commit comments

Comments
 (0)