FERS 1.0.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_metadata.h"
13#include "world.h"
14
15/**
16 * @class FersContext
17 * @brief Manages the lifetime and state of a single FERS simulation scenario.
18 *
19 * This class serves as the C++ backend for the opaque `fers_context_t`
20 * handle exposed by the C-API. The primary reason for its existence is to
21 * apply the "Pimpl" (Pointer to Implementation) idiom at the ABI boundary. By
22 * hiding the C++ standard library types (`std::unique_ptr`, `std::mt19937`) and
23 * the full definition of `core::World` from the C header, we create a stable
24 * ABI that does not break when the internal C++ implementation changes. This
25 * encapsulation ensures that clients of the C-API (like Rust) do not need to be
26 * recompiled if only the library's internals are modified.
27 *
28 * Its secondary role is to own the `core::World` object, which represents the
29 * entire scenario, and the master random number generator. This guarantees that
30 * the simulation state persists in memory between API calls and that randomness
31 * can be controlled deterministically from a single source.
32 */
34{
35public:
36 /**
37 * @brief Constructs a new simulation context, initializing an empty world.
38 *
39 * The master random number generator is default-constructed. This is a
40 * deliberate choice to allow the seed to be configured later, typically
41 * after parsing a scenario file. This ensures that the scenario itself can
42 * define its own seed for reproducible simulations. If no seed is provided,
43 * a random one is generated at load time.
44 */
45 // NOLINTNEXTLINE(cert-msc51-cpp)
46 FersContext() : _world(std::make_unique<core::World>()) {}
47
48 /**
49 * @brief Retrieves a pointer to the simulation world.
50 * This provides direct, mutable access to the in-memory representation of the
51 * scenario, allowing API functions to modify it.
52 * @return A non-owning pointer to the `core::World` object.
53 */
54 [[nodiscard]] core::World* getWorld() const noexcept { return _world.get(); }
55
56 /**
57 * @brief Retrieves a mutable reference to the master random number seeder.
58 *
59 * A single master generator is used to seed all other random number
60 * generators within the simulation (e.g., for noise models, RCS
61 * fluctuations). This design is crucial for ensuring that a simulation can
62 * be made fully deterministic and reproducible by controlling a single seed
63 * value at the top level.
64 * @return A reference to the `std::mt19937` engine.
65 */
66 [[nodiscard]] std::mt19937& getMasterSeeder() noexcept { return _master_seeder; }
67
68 /**
69 * @brief Sets the output directory for simulation results.
70 */
71 void setOutputDir(std::string dir) { _output_dir = std::move(dir); }
72
73 /**
74 * @brief Gets the output directory for simulation results.
75 */
76 [[nodiscard]] const std::string& getOutputDir() const noexcept { return _output_dir; }
77
78 void setLastOutputMetadata(core::OutputMetadata metadata) { _last_output_metadata = std::move(metadata); }
79
80 void clearLastOutputMetadata() { _last_output_metadata = core::OutputMetadata{}; }
81
82 [[nodiscard]] std::string getLastOutputMetadataJson() const
83 {
84 return core::outputMetadataToJsonString(_last_output_metadata);
85 }
86
87private:
88 /// Owns the `core::World` object, which contains all simulation entities.
89 /// Using `std::unique_ptr` ensures that the world's complex state is
90 /// automatically cleaned up when the FersContext is destroyed.
91 std::unique_ptr<core::World> _world;
92
93 /// Master random engine used to seed all other random generators in the simulation.
94 std::mt19937 _master_seeder;
95
96 /// Output directory for the simulation files
97 std::string _output_dir = ".";
98
99 core::OutputMetadata _last_output_metadata;
100};
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)
const std::string & getOutputDir() const noexcept
Gets the output directory for simulation results.
std::mt19937 & getMasterSeeder() noexcept
Retrieves a mutable reference to the master random number seeder.
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
void clearLastOutputMetadata()
The World class manages the simulator environment.
Definition world.h:39
std::string outputMetadataToJsonString(const OutputMetadata &metadata)
Header file for the World class in the simulator.