Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Operand.h
Go to the documentation of this file.
1/**
2 * @file Operand.h
3 * @brief Defines the Operand type used in condition preset expressions.
4 * @author Olympe Engine
5 * @date 2026-03-16
6 *
7 * @details
8 * An Operand represents one side (left or right) of a condition comparison.
9 * It can refer to a blackboard variable by name (Variable mode), hold a
10 * literal numeric constant (Const mode), or represent an external data input
11 * pin on the node (Pin mode).
12 *
13 * C++14 compliant — uses std::variant via mpark::variant polyfill is NOT
14 * required here; the union is modelled with explicit tagged members to stay
15 * within C++14 without extra dependencies.
16 */
17
18#pragma once
19
20#include <string>
21#include "../../third_party/nlohmann/json.hpp"
22
23namespace Olympe {
24
25/**
26 * @enum OperandMode
27 * @brief Discriminates which source the operand draws its value from.
28 */
29enum class OperandMode {
30 Variable, ///< References a blackboard variable by ID (string key)
31 Const, ///< Literal numeric constant (double)
32 Pin ///< External data input pin on the node (identified by label)
33};
34
35/**
36 * @struct Operand
37 * @brief One side of a ConditionPreset comparison expression.
38 *
39 * @details
40 * Only one of `stringValue` / `constValue` is meaningful depending on `mode`:
41 * - Variable : stringValue holds the blackboard variable ID (e.g. "mHealth")
42 * - Const : constValue holds the literal number (e.g. 2.0)
43 * - Pin : stringValue holds the pin label (e.g. "Pin:1")
44 */
45struct Operand {
46 OperandMode mode; ///< Discriminator tag
47 std::string stringValue; ///< Variable ID or Pin label (mode == Variable|Pin)
48 double constValue; ///< Literal constant (mode == Const)
49
50 // -----------------------------------------------------------------------
51 // Constructors
52 // -----------------------------------------------------------------------
53
54 /// Default: Variable mode, empty string.
55 Operand();
56
57 /// @brief Factory — creates a Variable-mode operand.
58 /// @param variableID Blackboard variable key (e.g. "mHealth").
59 static Operand CreateVariable(const std::string& variableID);
60
61 /// @brief Factory — creates a Const-mode operand.
62 /// @param constVal Literal numeric value.
63 static Operand CreateConst(double constVal);
64
65 /// @brief Factory — creates a Pin-mode operand.
66 /// @param pinLabel Pin label shown in the UI (e.g. "Pin:1").
67 static Operand CreatePin(const std::string& pinLabel);
68
69 // -----------------------------------------------------------------------
70 // Helpers
71 // -----------------------------------------------------------------------
72
73 /// @brief Returns a human-readable display string.
74 /// Examples: "[mHealth]", "[2]", "[Pin:1]"
75 std::string GetDisplayString() const;
76
77 /// @brief Returns true when mode == Pin.
78 bool IsPin() const;
79
80 /// @brief Returns true when mode == Variable.
81 bool IsVariable() const;
82
83 /// @brief Returns true when mode == Const.
84 bool IsConst() const;
85
86 // -----------------------------------------------------------------------
87 // Serialization
88 // -----------------------------------------------------------------------
89
90 /// @brief Serializes this Operand to a JSON object.
91 nlohmann::json ToJson() const;
92
93 /// @brief Deserializes an Operand from a JSON object.
94 /// @param data JSON object (must contain "mode" and "value" keys).
95 static Operand FromJson(const nlohmann::json& data);
96};
97
98} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
< 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