Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Operand.cpp
Go to the documentation of this file.
1/**
2 * @file Operand.cpp
3 * @brief Implementation of Operand — construction, helpers, and serialization.
4 * @author Olympe Engine
5 * @date 2026-03-16
6 *
7 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
8 */
9
10#include "Operand.h"
11
12#include <stdexcept>
13#include <string>
14
15namespace Olympe {
16
17// ---------------------------------------------------------------------------
18// Default constructor
19// ---------------------------------------------------------------------------
20
22 : mode(OperandMode::Variable)
23 , stringValue("")
24 , constValue(0.0)
25{
26}
27
28// ---------------------------------------------------------------------------
29// Factory methods
30// ---------------------------------------------------------------------------
31
32/*static*/
34{
35 Operand op;
38 op.constValue = 0.0;
39 return op;
40}
41
42/*static*/
44{
45 Operand op;
48 op.stringValue = "";
49 return op;
50}
51
52/*static*/
54{
55 Operand op;
58 op.constValue = 0.0;
59 return op;
60}
61
62// ---------------------------------------------------------------------------
63// Helpers
64// ---------------------------------------------------------------------------
65
66std::string Operand::GetDisplayString() const
67{
68 switch (mode)
69 {
71 return "[" + stringValue + "]";
72
74 {
75 // Format without trailing zeros when the value is a whole number.
76 // e.g. 2.0 -> "2", 2.5 -> "2.5"
77 if (constValue == static_cast<double>(static_cast<long long>(constValue)))
78 {
79 // Whole number — omit decimal point.
80 std::string s = std::to_string(static_cast<long long>(constValue));
81 return "[" + s + "]";
82 }
83 // Has fractional part — use default float representation.
84 std::string s = std::to_string(constValue);
85 // Trim trailing zeros after decimal point.
86 size_t dot = s.find('.');
87 if (dot != std::string::npos)
88 {
89 size_t last = s.find_last_not_of('0');
90 if (last != std::string::npos && last > dot)
91 s = s.substr(0, last + 1);
92 else if (last == dot)
93 s = s.substr(0, dot);
94 }
95 return "[" + s + "]";
96 }
97
99 return "[" + stringValue + "]";
100 }
101 return "[]";
102}
103
104bool Operand::IsPin() const
105{
106 return mode == OperandMode::Pin;
107}
108
110{
111 return mode == OperandMode::Variable;
112}
113
115{
116 return mode == OperandMode::Const;
117}
118
119// ---------------------------------------------------------------------------
120// Serialization
121// ---------------------------------------------------------------------------
122
124{
125 nlohmann::json j = nlohmann::json::object();
126
127 switch (mode)
128 {
130 j["mode"] = "Variable";
131 j["value"] = stringValue;
132 break;
133
135 j["mode"] = "Const";
136 j["value"] = constValue;
137 break;
138
139 case OperandMode::Pin:
140 j["mode"] = "Pin";
141 j["value"] = stringValue;
142 break;
143 }
144
145 return j;
146}
147
148/*static*/
150{
151 Operand op;
152
153 if (!data.is_object())
154 return op;
155
156 std::string modeStr;
157 if (data.contains("mode") && data["mode"].is_string())
158 modeStr = data["mode"].get<std::string>();
159
160 if (modeStr == "Variable")
161 {
163 if (data.contains("value") && data["value"].is_string())
164 op.stringValue = data["value"].get<std::string>();
165 }
166 else if (modeStr == "Const")
167 {
169 if (data.contains("value") && data["value"].is_number())
170 op.constValue = data["value"].get<double>();
171 }
172 else if (modeStr == "Pin")
173 {
175 if (data.contains("value") && data["value"].is_string())
176 op.stringValue = data["value"].get<std::string>();
177 }
178
179 return op;
180}
181
182} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Defines the Operand type used in condition preset expressions.
< Provides AssetID and INVALID_ASSET_ID
OperandMode
Discriminates which source the operand draws its value from.
Definition Operand.h:29
@ Variable
References a blackboard variable by ID (string key)
@ Const
Literal numeric constant (double)
@ Pin
External data input pin on the node (identified by label)
nlohmann::json json
One side of a ConditionPreset comparison expression.
Definition Operand.h:45
double constValue
Literal constant (mode == Const)
Definition Operand.h:48
static Operand FromJson(const nlohmann::json &data)
Deserializes an Operand from a JSON object.
Definition Operand.cpp:149
nlohmann::json ToJson() const
Serializes this Operand to a JSON object.
Definition Operand.cpp:123
bool IsVariable() const
Returns true when mode == Variable.
Definition Operand.cpp:109
bool IsPin() const
Returns true when mode == Pin.
Definition Operand.cpp:104
static Operand CreateVariable(const std::string &variableID)
Factory — creates a Variable-mode operand.
Definition Operand.cpp:33
std::string stringValue
Variable ID or Pin label (mode == Variable|Pin)
Definition Operand.h:47
static Operand CreatePin(const std::string &pinLabel)
Factory — creates a Pin-mode operand.
Definition Operand.cpp:53
std::string GetDisplayString() const
Returns a human-readable display string.
Definition Operand.cpp:66
static Operand CreateConst(double constVal)
Factory — creates a Const-mode operand.
Definition Operand.cpp:43
bool IsConst() const
Returns true when mode == Const.
Definition Operand.cpp:114
OperandMode mode
Discriminator tag.
Definition Operand.h:46
Operand()
Default: Variable mode, empty string.
Definition Operand.cpp:21