Skip to content

Commit

Permalink
🔀 Merge pull request #28 from epitech-mirroring/22-refactor-components
Browse files Browse the repository at this point in the history
♻️ Refactored components to allow "composed" one
  • Loading branch information
RenardFute authored Feb 14, 2024
2 parents 9b47a52 + f1d8a58 commit 18c9bbd
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
##

# All the source files
CXX_SOURCES = src/main.cpp \
src/ComponentFactory.cpp \
src/components/AbstractComponent.cpp \
src/components/InputComponent.cpp \
src/components/OrComponent.cpp \
CXX_SOURCES = src/main.cpp \
src/ComponentFactory.cpp \
src/components/AbstractComponent.cpp \
src/components/ComposedComponent.cpp \
src/components/InputComponent.cpp \
src/components/OrComponent.cpp \
src/components/Composed4071Component.cpp \

# Compiler and linker settings
NAME = nanotekspice
Expand Down
54 changes: 54 additions & 0 deletions src/components/Composed4071Component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/

#include "Composed4071Component.hpp"
#include "OrComponent.hpp"

using namespace nts::Components;

Composed4071Component::Composed4071Component(): ComposedComponent(14, 4)
{
_internal["A"] = new OrComponent();
_internal["B"] = new OrComponent();
_internal["C"] = new OrComponent();
_internal["D"] = new OrComponent();
this->setPinMode(3, PinMode::OUTPUT);
this->setPinMode(4, PinMode::OUTPUT);
this->setPinMode(10, PinMode::OUTPUT);
this->setPinMode(11, PinMode::OUTPUT);
this->setLink(1, *_internal["A"], 1);
this->setLink(2, *_internal["A"], 2);
this->setLink(5, *_internal["B"], 1);
this->setLink(6, *_internal["B"], 2);
this->setLink(8, *_internal["C"], 1);
this->setLink(9, *_internal["C"], 2);
this->setLink(12, *_internal["D"], 1);
this->setLink(13, *_internal["D"], 2);
this->setLink(3, *_internal["A"], 3);
this->setLink(4, *_internal["B"], 3);
this->setLink(10, *_internal["C"], 3);
this->setLink(11, *_internal["D"], 3);
}

nts::Tristate Composed4071Component::compute(std::size_t pin)
{
if (pin == 3)
return _internal["A"]->compute(3);
if (pin == 4)
return _internal["B"]->compute(3);
if (pin == 10)
return _internal["C"]->compute(3);
if (pin == 11)
return _internal["D"]->compute(3);
return UNDEFINED;
}

Composed4071Component *Composed4071Component::clone() const
{
return new Composed4071Component();
}
20 changes: 20 additions & 0 deletions src/components/Composed4071Component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/

#pragma once
#include "ComposedComponent.hpp"

namespace nts::Components {
class Composed4071Component: public ComposedComponent {
public:
Composed4071Component();
~Composed4071Component() override = default;
Tristate compute(std::size_t pin) override;
[[nodiscard]] Composed4071Component *clone() const override;
};
}
15 changes: 15 additions & 0 deletions src/components/ComposedComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/

#include "ComposedComponent.hpp"

using namespace nts::Components;

ComposedComponent::ComposedComponent(std::size_t nbPins, std::size_t nbInternals) : AbstractComponent(nbPins) {
this->_internal.reserve(nbInternals);
}
22 changes: 22 additions & 0 deletions src/components/ComposedComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/

#pragma once
#include "AbstractComponent.hpp"
#include <unordered_map>

namespace nts::Components {
class ComposedComponent: public AbstractComponent {
protected:
std::unordered_map<std::string, IComponent *> _internal;

ComposedComponent(std::size_t nbPins, std::size_t nbInternals);
public:
~ComposedComponent() override = default;
};
}
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include "ComponentFactory.hpp"
#include "components/InputComponent.hpp"
#include "components/OrComponent.hpp"
#include "components/Composed4071Component.hpp"

static void registerComponents(nts::ComponentFactory &factory)
{
factory.registerComponent("input", new nts::Components::InputComponent());
factory.registerComponent("or", new nts::Components::OrComponent());
factory.registerComponent("4071", new nts::Components::Composed4071Component());
}

int main() {
Expand Down

0 comments on commit 18c9bbd

Please sign in to comment.