From e631a84565eb262342eb9495a347cb0bbf69a3a3 Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 26 Jun 2025 13:31:45 +0200 Subject: [PATCH] Refactor form classes and improve formatting for consistency --- .clang-format | 93 +++++++++++++++++++++++++++++ ex02/inc/PresidentialPardonForm.hpp | 5 +- ex02/inc/RobotomyRequestForm.hpp | 2 +- ex02/inc/ShrubberyCreationForm.hpp | 5 +- ex02/inc/colors.h | 6 +- ex02/src/PresidentialPardonForm.cpp | 21 ++++--- ex02/src/RobotomyRequestForm.cpp | 26 +++++--- ex02/src/ShrubberyCreationForm.cpp | 57 ++++++++++-------- 8 files changed, 166 insertions(+), 49 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..41baf0f --- /dev/null +++ b/.clang-format @@ -0,0 +1,93 @@ +# .clang-format +--- +# Base style to inherit from (llvm, google, chromium, microsoft, mozilla, webkit) +BasedOnStyle: microsoft + +# Indentation +IndentWidth: 4 # Number of spaces per indent level +TabWidth: 4 # Number of spaces per tab +UseTab: Never # Options: Never, ForIndentation, Always + +# Braces +BreakBeforeBraces: Allman # Options: Attach, Linux, Stroustrup, Allman, GNU, WebKit, Custom +BraceWrapping: + AfterClass: true # Put brace on new line after class + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false + +# Spaces +SpaceBeforeParens: ControlStatements # Options: Never, ControlStatements, Always +SpaceInEmptyParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpacesInAngles: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true + +# Alignment +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true + +# Column limit +ColumnLimit: 80 # Max line length before wrapping + +# Pointer/reference alignment +PointerAlignment: Right # Options: Left, Right, Middle + +# Includes +IncludeBlocks: Regroup # Options: Preserve, Merge, Regroup +IncludeCategories: + - Regex: '^<.*\.h>' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^<.*>' + Priority: 1 + SortPriority: 0 + CaseSensitive: false + - Regex: '^".*\.hpp"' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '^".*\.h"' + Priority: 4 + SortPriority: 0 + CaseSensitive: false +SortIncludes: true + +# Function declaration/definition formatting +AllowAllParametersOfDeclarationOnNextLine: true +BinPackParameters: true +BinPackArguments: true + +# Namespace indentation +NamespaceIndentation: None # Options: None, Inner, All + +# Constructor initializer formatting +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 + +# Access modifier indentation +AccessModifierOffset: -2 + +# Trailing commas +Cpp11BracedListStyle: true + +# Comment formatting +ReflowComments: true +SpacesBeforeTrailingComments: 1 + +# Misc +IndentCaseLabels: true +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 diff --git a/ex02/inc/PresidentialPardonForm.hpp b/ex02/inc/PresidentialPardonForm.hpp index 48666f2..80e2b1f 100644 --- a/ex02/inc/PresidentialPardonForm.hpp +++ b/ex02/inc/PresidentialPardonForm.hpp @@ -15,9 +15,10 @@ class PresidentialPardonForm : public AForm PresidentialPardonForm(std::string target); PresidentialPardonForm(const PresidentialPardonForm &other); ~PresidentialPardonForm(); - PresidentialPardonForm &operator=(const PresidentialPardonForm &other) = delete; + PresidentialPardonForm &operator=(const PresidentialPardonForm &other) = + delete; - void execute(const Bureaucrat &executor) const override; + void execute(const Bureaucrat &executor) const override; const std::string getTarget() const; }; diff --git a/ex02/inc/RobotomyRequestForm.hpp b/ex02/inc/RobotomyRequestForm.hpp index 8a64265..d8edaf2 100644 --- a/ex02/inc/RobotomyRequestForm.hpp +++ b/ex02/inc/RobotomyRequestForm.hpp @@ -17,7 +17,7 @@ class RobotomyRequestForm : public AForm ~RobotomyRequestForm(); RobotomyRequestForm &operator=(const RobotomyRequestForm &other) = delete; - void execute(const Bureaucrat &executor) const override; + void execute(const Bureaucrat &executor) const override; const std::string getTarget() const; }; diff --git a/ex02/inc/ShrubberyCreationForm.hpp b/ex02/inc/ShrubberyCreationForm.hpp index 6757841..98a6a6f 100644 --- a/ex02/inc/ShrubberyCreationForm.hpp +++ b/ex02/inc/ShrubberyCreationForm.hpp @@ -15,9 +15,10 @@ class ShrubberyCreationForm : public AForm ShrubberyCreationForm(std::string target); ShrubberyCreationForm(const ShrubberyCreationForm &other); ~ShrubberyCreationForm(); - ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other) = delete; + ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other) = + delete; - void execute(const Bureaucrat &executor) const override; + void execute(const Bureaucrat &executor) const override; const std::string getTarget() const; }; diff --git a/ex02/inc/colors.h b/ex02/inc/colors.h index dd3b2e5..aaaf665 100644 --- a/ex02/inc/colors.h +++ b/ex02/inc/colors.h @@ -3,9 +3,9 @@ #define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " " #define AFORM BOLD BACKGROUND5 " AForm: " RESET " " -#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND6 " ShrubberyCreationForm: " RESET " " -#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND7 " RobotomyRequestForm: " RESET " " -#define PRESIDENTIAL_PARDON_FORM BOLD BACKGROUND8 " PresidentialPardonForm: " RESET " " +#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " " +#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " " +#define PRESIDENTIAL_PARDON_FORM BOLD BACKGROUND9 " PresidentialPardonForm: " RESET " " #define CONSTRUCTOR "Constructor called" #define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called" diff --git a/ex02/src/PresidentialPardonForm.cpp b/ex02/src/PresidentialPardonForm.cpp index fe4d72d..f5662a7 100644 --- a/ex02/src/PresidentialPardonForm.cpp +++ b/ex02/src/PresidentialPardonForm.cpp @@ -1,14 +1,17 @@ +#include "PresidentialPardonForm.hpp" + #include #include #include "AForm.hpp" -#include "PresidentialPardonForm.hpp" + #include "colors.h" PresidentialPardonForm::PresidentialPardonForm(std::string target) - : AForm("PresidaentialPardonForm", 1, 1), _target(target) + : AForm("Presidential Pardon", 25, 5), _target(target) { - std::cout << PRESIDENTIAL_PARDON_FORM PARAMETERIZED_CONSTRUCTOR << std::endl; + std::cout << PRESIDENTIAL_PARDON_FORM PARAMETERIZED_CONSTRUCTOR + << std::endl; std::cout << PRESIDENTIAL_PARDON_FORM << *this << std::endl; } @@ -17,7 +20,8 @@ PresidentialPardonForm::~PresidentialPardonForm() std::cout << PRESIDENTIAL_PARDON_FORM DESTRUCTOR << std::endl; } -PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other) +PresidentialPardonForm::PresidentialPardonForm( + const PresidentialPardonForm &other) : AForm(other), _target(other._target) { std::cout << PRESIDENTIAL_PARDON_FORM COPY_CONSTRUCTOR << std::endl; @@ -26,8 +30,10 @@ PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &oth std::ostream &operator<<(std::ostream &os, const PresidentialPardonForm &form) { - os << form.getName() << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") - << ", Sign Grade: " << form.getSignGrade() << ", Execute Grade: " << form.getExecuteGrade() + os << form.getName() + << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") + << ", Sign Grade: " << form.getSignGrade() + << ", Execute Grade: " << form.getExecuteGrade() << ", Target: " << form.getTarget(); return os; } @@ -39,7 +45,8 @@ void PresidentialPardonForm::execute(const Bureaucrat &executor) const if (executor.getGrade() > getExecuteGrade()) throw AForm::GradeTooLowException(); - std::cout << "Presidential pardon granted to " << _target << " by " << executor.getName() << "." << std::endl; + std::cout << PRESIDENTIAL_PARDON_FORM<< executor.getName() << " made shure Zaphod Beeblebrox pardoned " + << _target << "." << std::endl; } const std::string PresidentialPardonForm::getTarget() const diff --git a/ex02/src/RobotomyRequestForm.cpp b/ex02/src/RobotomyRequestForm.cpp index 4353b0b..5595ad1 100644 --- a/ex02/src/RobotomyRequestForm.cpp +++ b/ex02/src/RobotomyRequestForm.cpp @@ -1,16 +1,20 @@ #include "RobotomyRequestForm.hpp" -#include "colors.h" + #include #include #include #include -RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm("RobotomyRequestForm", 72, 45), _target(target) +#include "colors.h" + +RobotomyRequestForm::RobotomyRequestForm(std::string target) + : AForm("Robotomy Request", 72, 45), _target(target) { std::cout << ROBOTOMY_REQUEST_FORM PARAMETERIZED_CONSTRUCTOR << std::endl; } -RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other) : AForm(other), _target(other._target) +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other) + : AForm(other), _target(other._target) { std::cout << ROBOTOMY_REQUEST_FORM COPY_CONSTRUCTOR << std::endl; } @@ -27,12 +31,16 @@ void RobotomyRequestForm::execute(const Bureaucrat &executor) const if (executor.getGrade() > getExecuteGrade()) throw AForm::GradeTooLowException(); - std::cout << "Drilling noises... "; + std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR + << "Drrr, Drrr. Drilling noises... " << std::endl; std::srand(static_cast(std::time(0))); if (std::rand() % 2) - std::cout << _target << " has been robotomized successfully!" << std::endl; + std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target + << " has been robotomized!" << std::endl; else - std::cout << "Robotomy failed for " << _target << "." << std::endl; + std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target + << "'s robotomy failed. " << _target + << " is not the same anymore..." << std::endl; } const std::string RobotomyRequestForm::getTarget() const @@ -42,8 +50,10 @@ const std::string RobotomyRequestForm::getTarget() const std::ostream &operator<<(std::ostream &os, const RobotomyRequestForm &form) { - os << form.getName() << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") - << ", Sign Grade: " << form.getSignGrade() << ", Execute Grade: " << form.getExecuteGrade() + os << form.getName() + << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") + << ", Sign Grade: " << form.getSignGrade() + << ", Execute Grade: " << form.getExecuteGrade() << ", Target: " << form.getTarget(); return os; } diff --git a/ex02/src/ShrubberyCreationForm.cpp b/ex02/src/ShrubberyCreationForm.cpp index 6c31cf3..2e06f9a 100644 --- a/ex02/src/ShrubberyCreationForm.cpp +++ b/ex02/src/ShrubberyCreationForm.cpp @@ -1,13 +1,15 @@ -#include "AForm.hpp" -#include "Bureaucrat.hpp" +#include "ShrubberyCreationForm.hpp" + #include #include -#include "ShrubberyCreationForm.hpp" +#include "AForm.hpp" +#include "Bureaucrat.hpp" + #include "colors.h" ShrubberyCreationForm::ShrubberyCreationForm(std::string target) - : AForm("ShrubberyCreationForm", 145, 137), _target(target) + : AForm("Shrubbery Creation", 145, 137), _target(target) { std::cout << SHRUBBERY_CREATION_FORM PARAMETERIZED_CONSTRUCTOR << std::endl; std::cout << SHRUBBERY_CREATION_FORM << *this << std::endl; @@ -23,7 +25,7 @@ ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other) ShrubberyCreationForm::~ShrubberyCreationForm() { std::cout << SHRUBBERY_CREATION_FORM DESTRUCTOR << std::endl; -} +} void ShrubberyCreationForm::execute(const Bureaucrat &executor) const { @@ -35,27 +37,29 @@ void ShrubberyCreationForm::execute(const Bureaucrat &executor) const std::ofstream ofs(_target + "_shrubbery"); if (!ofs) { - std::cerr << "Error creating file: " << _target << "_shrubbery" << std::endl; + std::cerr << "Error creating file: " << _target << "_shrubbery" + << std::endl; return; } - - ofs << " # #### ####\n" - << " ### \\/#|### |/####\n" - << " ##\\/#/ \\||/##/_/##/_#\n" - << " ### \\/###|/ \\/ # ###\n" - << " ##_\\_#\\_\\## | #/###_/_####\n" - << " ## #### # \\ #| / #### ##/##\n" - << " __#_--###` |{,###---###-~\n" - << " \\ }{\n" - << " }}{\n" - << " }}{\n" - << " ejm {{}\n" - << " , -=-~{ .-^- _\n" - << " `}\n" - << " {\n"; - + + ofs << " # #### ####\n" + << " ### \\/#|### |/####\n" + << " ##\\/#/ \\||/##/_/##/_#\n" + << " ### \\/###|/ \\/ # ###\n" + << " ##_\\_#\\_\\## | #/###_/_####\n" + << " ## #### # \\ #| / #### ##/##\n" + << " __#_--###` |{,###---###-~\n" + << " \\ }{\n" + << " }}{\n" + << " }}{\n" + << " {{}\n" + << " , -=-~{ .-^- _\n" + << " `}\n" + << " {\n"; + ofs.close(); - std::cout << "Shrubbery created in file: " << _target << "_shrubbery" << std::endl; + std::cout << SHRUBBERY_CREATION_FORM << "Shrubbery created in file: " << _target << "_shrubbery" + << std::endl; } const std::string ShrubberyCreationForm::getTarget() const @@ -65,9 +69,10 @@ const std::string ShrubberyCreationForm::getTarget() const std::ostream &operator<<(std::ostream &os, const ShrubberyCreationForm &form) { - os << form.getName() << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") - << ", Sign Grade: " << form.getSignGrade() << ", Execute Grade: " << form.getExecuteGrade() + os << form.getName() + << " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed") + << ", Sign Grade: " << form.getSignGrade() + << ", Execute Grade: " << form.getExecuteGrade() << ", Target: " << form.getTarget(); return os; } -