gtranslationTable
Loading...
Searching...
No Matches
tt_example.cc
Go to the documentation of this file.
1
21
22// translationTable
23#include "gtranslationTable.h"
25
26// gemc
27#include "glogger.h"
28
29#include <stdexcept>
30#include <type_traits>
31
32using std::vector;
33
34static_assert(!std::is_default_constructible_v<GElectronic>);
35static_assert(std::is_enum_v<GElectronic::ComparisonMode>);
36static_assert(!std::is_convertible_v<int, GElectronic::ComparisonMode>);
37
48int main(int argc, char* argv[]) {
49 // Build the module options and bind them to the project options infrastructure.
50 auto gopts = std::make_shared<GOptions>(argc, argv, gtranslationTable::defineOptions());
51
52 // Create a logger instance for this example.
53 // Note: this logger is separate from the logger used internally by GTranslationTable via GBase.
54 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, TRANSLATIONTABLE_LOGGER); // duplicate logger
55
56 // Two distinct identity vectors. In realistic scenarios these may represent detector/channel addressing.
57 vector<int> element1 = {1, 2, 3, 4, 5};
58 vector<int> element2 = {2, 2, 3, 4, 5};
59
60 // Two example electronics configurations to associate with the identities.
61 GElectronic crate1(2, 1, 3, GElectronic::ComparisonMode::crate_slot_channel);
62 GElectronic crate2(2, 1, 4, GElectronic::ComparisonMode::crate_slot_channel);
63 try {
64 [[maybe_unused]] GElectronic invalid(-1, 1, 4, GElectronic::ComparisonMode::crate_slot_channel);
65 return EXIT_FAILURE;
66 }
67 catch (const std::invalid_argument&) {
68 // Invalid addresses are rejected before translation-table insertion.
69 }
70
71 // Construct the translation table. It will use the same options object for its internal logger setup.
72 GTranslationTable translationTable(gopts);
73
74 // Register both electronics configurations.
75 translationTable.addGElectronicWithIdentity(element1, crate1);
76 translationTable.addGElectronicWithIdentity(element2, crate2);
77
78 // Retrieve one configuration and print it.
79 const GElectronic& retrievedElectronic = translationTable.getElectronics(element1);
80 if (retrievedElectronic.getHAddress() != vector<int>{2, 1, 3}) {
81 return EXIT_FAILURE;
82 }
83
84 // A duplicate registration preserves the original object and lookup returns that stored object directly.
85 translationTable.addGElectronicWithIdentity(element1, crate2);
86 if (&retrievedElectronic != &translationTable.getElectronics(element1) ||
87 translationTable.getElectronics(element1).getHAddress() != vector<int>{2, 1, 3}) {
88 return EXIT_FAILURE;
89 }
90
91 // Level 0: essential output for a user running the example.
92 log->info(0, "Retrieved electronic: ", retrievedElectronic);
93
94 return EXIT_SUCCESS;
95}
Stores and retrieves GElectronic configurations by a vector-based identity.
const GElectronic & getElectronics(const std::vector< int > &identity) const
Retrieves the electronics configuration associated with a given identity vector.
void addGElectronicWithIdentity(const std::vector< int > &identity, const GElectronic &gtron)
Registers an electronics configuration for a given identity vector.
#define SFUNCTION_NAME
Option definitions for the Translation Table module.
constexpr const char * TRANSLATIONTABLE_LOGGER
GOptions defineOptions()
Defines the module option set for gtranslationTable.
int main(int argc, char *argv[])
std::vector< int > getHAddress() const