1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-02 02:42:36 +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>
namespace Configuration
{
namespace Configuration {
void AfterParse::handleDetail(const char* name, Configurable* value) {
value->afterParse();
value->handle(*this);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -94,8 +94,7 @@ namespace Configuration {
if (!Eof() && Current() == '\n') {
Inc();
} // \r\n
}
else {
} else {
switch (Current()) {
case '"':
case '\'': {
@@ -122,16 +121,14 @@ namespace Configuration {
for (auto i = 0; i < 4; ++i) {
Inc();
}
}
else if (EqualsCaseInsensitive("false")) {
} else if (EqualsCaseInsensitive("false")) {
token_.kind_ = TokenKind::Boolean;
token_.bValue_ = false;
for (auto i = 0; i < 5; ++i) {
Inc();
}
}
else if (IsDigit() || Current() == '-') {
} else if (IsDigit() || Current() == '-') {
auto doubleOrIntStart = current_;
int intValue = 0;
@@ -154,16 +151,14 @@ namespace Configuration {
token_.kind_ = TokenKind::FloatingPoint;
current_ = floatEnd;
}
else {
} else {
if (negative) {
intValue = -intValue;
}
token_.iValue_ = intValue;
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
token_.kind_ = TokenKind::String;
token_.sValueStart_ = current_;
@@ -194,8 +189,7 @@ namespace Configuration {
}
}
}
}
else {
} else {
token_.kind_ = TokenKind::Eof;
token_.indent_ = 0;
}

View File

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

View File

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