1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-02 10:53:01 +02:00

clang-format Configuration/

This commit is contained in:
Mitch Bradley
2021-05-20 10:02:42 -10:00
parent 596510965f
commit ab61f96012
18 changed files with 255 additions and 339 deletions

View File

@@ -4,8 +4,7 @@
#include <cstring> #include <cstring>
namespace Configuration namespace Configuration {
{
void AfterParse::handleDetail(const char* name, Configurable* value) { void AfterParse::handleDetail(const char* name, Configurable* value) {
value->afterParse(); value->afterParse();
value->handle(*this); value->handle(*this);

View File

@@ -5,12 +5,10 @@
#include "../Pin.h" #include "../Pin.h"
#include "HandlerBase.h" #include "HandlerBase.h"
namespace Configuration namespace Configuration {
{
class Configurable; class Configurable;
class AfterParse : public HandlerBase class AfterParse : public HandlerBase {
{
AfterParse(const AfterParse&) = delete; AfterParse(const AfterParse&) = delete;
AfterParse& operator=(const AfterParse&) = delete; AfterParse& operator=(const AfterParse&) = delete;
@@ -22,11 +20,11 @@ namespace Configuration
public: public:
AfterParse() = default; AfterParse() = default;
void handle(const char* name, bool& value) override { } void handle(const char* name, bool& value) override {}
void handle(const char* name, int& value) override { } void handle(const char* name, int& value) override {}
void handle(const char* name, float& value) override { } void handle(const char* name, float& value) override {}
void handle(const char* name, double& value) override { } void handle(const char* name, double& value) override {}
void handle(const char* name, StringRange& value) override { } void handle(const char* name, StringRange& value) override {}
void handle(const char* name, Pin& value) override { } void handle(const char* name, Pin& value) override {}
}; };
} }

View File

@@ -3,12 +3,10 @@
#include "Generator.h" #include "Generator.h"
#include "Parser.h" #include "Parser.h"
namespace Configuration namespace Configuration {
{
class HandlerBase; class HandlerBase;
class Configurable class Configurable {
{
Configurable(const Configurable&) = delete; Configurable(const Configurable&) = delete;
Configurable(Configurable&&) = default; Configurable(Configurable&&) = default;

View File

@@ -5,27 +5,21 @@
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
namespace Configuration namespace Configuration {
{ void Generator::enter(const char* name) {
void Generator::enter(const char* name)
{
indent(); indent();
dst_ << name << ":\n"; dst_ << name << ":\n";
indent_++; indent_++;
} }
void Generator::add(Configuration::Configurable* configurable) void Generator::add(Configuration::Configurable* configurable) {
{ if (configurable != nullptr) {
if (configurable != nullptr)
{
configurable->handle(*this); configurable->handle(*this);
} }
} }
void Generator::leave() void Generator::leave() {
{ if (!lastIsNewline_) {
if (!lastIsNewline_)
{
dst_ << '\n'; dst_ << '\n';
lastIsNewline_ = true; lastIsNewline_ = true;
} }

View File

@@ -7,12 +7,10 @@
#include "../StringStream.h" #include "../StringStream.h"
#include "HandlerBase.h" #include "HandlerBase.h"
namespace Configuration namespace Configuration {
{
class Configurable; class Configurable;
class Generator : public HandlerBase class Generator : public HandlerBase {
{
Generator(const Generator&) = delete; Generator(const Generator&) = delete;
Generator& operator=(const Generator&) = delete; Generator& operator=(const Generator&) = delete;
@@ -22,8 +20,7 @@ namespace Configuration
inline void indent() { inline void indent() {
lastIsNewline_ = false; lastIsNewline_ = false;
for (int i = 0; i < indent_ * 2; ++i) for (int i = 0; i < indent_ * 2; ++i) {
{
dst_ << ' '; dst_ << ' ';
} }
} }

View File

@@ -6,8 +6,7 @@
namespace Configuration { namespace Configuration {
template <typename BaseType> template <typename BaseType>
class GenericFactory class GenericFactory {
{
static GenericFactory& instance() { static GenericFactory& instance() {
static GenericFactory instance_; static GenericFactory instance_;
return instance_; return instance_;
@@ -35,30 +34,19 @@ namespace Configuration {
std::vector<BuilderBase*> builders_; std::vector<BuilderBase*> builders_;
inline static void registerBuilder(BuilderBase* builder) inline static void registerBuilder(BuilderBase* builder) { instance().builders_.push_back(builder); }
{
instance().builders_.push_back(builder);
}
public: public:
template <typename DerivedType> template <typename DerivedType>
class InstanceBuilder : public BuilderBase class InstanceBuilder : public BuilderBase {
{
public: public:
InstanceBuilder(const char* name) : BuilderBase(name) { InstanceBuilder(const char* name) : BuilderBase(name) { instance().registerBuilder(this); }
instance().registerBuilder(this);
}
BaseType* create() const override BaseType* create() const override { return new DerivedType(); }
{
return new DerivedType();
}
}; };
static void handle(Configuration::HandlerBase& handler, BaseType*& inst) static void handle(Configuration::HandlerBase& handler, BaseType*& inst) {
{ if (inst == nullptr) {
if (inst == nullptr)
{
for (auto it : instance().builders_) { for (auto it : instance().builders_) {
if (handler.matchesUninitialized(it->name())) { if (handler.matchesUninitialized(it->name())) {
inst = it->create(); inst = it->create();
@@ -67,9 +55,7 @@ namespace Configuration {
return; return;
} }
} }
} } else {
else
{
handler.handleDetail(inst->name(), inst); handler.handleDetail(inst->name(), inst);
} }
} }

View File

@@ -4,15 +4,13 @@
#include "../Pin.h" #include "../Pin.h"
#include "../StringRange.h" #include "../StringRange.h"
namespace Configuration namespace Configuration {
{
class Configurable; class Configurable;
template <typename BaseType> template <typename BaseType>
class GenericFactory; class GenericFactory;
class HandlerBase class HandlerBase {
{
protected: protected:
virtual void handleDetail(const char* name, Configurable* value) = 0; virtual void handleDetail(const char* name, Configurable* value) = 0;
virtual bool matchesUninitialized(const char* name) = 0; virtual bool matchesUninitialized(const char* name) = 0;
@@ -28,12 +26,10 @@ namespace Configuration
virtual void handle(const char* name, StringRange& value) = 0; virtual void handle(const char* name, StringRange& value) = 0;
virtual void handle(const char* name, Pin& value) = 0; virtual void handle(const char* name, Pin& value) = 0;
virtual void handle(const char* name, String& value) virtual void handle(const char* name, String& value) {
{
StringRange range; StringRange range;
handle(name, range); handle(name, range);
if (range.begin() != nullptr) if (range.begin() != nullptr) {
{
value = range.str(); value = range.str();
} }
} }
@@ -42,15 +38,12 @@ namespace Configuration
template <typename T> template <typename T>
void handle(const char* name, T*& value) { void handle(const char* name, T*& value) {
if (handlerType() == HandlerType::Parser) if (handlerType() == HandlerType::Parser) {
{ if (value == nullptr && matchesUninitialized(name)) {
if (value == nullptr && matchesUninitialized(name))
{
value = new T(); value = new T();
handleDetail(name, value); handleDetail(name, value);
} }
} } else {
else {
if (value != nullptr) { if (value != nullptr) {
handleDetail(name, value); handleDetail(name, value);
} }
@@ -58,6 +51,8 @@ namespace Configuration
} }
template <typename T> template <typename T>
void handle(const char* name, T& value) { handleDetail(name, &value); } void handle(const char* name, T& value) {
handleDetail(name, &value);
}
}; };
} }

View File

@@ -1,13 +1,5 @@
#pragma once #pragma once
namespace Configuration namespace Configuration {
{ enum struct HandlerType { Parser, AfterParse, Runtime, Generator, Validator };
enum struct HandlerType
{
Parser,
AfterParse,
Runtime,
Generator,
Validator
};
} }

View File

@@ -2,14 +2,10 @@
#include "LegacySettingRegistry.h" #include "LegacySettingRegistry.h"
namespace Configuration namespace Configuration {
{ class LegacySettingHandler {
class LegacySettingHandler
{
public: public:
inline LegacySettingHandler() { inline LegacySettingHandler() { LegacySettingRegistry::registerHandler(this); }
LegacySettingRegistry::registerHandler(this);
}
LegacySettingHandler(const LegacySettingHandler&) = delete; LegacySettingHandler(const LegacySettingHandler&) = delete;
LegacySettingHandler(LegacySettingHandler&&) = delete; LegacySettingHandler(LegacySettingHandler&&) = delete;

View File

@@ -2,17 +2,10 @@
#include "LegacySettingHandler.h" #include "LegacySettingHandler.h"
namespace Configuration namespace Configuration {
{ bool LegacySettingRegistry::isLegacySetting(const char* str) { return str[0] == '$' && (str[1] >= '0' && str[1] <= '9'); }
bool LegacySettingRegistry::isLegacySetting(const char* str)
{
return str[0] == '$' && (str[1] >= '0' && str[1] <= '9');
}
void LegacySettingRegistry::registerHandler(LegacySettingHandler* handler) void LegacySettingRegistry::registerHandler(LegacySettingHandler* handler) { instance().handlers_.push_back(handler); }
{
instance().handlers_.push_back(handler);
}
bool LegacySettingRegistry::tryHandleLegacy(const char* str) { bool LegacySettingRegistry::tryHandleLegacy(const char* str) {
if (isLegacySetting(str)) { if (isLegacySetting(str)) {
@@ -21,8 +14,7 @@ namespace Configuration
int value = 0; int value = 0;
++str; ++str;
while (*str && *str >= '0' && *str <= '9') while (*str && *str >= '0' && *str <= '9') {
{
value = value * 10 + (*str - '0'); value = value * 10 + (*str - '0');
++str; ++str;
} }
@@ -31,13 +23,11 @@ namespace Configuration
++str; ++str;
handleLegacy(value, str); handleLegacy(value, str);
} } else {
else {
log_warn("Incorrect setting '" << start << "': cannot find '='."); log_warn("Incorrect setting '" << start << "': cannot find '='.");
} }
return true; return true;
} } else {
else {
return false; return false;
} }
} }
@@ -45,8 +35,7 @@ namespace Configuration
void LegacySettingRegistry::handleLegacy(int index, const char* value) { void LegacySettingRegistry::handleLegacy(int index, const char* value) {
bool handled = false; bool handled = false;
for (auto it : instance().handlers_) { for (auto it : instance().handlers_) {
if (it->index() == index) if (it->index() == index) {
{
handled = true; handled = true;
it->handle(value); it->handle(value);
} }

View File

@@ -6,8 +6,7 @@
namespace Configuration { namespace Configuration {
class LegacySettingHandler; class LegacySettingHandler;
class LegacySettingRegistry class LegacySettingRegistry {
{
static LegacySettingRegistry& instance() { static LegacySettingRegistry& instance() {
static LegacySettingRegistry instance_; static LegacySettingRegistry instance_;
return instance_; return instance_;

View File

@@ -17,8 +17,7 @@ namespace Configuration {
// Attempt to use the correct position in the parser: // Attempt to use the correct position in the parser:
if (current_.keyEnd_) { if (current_.keyEnd_) {
throw ParseException(start_, current_.keyEnd_, description); throw ParseException(start_, current_.keyEnd_, description);
} } else {
else {
Tokenizer::ParseError(description); Tokenizer::ParseError(description);
} }
} }
@@ -114,8 +113,7 @@ namespace Configuration {
return current_.fValue_; return current_.fValue_;
} }
Pin Parser::pinValue() const Pin Parser::pinValue() const {
{
if (current_.kind_ != TokenKind::String) { if (current_.kind_ != TokenKind::String) {
parseError("Expected a string value (e.g. 'foo')"); parseError("Expected a string value (e.g. 'foo')");
} }

View File

@@ -39,8 +39,7 @@ namespace Configuration {
void leave(); void leave();
inline bool is(const char* expected) const { inline bool is(const char* expected) const {
return current_.keyStart_ != nullptr && return current_.keyStart_ != nullptr && !strncmp(expected, current_.keyStart_, size_t(current_.keyEnd_ - current_.keyStart_));
!strncmp(expected, current_.keyStart_, size_t(current_.keyEnd_ - current_.keyStart_));
} }
inline StringRange key() const { return StringRange(current_.keyStart_, current_.keyEnd_); } inline StringRange key() const { return StringRange(current_.keyStart_, current_.keyEnd_); }

View File

@@ -2,19 +2,14 @@
#include <cstdlib> #include <cstdlib>
namespace Configuration namespace Configuration {
{ void RuntimeSetting::handleDetail(const char* name, Configuration::Configurable* value) {
void RuntimeSetting::handleDetail(const char* name, Configuration::Configurable* value) if (is(name) && this->value() == nullptr) {
{
if (is(name) && this->value() == nullptr)
{
auto previous = start_; auto previous = start_;
// Figure out next node // Figure out next node
auto next = start_; auto next = start_;
for (; *next && *next != '=' && *next != '/'; ++next) for (; *next && *next != '=' && *next != '/'; ++next) {}
{
}
// Do we have a child? // Do we have a child?
if (*next == '/') { if (*next == '/') {
@@ -30,35 +25,27 @@ namespace Configuration
} }
} }
void RuntimeSetting::handle(const char* name, int& value) void RuntimeSetting::handle(const char* name, int& value) {
{ if (is(name) && this->value() != nullptr) {
if (is(name) && this->value() != nullptr)
{
value = atoi(this->value()); value = atoi(this->value());
} }
} }
void RuntimeSetting::handle(const char* name, double& value) void RuntimeSetting::handle(const char* name, double& value) {
{ if (is(name) && this->value() != nullptr) {
if (is(name) && this->value() != nullptr)
{
char* floatEnd; char* floatEnd;
value = strtod(this->value(), &floatEnd); value = strtod(this->value(), &floatEnd);
} }
} }
void RuntimeSetting::handle(const char* name, StringRange& value) void RuntimeSetting::handle(const char* name, StringRange& value) {
{ if (is(name) && this->value() != nullptr) {
if (is(name) && this->value() != nullptr)
{
value = this->value(); value = this->value();
} }
} }
void RuntimeSetting::handle(const char* name, Pin& value) void RuntimeSetting::handle(const char* name, Pin& value) {
{ if (is(name) && this->value() != nullptr) {
if (is(name) && this->value() != nullptr)
{
value = Pin::create(StringRange(this->value())); value = Pin::create(StringRange(this->value()));
} }
} }

View File

@@ -3,10 +3,8 @@
#include "HandlerBase.h" #include "HandlerBase.h"
#include "Configurable.h" #include "Configurable.h"
namespace Configuration namespace Configuration {
{ class RuntimeSetting : public Configuration::HandlerBase {
class RuntimeSetting : public Configuration::HandlerBase
{
private: private:
const char* setting_; // $foo/bar=12 const char* setting_; // $foo/bar=12
const char* start_; const char* start_;
@@ -15,18 +13,18 @@ namespace Configuration
if (start_ != nullptr) { if (start_ != nullptr) {
auto len = strlen(name); auto len = strlen(name);
return !strncmp(name, start_, len) && (start_[len] == '=' || start_[len] == '/'); return !strncmp(name, start_, len) && (start_[len] == '=' || start_[len] == '/');
} } else {
else {
return false; return false;
} }
} }
const char* value() const const char* value() const {
{ for (const char* it = start_; *it; ++it) {
for (const char* it = start_; *it; ++it) if (*it == '/') {
{ return nullptr;
if (*it == '/') { return nullptr; } } else if (*it == '=') {
else if (*it == '=') { return it + 1; } return it + 1;
}
} }
return nullptr; return nullptr;
} }
@@ -37,7 +35,7 @@ namespace Configuration
bool matchesUninitialized(const char* name) override { return false; } bool matchesUninitialized(const char* name) override { return false; }
public: public:
RuntimeSetting(const char* runtimeSetting) : setting_(runtimeSetting + 1), start_(runtimeSetting+1) {} RuntimeSetting(const char* runtimeSetting) : setting_(runtimeSetting + 1), start_(runtimeSetting + 1) {}
void handle(const char* name, int& value) override; void handle(const char* name, int& value) override;
void handle(const char* name, double& value) override; void handle(const char* name, double& value) override;

View File

@@ -94,8 +94,7 @@ namespace Configuration {
if (!Eof() && Current() == '\n') { if (!Eof() && Current() == '\n') {
Inc(); Inc();
} // \r\n } // \r\n
} } else {
else {
switch (Current()) { switch (Current()) {
case '"': case '"':
case '\'': { case '\'': {
@@ -122,16 +121,14 @@ namespace Configuration {
for (auto i = 0; i < 4; ++i) { for (auto i = 0; i < 4; ++i) {
Inc(); Inc();
} }
} } else if (EqualsCaseInsensitive("false")) {
else if (EqualsCaseInsensitive("false")) {
token_.kind_ = TokenKind::Boolean; token_.kind_ = TokenKind::Boolean;
token_.bValue_ = false; token_.bValue_ = false;
for (auto i = 0; i < 5; ++i) { for (auto i = 0; i < 5; ++i) {
Inc(); Inc();
} }
} } else if (IsDigit() || Current() == '-') {
else if (IsDigit() || Current() == '-') {
auto doubleOrIntStart = current_; auto doubleOrIntStart = current_;
int intValue = 0; int intValue = 0;
@@ -154,16 +151,14 @@ namespace Configuration {
token_.kind_ = TokenKind::FloatingPoint; token_.kind_ = TokenKind::FloatingPoint;
current_ = floatEnd; current_ = floatEnd;
} } else {
else {
if (negative) { if (negative) {
intValue = -intValue; intValue = -intValue;
} }
token_.iValue_ = intValue; token_.iValue_ = intValue;
token_.kind_ = TokenKind::IntegerValue; token_.kind_ = TokenKind::IntegerValue;
} }
} } else {
else {
// If it's not 'true', not 'false', and not a digit, we have a string delimited by a whitespace // If it's not 'true', not 'false', and not a digit, we have a string delimited by a whitespace
token_.kind_ = TokenKind::String; token_.kind_ = TokenKind::String;
token_.sValueStart_ = current_; token_.sValueStart_ = current_;
@@ -194,8 +189,7 @@ namespace Configuration {
} }
} }
} }
} } else {
else {
token_.kind_ = TokenKind::Eof; token_.kind_ = TokenKind::Eof;
token_.indent_ = 0; token_.indent_ = 0;
} }

View File

@@ -4,8 +4,7 @@
#include <cstring> #include <cstring>
namespace Configuration namespace Configuration {
{
void Validator::handleDetail(const char* name, Configurable* value) { void Validator::handleDetail(const char* name, Configurable* value) {
value->validate(); value->validate();
value->handle(*this); value->handle(*this);

View File

@@ -5,12 +5,10 @@
#include "../Pin.h" #include "../Pin.h"
#include "HandlerBase.h" #include "HandlerBase.h"
namespace Configuration namespace Configuration {
{
class Configurable; class Configurable;
class Validator : public HandlerBase class Validator : public HandlerBase {
{
Validator(const Validator&) = delete; Validator(const Validator&) = delete;
Validator& operator=(const Validator&) = delete; Validator& operator=(const Validator&) = delete;
@@ -22,11 +20,11 @@ namespace Configuration
public: public:
Validator() = default; Validator() = default;
void handle(const char* name, bool& value) override { } void handle(const char* name, bool& value) override {}
void handle(const char* name, int& value) override { } void handle(const char* name, int& value) override {}
void handle(const char* name, float& value) override { } void handle(const char* name, float& value) override {}
void handle(const char* name, double& value) override { } void handle(const char* name, double& value) override {}
void handle(const char* name, StringRange& value) override { } void handle(const char* name, StringRange& value) override {}
void handle(const char* name, Pin& value) override { } void handle(const char* name, Pin& value) override {}
}; };
} }