Skip to content

Commit

Permalink
variants example
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pelanek committed Jun 13, 2024
1 parent 0683e69 commit 6a756e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
13 changes: 11 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
add_executable(test
add_executable(test1
basicExample.cpp
)

target_link_libraries(test PRIVATE
target_link_libraries(test1 PRIVATE
telemetry::telemetry
telemetry::appFs
)

add_executable(test2
variantsExample.cpp
)

target_link_libraries(test2 PRIVATE
telemetry::telemetry
telemetry::appFs
)
51 changes: 51 additions & 0 deletions examples/variantsExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <telemetry.hpp>
#include <appFs.hpp>
#include <memory>

// example help
#include <iostream>

// Simplest type. Only contains one value .
// The values can be of type std::monostate, bool, uint64_t, int64_t, double, std::string.
telemetry::Scalar scalarReturn() {
return telemetry::Scalar(static_cast<uint64_t>(100));
}

// Contains not only scalar but also has a unit.
// The unit is of type string.
telemetry::ScalarWithUnit scalartWithUnitReturn() {
return telemetry::ScalarWithUnit(static_cast<double>(42), "%");
}

// Array is a vector of Scalars.
telemetry::Array arrayReturn() {
telemetry::Array arr;

const uint64_t nScalars = 10;

for(uint64_t index = 0; index < nScalars; index++) {
arr.emplace_back(telemetry::Scalar(index));
}

return arr;
}

// Dict is a map with string as a key.
// Value can be std::monostate or any one of the three previous types
telemetry::Dict dictReturn() {
telemetry::Dict dict;

dict["1"] = telemetry::Scalar(static_cast<int64_t>(10));
dict["2"] = telemetry::ScalarWithUnit(static_cast<bool>(10), "bool");
dict["3"] = telemetry::Array(std::vector<telemetry::Scalar>());

return dict;
}

int main() {
// telemetry::Content can be easily printed or stored as string with contentToString()
std::cout << telemetry::contentToString(scalarReturn());
std::cout << telemetry::contentToString(scalartWithUnitReturn());
std::cout << telemetry::contentToString(arrayReturn());
std::cout << telemetry::contentToString(dictReturn());
}

0 comments on commit 6a756e6

Please sign in to comment.