Skip to content

Commit 0eec66a

Browse files
committed
get module name automatically
1 parent 2879ecc commit 0eec66a

3 files changed

Lines changed: 39 additions & 21 deletions

File tree

LoggerCore/src/LoggerFunction.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "Particle.h"
33
#include "LoggerFunctionReturns.h"
4+
#include "LoggerModule.h"
45

56
/**
67
* extension of return codes
@@ -115,12 +116,17 @@ class LoggerFunction {
115116
*/
116117
template <typename T>
117118
// defined here instead of in cpp for full flexibility
118-
void registerCommand(T* instance, bool (T::*method)(Variant&), const char* module, const char* cmd) {
119+
void registerCommand(T* instance, bool (T::*method)(Variant&), const char* cmd) {
119120
std::function<bool(Variant&)> cb = [instance, method](Variant& v) {
120121
return (instance->*method)(v);
121122
};
122123
const Vector<String> empty = {};
123-
registerCommand(cb, module, cmd, empty, false, empty, false);
124+
if constexpr (std::is_convertible_v<T*, LoggerModule*>) {
125+
LoggerModule* m = static_cast<LoggerModule*>(instance);
126+
registerCommand(cb, m->getName(), cmd, empty, false, empty, false);
127+
} else {
128+
registerCommand(cb, "", cmd, empty, false, empty, false);
129+
}
124130
}
125131

126132
/**
@@ -129,12 +135,17 @@ class LoggerFunction {
129135
*/
130136
template <typename T>
131137
// defined here instead of in cpp for full flexibility
132-
void registerCommandWithTextValues(T* instance, bool (T::*method)(Variant&), const char* module, const char* cmd, const Vector<String>& text_values, bool value_optional = false) {
138+
void registerCommandWithTextValues(T* instance, bool (T::*method)(Variant&), const char* cmd, const Vector<String>& text_values, bool value_optional = false) {
133139
std::function<bool(Variant&)> cb = [instance, method](Variant& v) {
134140
return (instance->*method)(v);
135141
};
136142
const Vector<String> empty = {};
137-
registerCommand(cb, module, cmd, text_values, false, empty, value_optional);
143+
if constexpr (std::is_convertible_v<T*, LoggerModule*>) {
144+
LoggerModule* m = static_cast<LoggerModule*>(instance);
145+
registerCommand(cb, m->getName(), cmd, text_values, false, empty, value_optional);
146+
} else {
147+
registerCommand(cb, "", cmd, text_values, false, empty, value_optional);
148+
}
138149
}
139150

140151
/**
@@ -143,12 +154,17 @@ class LoggerFunction {
143154
*/
144155
template <typename T>
145156
// defined here instead of in cpp for full flexibility
146-
void registerCommandWithNumericValues(T* instance, bool (T::*method)(Variant&), const char* module, const char* cmd, const Vector<String>& numeric_units = {}, bool value_optional = false) {
157+
void registerCommandWithNumericValues(T* instance, bool (T::*method)(Variant&), const char* cmd, const Vector<String>& numeric_units = {}, bool value_optional = false) {
147158
std::function<bool(Variant&)> cb = [instance, method](Variant& v) {
148159
return (instance->*method)(v);
149160
};
150161
const Vector<String> empty = {};
151-
registerCommand(cb, module, cmd, empty, true, numeric_units, value_optional);
162+
if constexpr (std::is_convertible_v<T*, LoggerModule*>) {
163+
LoggerModule* m = static_cast<LoggerModule*>(instance);
164+
registerCommand(cb, m->getName(), cmd, empty, true, numeric_units, value_optional);
165+
} else {
166+
registerCommand(cb, "", cmd, empty, true, numeric_units, value_optional);
167+
}
152168
}
153169

154170
/**
@@ -157,11 +173,16 @@ class LoggerFunction {
157173
*/
158174
template <typename T>
159175
// defined here instead of in cpp for full flexibility
160-
void registerCommandWithMixedValues(T* instance, bool (T::*method)(Variant&), const char* module, const char* cmd, const Vector<String>& text_values, const Vector<String>& numeric_units = {}, bool value_optional = false) {
176+
void registerCommandWithMixedValues(T* instance, bool (T::*method)(Variant&), const char* cmd, const Vector<String>& text_values, const Vector<String>& numeric_units = {}, bool value_optional = false) {
161177
std::function<bool(Variant&)> cb = [instance, method](Variant& v) {
162178
return (instance->*method)(v);
163179
};
164-
registerCommand(cb, module, cmd, text_values, true, numeric_units, value_optional);
180+
if constexpr (std::is_convertible_v<T*, LoggerModule*>) {
181+
LoggerModule* m = static_cast<LoggerModule*>(instance);
182+
registerCommand(cb, m->getName(), cmd, text_values, true, numeric_units, value_optional);
183+
} else {
184+
registerCommand(cb, "", cmd, text_values, true, numeric_units, value_optional);
185+
}
165186
}
166187

167188
/**

LoggerCore/src/LoggerModule.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ class LoggerModule {
1919
return(m_name);
2020
}
2121

22-
void registerCommands(LoggerFunction* func) {
23-
24-
};
25-
2622
/**
2723
* @brief call this while parsing a command to indicate a warning to the user that does not fail the command
2824
*/

src/function/function_test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "LoggerFunction.h"
1111
#include "LoggerFunctionReturns.h"
1212
#include "LoggerModule.h"
13+
#include "LoggerTimer.h"
1314

1415
// enable system treading
1516
#ifndef SYSTEM_VERSION_v620
@@ -45,7 +46,7 @@ class MyModule : public LoggerModule {
4546

4647
// 'hello'
4748
void registerHelloCommand(LoggerFunction* func, const char* cmd = "hello") {
48-
func->registerCommand(this, &MyModule::hello, getName(), cmd);
49+
func->registerCommand(this, &MyModule::hello, cmd);
4950
}
5051

5152
bool hello(Variant& call) {
@@ -56,7 +57,7 @@ class MyModule : public LoggerModule {
5657

5758
// 'whatup'
5859
void registerWhatupCommand(LoggerFunction* func, const char* cmd = "whatup") {
59-
func->registerCommand(this, &MyModule::whatup, getName(), cmd);
60+
func->registerCommand(this, &MyModule::whatup, cmd);
6061
}
6162

6263
bool whatup(Variant& call) {
@@ -92,30 +93,30 @@ void setup() {
9293

9394
// register a suite of test commands
9495
// start auto-test
95-
func->registerCommand(mod, &MyModule::auto_test, mod->getName(), "auto-test");
96+
func->registerCommand(mod, &MyModule::auto_test, "auto-test");
9697

9798
// register all commands defined in the module class (usually all of them defined there)
9899
mod->registerHelloCommand(func);
99100
mod->registerWhatupCommand(func);
100101
mod->registerWhatupCommand(func, "WHATUP");
101102

102103
// simple command
103-
func->registerCommand(mod, &MyModule::test, mod->getName(), "test1");
104+
func->registerCommand(mod, &MyModule::test, "test1");
104105

105106
// command that accepts on/off values
106-
func->registerCommandWithTextValues(mod, &MyModule::test, mod->getName(), "test2", {LoggerFunction::on, LoggerFunction::off});
107+
func->registerCommandWithTextValues(mod, &MyModule::test, "test2", {LoggerFunction::on, LoggerFunction::off});
107108

108109
// command that accepts a/b/2 values but providing a value is optional (last param)
109-
func->registerCommandWithTextValues(mod, &MyModule::test, mod->getName(), "test3", {"a", "b", "2"}, true);
110+
func->registerCommandWithTextValues(mod, &MyModule::test, "test3", {"a", "b", "2"}, true);
110111

111112
// command that accepts numeric values (no units)
112-
func->registerCommandWithNumericValues(mod, &MyModule::test, mod->getName(), "test4");
113+
func->registerCommandWithNumericValues(mod, &MyModule::test, "test4");
113114

114115
// command that accepts numeric values with specific units, providing the value is optional (last param)
115-
func->registerCommandWithNumericValues(mod, &MyModule::test, mod->getName(), "test5", {"sec", "min"}, true);
116+
func->registerCommandWithNumericValues(mod, &MyModule::test, "test5", {"sec", "min"}, true);
116117

117118
// command that accepts mixed values with a few specific text values OR numeric values with specific units
118-
func->registerCommandWithMixedValues(mod, &MyModule::test, mod->getName(), "test6", {"manual"}, {"ms", "sec"});
119+
func->registerCommandWithMixedValues(mod, &MyModule::test, "test6", {"manual"}, {"ms", "sec"});
119120

120121
// start listening to function calls
121122
func->setup();

0 commit comments

Comments
 (0)