FERS 0.1.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
fers_context.h
Go to the documentation of this file.
1/**
2 * @file fers_context.h
3 * @brief Internal C++ class that encapsulates the state of a simulation instance.
4 */
5
6#pragma once
7
8#include <memory>
9#include <random>
10#include <utility>
11
12#include "output_config.h"
13#include "output_metadata.h"
14#include "world.h"
15
16/**
17 * @class FersContext
18 * @brief Manages the lifetime and state of a single FERS simulation scenario.
19 *
20 * This class serves as the C++ backend for the opaque `fers_context_t`
21 * handle exposed by the C-API. The primary reason for its existence is to
22 * apply the "Pimpl" (Pointer to Implementation) idiom at the ABI boundary. By
23 * hiding the C++ standard library types (`std::unique_ptr`, `std::mt19937`) and
24 * the full definition of `core::World` from the C header, we create a stable
25 * ABI that does not break when the internal C++ implementation changes. This
26 * encapsulation ensures that clients of the C-API (like Rust) do not need to be
27 * recompiled if only the library's internals are modified.
28 *
29 * Its secondary role is to own the `core::World` object, which represents the
30 * entire scenario, and the master random number generator. This guarantees that
31 * the simulation state persists in memory between API calls and that randomness
32 * can be controlled deterministically from a single source.
33 */
35{
36public:
37 /**
38 * @brief Constructs a new simulation context, initializing an empty world.
39 *
40 * The master random number generator is default-constructed. This is a
41 * deliberate choice to allow the seed to be configured later, typically
42 * after parsing a scenario file. This ensures that the scenario itself can
43 * define its own seed for reproducible simulations. If no seed is provided,
44 * a random one is generated at load time.
45 */
46 // NOLINTNEXTLINE(cert-msc51-cpp)
47 FersContext() : _world(std::make_unique<core::World>()) {}
48
49 /**
50 * @brief Retrieves a pointer to the simulation world.
51 * This provides direct, mutable access to the in-memory representation of the
52 * scenario, allowing API functions to modify it.
53 * @return A non-owning pointer to the `core::World` object.
54 */
55 [[nodiscard]] core::World* getWorld() const noexcept { return _world.get(); }
56
57 /**
58 * @brief Retrieves a mutable reference to the master random number seeder.
59 *
60 * A single master generator is used to seed all other random number
61 * generators within the simulation (e.g., for noise models, RCS
62 * fluctuations). This design is crucial for ensuring that a simulation can
63 * be made fully deterministic and reproducible by controlling a single seed
64 * value at the top level.
65 * @return A reference to the `std::mt19937` engine.
66 */
67 [[nodiscard]] std::mt19937& getMasterSeeder() noexcept { return _master_seeder; }
68
69 /**
70 * @brief Sets the output directory for simulation results.
71 */
72 void setOutputDir(std::string dir) { _output_dir = std::move(dir); }
73
74 /**
75 * @brief Gets the output directory for simulation results.
76 */
77 [[nodiscard]] const std::string& getOutputDir() const noexcept { return _output_dir; }
78
79 /**
80 * @brief Sets the runtime output configuration.
81 */
82 void setOutputConfig(core::OutputConfig config) { _output_config = std::move(config); }
83
84 /**
85 * @brief Gets the runtime output configuration.
86 */
87 [[nodiscard]] const core::OutputConfig& getOutputConfig() const noexcept { return _output_config; }
88
89 /**
90 * @brief Stores metadata from the most recent simulation run.
91 * @param metadata The metadata snapshot to store.
92 */
93 void setLastOutputMetadata(core::OutputMetadata metadata) { _last_output_metadata = std::move(metadata); }
94
95 /**
96 * @brief Clears any stored simulation output metadata.
97 */
98 void clearLastOutputMetadata() { _last_output_metadata = core::OutputMetadata{}; }
99
100 /**
101 * @brief Serializes the last simulation output metadata as JSON.
102 * @return JSON representation of the last output metadata.
103 */
104 [[nodiscard]] std::string getLastOutputMetadataJson() const
105 {
106 return core::outputMetadataToJsonString(_last_output_metadata);
107 }
108
109private:
110 /// Owns the `core::World` object, which contains all simulation entities.
111 /// Using `std::unique_ptr` ensures that the world's complex state is
112 /// automatically cleaned up when the FersContext is destroyed.
113 std::unique_ptr<core::World> _world;
114
115 /// Master random engine used to seed all other random generators in the simulation.
116 std::mt19937 _master_seeder;
117
118 /// Output directory for the simulation files
119 std::string _output_dir = ".";
120
121 /// Runtime output selection and transport-specific settings.
122 core::OutputConfig _output_config;
123
124 /// Metadata from the most recent simulation output.
125 core::OutputMetadata _last_output_metadata;
126};
Manages the lifetime and state of a single FERS simulation scenario.
FersContext()
Constructs a new simulation context, initializing an empty world.
void setLastOutputMetadata(core::OutputMetadata metadata)
Stores metadata from the most recent simulation run.
const std::string & getOutputDir() const noexcept
Gets the output directory for simulation results.
void setOutputConfig(core::OutputConfig config)
Sets the runtime output configuration.
std::mt19937 & getMasterSeeder() noexcept
Retrieves a mutable reference to the master random number seeder.
const core::OutputConfig & getOutputConfig() const noexcept
Gets the runtime output configuration.
void setOutputDir(std::string dir)
Sets the output directory for simulation results.
core::World * getWorld() const noexcept
Retrieves a pointer to the simulation world.
std::string getLastOutputMetadataJson() const
Serializes the last simulation output metadata as JSON.
void clearLastOutputMetadata()
Clears any stored simulation output metadata.
The World class manages the simulator environment.
Definition world.h:39
std::string outputMetadataToJsonString(const OutputMetadata &metadata)
Serializes a full simulation output metadata snapshot to JSON.
Metadata summary for the full simulation output set.
Header file for the World class in the simulator.