some more thoughts for refactoring

This commit is contained in:
Blue 2023-09-18 15:59:43 -03:00
parent b92b66c101
commit 78b7407368
Signed by: blue
GPG Key ID: 9B203B252A63EE38
11 changed files with 169 additions and 43 deletions

View File

@ -39,6 +39,7 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
) )
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src2)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(src2) add_subdirectory(src2)

View File

@ -1,11 +1,13 @@
set(SOURCES set(SOURCES
component.cpp
collection.cpp collection.cpp
loggable.cpp
) )
set(HEADERS set(HEADERS
component.h
collection.h collection.h
loggable.h
) )
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
add_subdirectory(components)

View File

@ -1 +0,0 @@
#include "component.h"

View File

@ -1,40 +0,0 @@
#pragma once
#include <string>
#include <filesystem>
#include "loggger.h"
#include "collection.h"
class Component {
public:
enum State {
initial,
reading,
building,
ready
};
enum Type {
unknown,
file,
directory,
mason
};
Component(Logger* logger, Collection* collection);
virtual ~Component();
Type getType() const;
Type getState() const;
virtual void read(const std::filesystem::path& path) = 0;
virtual void build(const std::filesystem::path& destination, const std::string& target) = 0;
protected:
State state;
Type type;
Logger* logger;
Collection* collection;
std::filesystem::path location;
};

View File

@ -0,0 +1,11 @@
set(SOURCES
component.cpp
file.cpp
)
set(HEADERS
component.h
file.h
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -0,0 +1,23 @@
#include "component.h"
Component::Component(const std::shared_ptr<Collection>& collection, const std::shared_ptr<Logger>& logger):
Loggable(logger),
collection(collection)
{}
void Component::read(const std::filesystem::path& path) {
if (state != initial)
throw WrongState(state, "read");
location = path;
state = reading;
}
Component::State Component::getState() const {
return state;
}
Component::Type Component::getType() const {
return type;
}

View File

@ -0,0 +1,53 @@
#pragma once
#include <string>
#include <filesystem>
#include <memory>
#include <exception>
#include "loggable.h"
#include "loggger.h"
#include "collection.h"
class Component : protected Loggable {
public:
class WrongState;
enum State {
initial,
reading,
building,
ready
};
enum Type {
unknown,
file,
directory,
mason
};
Component(const std::shared_ptr<Collection>& collection, const std::shared_ptr<Logger>& logger);
virtual ~Component() override = default;
Type getType() const;
State getState() const;
virtual void read(const std::filesystem::path& path);
virtual void build(const std::filesystem::path& destination, const std::string& target) = 0;
protected:
State state;
Type type;
std::shared_ptr<Collection> collection;
std::filesystem::path location;
};
class Component::WrongState : public std::exception {
public:
WrongState(State state, const std::string& action);
const char* what() const noexcept( true ) override;
private:
State state;
std::string action;
};

1
src2/components/file.cpp Normal file
View File

@ -0,0 +1 @@
#include "file.h"

14
src2/components/file.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <filesystem>
#include <memory>
#include "component.h"
class File : public Component {
public:
File(const std::shared_ptr<Collection>& collection, const std::shared_ptr<Logger>& logger);
protected:
};

37
src2/loggable.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "loggable.h"
Loggable::Loggable(const std::shared_ptr<Logger>& logger):
logger(logger)
{}
void Loggable::log(Logger::Severity severity, const std::string& message) const {
logger->log(severity, message);
}
void Loggable::debug(const std::string& message) const {
log(Logger::Severity::debug, message);
}
void Loggable::info(const std::string& message) const {
log(Logger::Severity::info, message);
}
void Loggable::minor(const std::string& message) const {
log(Logger::Severity::minor, message);
}
void Loggable::major(const std::string& message) const {
log(Logger::Severity::major, message);
}
void Loggable::warn(const std::string& message) const {
log(Logger::Severity::warning, message);
}
void Loggable::error(const std::string& message) const {
log(Logger::Severity::error, message);
}
void Loggable::fatal(const std::string& message) const {
log(Logger::Severity::fatal, message);
}

25
src2/loggable.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <memory>
#include "loggger.h"
class Loggable {
public:
explicit Loggable(const std::shared_ptr<Logger>& logger);
virtual ~Loggable() = default;
void debug(const std::string& message) const;
void info(const std::string& message) const;
void minor(const std::string& message) const;
void major(const std::string& message) const;
void warn(const std::string& message) const;
void error(const std::string& message) const;
void fatal(const std::string& message) const;
private:
void log(Logger::Severity severity, const std::string& message) const;
private:
std::shared_ptr<Logger> logger;
};