1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include "includes.h"
#include <vector>
#include <optional>
#include <map>
#include <set>
#include <string>
#include <IniFile.h>
IniFile configInit("/config.ini");
class Sensor
{
private:
bool m_ActiveState;
public:
Sensor(bool ActiveState)
{
m_ActiveState = ActiveState;
}
void SetActiveState(bool activeState)
{
m_ActiveState = activeState;
}
bool GetActiveState(void)
{
return (m_ActiveState);
}
};
namespace ini
{
// using namespace std;
struct path_type
{
std::vector<std::string> data;
path_type() = default;
path_type(const std::string §ion, const std::string &key) : data{section, key}
{
}
bool operator==(const path_type &other) const
{
return data == other.data;
}
bool operator<(const path_type &other) const // Note: this is necessary for sorting e.g. in std::map
{
return data < other.data;
}
};
struct file_type
{
bool open(const std::string &fn);
std::optional<std::string> getValue(const path_type &path)
{
std::optional<std::string> result;
if (path.data.size() != 2)
{
// std::cout << "Error ...";
//ESP_LOGE("struct file_type", "getValue wrong number of values");
}
else
{
const size_t bufferLen = 80;
char buffer[bufferLen];
//! HOW DO I SPECIFY section and key BELOW?
if (configInit.getValue(path.data[0].c_str(), path.data[1].c_str(), buffer, bufferLen)) // Note: data[0] -> section, data[1] -> key
//! class "std::vector<std::__cxx11::string,
//! std::allocator<std::__cxx11::string>>" has no member "section"C/C++(135)
{
PrintSectionHas(path.data[0], path.data[1], buffer);
result = buffer;
}
}
return result;
}
std::map<path_type, std::string> getValues(const std::set<path_type> &path_set)
{
std::map<path_type, std::string> result;
for (const path_type &path : path_set)
{
std::optional<std::string> val = getValue(path);
if (val.has_value())
result[path] = *val;
}
return result;
}
};
struct process_type
{
std::function<void(const std::string &)> command; // Note: the input is supposed to be string. The conversion may happen inside the function
std::function<void()> remedial_command;
process_type(const std::function<void(const std::string&)> &c, const std::function<void()> &r) : command{c}, remedial_command{r}
{
}
};
struct path_process_type : ini::path_type
{
process_type proc;
path_process_type(const std::string §ion, const std::string &key, const ini::process_type &p) : path_type{section, key}, proc{p}
{
}
};
void setHeartBeatColor(const std::string&); // Note: must be std::string, if you need const char* consider a lambda function
void setHeartBeatColorDefault();
#define ACTIVE true
#define INACTIVE false
Sensor S_4_20_MA_1(INACTIVE);
void processConfigIni(void)
{
using namespace std::placeholders; // adds visibility of _1, _2, _3,...
// bunch of stuff happens here to check SD card and attach to .ini file on SD
const auto lambda1 = [](const std::string &s)
{
S_4_20_MA_1.SetActiveState(bool());
};
const auto lambda2 = []()
{
S_4_20_MA_1.SetActiveState(bool(INACTIVE));
};
path_process_type paths[] =
{
{"heartbeat", "COLOR", {std::bind(setHeartBeatColor, _1), setHeartBeatColorDefault}},
{"s_4_20_MA_1", "ACTIVE", {lambda1, lambda2}}
};
//! no instance of constructor "ini::path_process_type::path_process_type" matches the argument listC/C++(289)
//! main.cpp(138, 13): argument types are: (const char [10], const char [6], {...})
//! (const ini::path_process_type)dynamic-init: <constructor-call>
extern file_type iniFile;
const auto values = iniFile.getValues(std::set<path_type>(std::begin(paths), std::end(paths)));
//! no suitable user-defined conversion from "std::set<ini::path_process_type,
//! std::less<ini::path_process_type>, std::allocator<ini::path_process_type>>"
//! to "const std::set<ini::path_type, std::less<ini::path_type>,
//! std::allocator<ini::path_type>>" existsC/C++(312)
for (ini::path_process_type &p : paths)
{
const auto it = values.find(p);
if (it != values.end())
{
PrintCouldNotRead(it->first.data[0], it->first.data[1]);
p.proc.remedial_command();
}
else
{
PrintSectionHas(it->first.data[0], it->first.data[1], it->second);
p.proc.command(it->second);
}
}
}
} // namespace ini
void setup()
{
}
void loop()
{
}
| |