Skip to content
Snippets Groups Projects
Commit 10709ab8 authored by vargheseg's avatar vargheseg
Browse files

Merge branch 'extend_config_reader'

Supports specifying modules in xml config.
parents 1b8e0f42 98dac179
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
#define CHIMERATK_APPLICATION_CORE_CONFIG_READER_H
#include <map>
#include<unordered_map>
#include <ChimeraTK/SupportedUserTypes.h>
......@@ -13,6 +14,7 @@ namespace ChimeraTK {
struct ArrayFunctorFill;
struct FunctorSetValues;
struct FunctorSetValuesArray;
class ModuleList;
/** Generic module to read an XML config file and provide the defined values as
* constant variables. The config file should look like this:
......@@ -45,6 +47,7 @@ namespace ChimeraTK {
ConfigReader(EntityOwner* owner, const std::string& name, const std::string& fileName,
const std::unordered_set<std::string>& tags = {});
~ConfigReader() override;
void mainLoop() override {}
void prepare() override;
......@@ -56,8 +59,12 @@ namespace ChimeraTK {
const T& get(const std::string& variableName) const;
protected:
/** File name */
std::string _fileName;
/** List to hold VariableNodes corresponding to xml modules **/
std::unique_ptr<ModuleList> _moduleList;
/** throw a parsing error with more information */
void parsingError(const std::string& message);
......@@ -93,7 +100,7 @@ namespace ChimeraTK {
/** Define type for map of std::string to Var, so we can put it into the
* TemplateUserTypeMap */
template<typename T>
using MapOfVar = std::map<std::string, Var<T>>;
using MapOfVar = std::unordered_map<std::string, Var<T>>;
/** Type-depending map of vectors of variables */
ChimeraTK::TemplateUserTypeMap<MapOfVar> variableMap;
......@@ -101,7 +108,7 @@ namespace ChimeraTK {
/** Define type for map of std::string to Array, so we can put it into the
* TemplateUserTypeMap */
template<typename T>
using MapOfArray = std::map<std::string, Array<T>>;
using MapOfArray = std::unordered_map<std::string, Array<T>>;
/** Type-depending map of vectors of arrays */
ChimeraTK::TemplateUserTypeMap<MapOfArray> arrayMap;
......@@ -118,6 +125,7 @@ namespace ChimeraTK {
template<typename T>
const std::vector<T>& get_impl(const std::string& variableName, std::vector<T>*) const;
friend struct FunctorFill;
friend struct ArrayFunctorFill;
friend struct FunctorSetValues;
......
This diff is collapsed.
......@@ -47,6 +47,24 @@ struct TestModule : ctk::ApplicationModule {
ctk::ArrayPushInput<int32_t> intArray{this, "intArray", "MV/m", 10, "Desc"};
ctk::ArrayPushInput<std::string> stringArray{this, "stringArray", "", 8, "Desc"};
struct Module1 : ctk::VariableGroup {
using ctk::VariableGroup::VariableGroup;
ctk::ScalarPushInput<int16_t> var16{this, "var16", "MV/m", "Desc"};
ctk::ScalarPushInput<uint16_t> var16u{this, "var16u", "MV/m", "Desc"};
ctk::ScalarPushInput<int32_t> var32{this, "var32", "MV/m", "Desc"};
ctk::ScalarPushInput<uint32_t> var32u{this, "var32u", "MV/m", "Desc"};
ctk::ScalarPushInput<std::string> varString{this, "varString", "MV/m", "Desc"};
struct SubModule : ctk::VariableGroup {
using ctk::VariableGroup::VariableGroup;
ctk::ScalarPushInput<uint32_t> var32u{this, "var32u", "MV/m", "Desc"};
ctk::ArrayPushInput<int32_t> intArray{this, "intArray", "MV/m", 10, "Desc"};
ctk::ArrayPushInput<std::string> stringArray{this, "stringArray", "", 8, "Desc"};
} submodule{this, "submodule", ""};
} module1{this, "module1", ""};
std::atomic<bool> done{false};
void mainLoop() {
......@@ -69,6 +87,18 @@ struct TestModule : ctk::ApplicationModule {
BOOST_CHECK_EQUAL(stringArray.getNElements(), 8);
for(size_t i = 0; i < 8; ++i) BOOST_CHECK_EQUAL(stringArray[i], "Hallo" + std::to_string(i + 1));
BOOST_CHECK_EQUAL((int16_t)module1.var16, -567);
BOOST_CHECK_EQUAL((uint16_t)module1.var16u, 678);
BOOST_CHECK_EQUAL((int32_t)module1.var32, -345678);
BOOST_CHECK_EQUAL((uint32_t)module1.var32u, 234567);
BOOST_CHECK_EQUAL((uint32_t)module1.submodule.var32u, 234567);
BOOST_CHECK_EQUAL(module1.submodule.intArray.getNElements(), 10);
for(size_t i = 0; i < 10; ++i) BOOST_CHECK_EQUAL(module1.submodule.intArray[i], 10 - i);
BOOST_CHECK_EQUAL(module1.submodule.stringArray.getNElements(), 8);
for(size_t i = 0; i < 8; ++i) BOOST_CHECK_EQUAL(module1.submodule.stringArray[i], "Hallo" + std::to_string(i + 1));
// no further update shall be received
usleep(1000000); // 1 second
BOOST_CHECK(!var8.readNonBlocking());
......@@ -84,6 +114,14 @@ struct TestModule : ctk::ApplicationModule {
BOOST_CHECK(!varString.readNonBlocking());
BOOST_CHECK(!intArray.readNonBlocking());
BOOST_CHECK(!module1.var16.readNonBlocking());
BOOST_CHECK(!module1.var16u.readNonBlocking());
BOOST_CHECK(!module1.var32.readNonBlocking());
BOOST_CHECK(!module1.var32u.readNonBlocking());
BOOST_CHECK(!module1.submodule.var32u.readNonBlocking());
BOOST_CHECK(!module1.submodule.intArray.readNonBlocking());
BOOST_CHECK(!module1.submodule.stringArray.readNonBlocking());
// inform main thread that we are done
done = true;
}
......@@ -159,6 +197,20 @@ BOOST_AUTO_TEST_CASE(testConfigReader) {
BOOST_CHECK_EQUAL(arrayValueString.size(), 8);
for(size_t i = 0; i < 8; ++i) BOOST_CHECK_EQUAL(arrayValueString[i], "Hallo" + std::to_string(i + 1));
BOOST_CHECK_EQUAL(app.config.get<int16_t>("module1/var16"), -567);
BOOST_CHECK_EQUAL(app.config.get<uint16_t>("module1/var16u"), 678);
BOOST_CHECK_EQUAL(app.config.get<int32_t>("module1/var32"), -345678);
BOOST_CHECK_EQUAL(app.config.get<uint32_t>("module1/var32u"), 234567);
BOOST_CHECK_EQUAL(app.config.get<uint32_t>("module1/submodule/var32u"), 234567);
arrayValue = app.config.get<std::vector<int>>("module1/submodule/intArray");
BOOST_CHECK_EQUAL(arrayValue.size(), 10);
for(size_t i = 0; i < 10; ++i) BOOST_CHECK_EQUAL(arrayValue[i], 10 - i);
arrayValueString = app.config.get<std::vector<std::string>>("module1/submodule/stringArray");
BOOST_CHECK_EQUAL(arrayValueString.size(), 8);
for(size_t i = 0; i < 8; ++i) BOOST_CHECK_EQUAL(arrayValueString[i], "Hallo" + std::to_string(i + 1));
app.config.connectTo(app.testModule);
app.initialise();
......
......@@ -38,6 +38,40 @@
<value i="6" v="Hallo7"/>
<value i="7" v="Hallo8"/>
</variable>
<module name="module1">
<variable name="varString" type="string" value="My dear mister singing club!"/>
<variable name="var16" type="int16" value="-567"/>
<variable name="var16u" type="uint16" value="678"/>
<variable name="var32" type="int32" value="-345678"/>
<variable name="var32u" type="uint32" value="234567"/>
<module name="submodule">
<variable name="var32u" type="uint32" value="234567"/>
<variable name="stringArray" type="string">
<value i="0" v="Hallo1"/>
<value i="1" v="Hallo2"/>
<value i="2" v="Hallo3"/>
<value i="3" v="Hallo4"/>
<value i="4" v="Hallo5"/>
<value i="5" v="Hallo6"/>
<value i="6" v="Hallo7"/>
<value i="7" v="Hallo8"/>
</variable>
<variable name="intArray" type="int32">
<!-- Values are intentionally out-of-order -->
<value i="0" v="10"/>
<value i="1" v="9"/>
<value i="2" v="8"/>
<value i="7" v="3"/>
<value i="8" v="2"/>
<value i="9" v="1"/>
<value i="3" v="7"/>
<value i="4" v="6"/>
<!-- Just a random comment -->
<value i="5" v="5"/>
<value i="6" v="4"/>
</variable>
</module>
</module>
<!-- Just a random comment -->
</configuration>
<!-- Just a random comment -->
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment