Branch data Line data Source code
1 : : /**
2 : : ***********************************************************************************************************************
3 : : * @file runtime.cpp
4 : : * @author Diego Martínez García (dmg0345@gmail.com)
5 : : * @date 08-01-2025 17:09:18 (UTC)
6 : : * @version 1.0.0
7 : : * @copyright github.com/dmg0345/commonapi_cpp_examples/blob/master/LICENSE
8 : : ***********************************************************************************************************************
9 : : */
10 : :
11 : : #include "utils/capi/priv/runtime.hpp"
12 : :
13 : : #include <string>
14 : :
15 : : #include "utils/error/error.hpp"
16 : :
17 : : #include <CommonAPI/CommonAPI.hpp>
18 : :
19 : : namespace Capi = Utils::Capi;
20 : : namespace Error = Utils::Error;
21 : :
22 : 0 : Capi::Runtime::Runtime(void) { }
23 : :
24 : 2 : Capi::Runtime::~Runtime(void) { }
25 : :
26 : 6 : Capi::Runtime & Capi::Runtime::get_instance(void)
27 : : {
28 : : // Initialize as a static, this is atomic and already thread safe.
29 [ + + ][ + - ]: 6 : static auto inst = Runtime();
30 : :
31 : 6 : return inst;
32 : : }
33 : :
34 : 6 : Error::ID Capi::Runtime::get_runtime(std::shared_ptr<CommonAPI::Runtime> & ptr) const
35 : : {
36 : 6 : auto error = Error::ID::UNKNOWN;
37 : :
38 : : // Retrieve pointer to runtime and check it is not NULL.
39 : 6 : auto ptr_temp = CommonAPI::Runtime::get();
40 [ - + ]: 12 : END_ON_TRUE_WITH_ERROR(ptr_temp == nullptr, Error::ID::NULL_POINTER);
41 : :
42 : : // All good.
43 : 6 : ptr = std::move(ptr_temp);
44 : 6 : error = Error::ID::OK;
45 : :
46 : 6 : end:
47 : :
48 : 12 : return error;
49 : 6 : }
50 : :
51 : 0 : Error::ID Capi::Runtime::set_properties(const std::map<std::string, std::string> & props) const
52 : : {
53 : 0 : auto error = Error::ID::UNKNOWN;
54 : 0 : std::shared_ptr<CommonAPI::Runtime> ptr_runtime = nullptr;
55 : :
56 : : // Get runtime pointer.
57 [ - - ]: 0 : END_ON_ERROR(this->get_runtime(ptr_runtime));
58 : :
59 : : // Set properties, in the order they were specified.
60 [ - - ]: 0 : for (const auto & [name, value] : props)
61 : : {
62 : : // Update value of the property.
63 : 0 : ptr_runtime->setProperty(name, value);
64 : :
65 : : // Retrieve value for the property after write, and ensure it was updated to specified value.
66 : 0 : std::map<std::string, std::string> rprops;
67 : 0 : END_ON_ERROR(this->get_properties({name}, rprops));
68 [ - - ]: 0 : END_ON_TRUE_WITH_ERROR(rprops[name] != value, Error::ID::CAPI_RUNTIME);
69 : 0 : }
70 : :
71 : : // All good.
72 : 0 : error = Error::ID::OK;
73 : :
74 : 0 : end:
75 : :
76 : 0 : return error;
77 : 0 : }
78 : :
79 : 0 : Error::ID Capi::Runtime::get_properties(const std::vector<std::string> & names,
80 : : std::map<std::string, std::string> & props) const
81 : : {
82 : 0 : auto error = Error::ID::UNKNOWN;
83 : 0 : std::shared_ptr<CommonAPI::Runtime> ptr_runtime = nullptr;
84 : 0 : std::map<std::string, std::string> temp_props = {};
85 : :
86 : : // Get runtime pointer.
87 [ - - ]: 0 : END_ON_ERROR(this->get_runtime(ptr_runtime));
88 : :
89 [ - - ]: 0 : for (const auto & name : names)
90 : : {
91 : : // Get value for the property.
92 : 0 : auto value = ptr_runtime->getProperty(name);
93 : :
94 : : // An empty string is an error.
95 [ - - ]: 0 : END_ON_TRUE_WITH_ERROR(value.empty(), Error::ID::CAPI_RUNTIME);
96 : :
97 : : // Add value to properties found.
98 : 0 : temp_props[name] = std::move(value);
99 : 0 : }
100 : :
101 : : // All good.
102 : 0 : props = std::move(temp_props);
103 : 0 : error = Error::ID::OK;
104 : :
105 : 0 : end:
106 : :
107 : 0 : return error;
108 : 0 : }
109 : :
110 : : /******************************************************************************************************END OF FILE*****/
|