diff --git a/doc/classes.puml b/doc/classes.puml new file mode 100644 index 0000000..9629917 --- /dev/null +++ b/doc/classes.puml @@ -0,0 +1,70 @@ +@startuml + +' Base class +class SystemElement { + - string name + + SystemElement(string name) +} + +' Template container class +class SystemElementContainer { + - unsigned int iter_count + - unordered_map content + + SystemElementContainer(string name) + + SystemElementContainer(string name, vector elements) + + bool exists(string name) + + void add(T* element) + + void add(SystemElementContainer* elements) + + void add(vector elements) + + T* get(string name) + + T* merge(string name) + + iterator begin() + + iterator end() + + const_iterator begin() const + + const_iterator end() const +} + +SystemElementContainer --|> SystemElement + +' Part and Pin classes +class Part { + + Part(string name) + + void add(Pin* pin) +} + +class Pin { + + Pin(string name) + + void connect(Signal* signal) +} + +Part --* Pin + +' Signal class +class Signal { + + Signal(string name) + + void connect(Pin* pin) +} + +Pin --> Signal + +' Module and Modules classes +class Module { + + Signals* signals + + Modules* prnt + + Module(string name) + + ~Module() +} + +class Modules { + + Modules() + + Modules(vector parts) + + void add(Module* module) + + ~Modules() +} + +Module --|> SystemElementContainer +Modules --|> SystemElementContainer +Module --> Modules +Module --> Signals + +@enduml diff --git a/src/imports/import_mentor.cpp b/src/imports/import_mentor.cpp index 90c46d5..bacdd3d 100644 --- a/src/imports/import_mentor.cpp +++ b/src/imports/import_mentor.cpp @@ -106,7 +106,9 @@ void ImportMentor::parse(Signals *signals) auto pin = new Pin(names[0]); // Create a new pin with the first name. Signal *s = nullptr; prt->add(pin); // Add the pin to the current part. - pin->connect(signals->merge(names[2])); // Connect the pin to a signal. + s = signals->merge(names[2]); // Merge the signal with module signals. + s->add(pin); // Add the pin to the signal pins list. + pin->connect(s); // Connect the pin to a signal. } } break; diff --git a/src/main.cpp b/src/main.cpp index a8fad52..ba7d9c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,8 +3,9 @@ using namespace std; int main() { - System sys = System(); + System *sys = new System(); string ment_filename = "/home/francois/Projets/twinsys/test/BPB-2177_Netlist_2_3.qcv"; - sys.Load("bpb", ment_filename, ImportType::IMPORT_MENTOR); + sys->Load("bpb", ment_filename, ImportType::IMPORT_MENTOR); + delete sys; }; diff --git a/src/system/modules.cpp b/src/system/modules.cpp index 343f64c..f2a3390 100644 --- a/src/system/modules.cpp +++ b/src/system/modules.cpp @@ -1,7 +1,30 @@ #include "modules.hpp" -Module::Module(std::string name) : SystemElementContainer(name) {} +#include "signals.hpp" + +Module::Module(std::string name) : SystemElementContainer(name), prnt(nullptr) { + signals = new Signals(); +} + +Module::~Module() { + for (const auto& [key, value] : content) { + delete value; + } + delete signals; +} Modules::Modules(void): SystemElementContainer("modules") {} -Modules::Modules(std::vector modules): SystemElementContainer("modules", modules) {} \ No newline at end of file +Modules::Modules(std::vector modules): SystemElementContainer("modules", modules) {} + +void Modules::add(Module *module) +{ + SystemElementContainer::add(module); + module->prnt = this; +} + +Modules::~Modules() { + for (const auto& [key, value] : content) { + delete value; + } +} \ No newline at end of file diff --git a/src/system/modules.hpp b/src/system/modules.hpp index 935133a..4687a2f 100644 --- a/src/system/modules.hpp +++ b/src/system/modules.hpp @@ -3,20 +3,28 @@ #include "syselmts.hpp" #include "parts.hpp" -#include "signals.hpp" + +#pragma once +class Modules; ///< Forward declaration of the Modules class. +class Signals; ///< Forward declaration of the Signals class. + class Module : public SystemElementContainer { public: - Signals sigs; + Signals *signals; + Modules *prnt; Module(std::string name); + ~Module(); }; class Modules : public SystemElementContainer { public: Modules(void); - Modules(std::vector pins); + Modules(std::vector parts); + void add(Module *module); + ~Modules(); }; #endif // _MODULES_HPP_ \ No newline at end of file diff --git a/src/system/parts.cpp b/src/system/parts.cpp index 8542c20..41ac50f 100644 --- a/src/system/parts.cpp +++ b/src/system/parts.cpp @@ -1,7 +1,19 @@ #include "parts.hpp" -Part::Part(std::string name) : SystemElementContainer(name) {}; +Part::Part(std::string name) : SystemElementContainer(name), prnt(nullptr) {}; + +void Part::add(Pin *pin) +{ + SystemElementContainer::add(pin); + pin->prnt = this; +} + +Part::~Part() { + for (const auto& [key, value] : content) { + delete value; + } +} Parts::Parts(void): SystemElementContainer("parts") {} -Parts::Parts(std::vector parts): SystemElementContainer("parts", parts) {} \ No newline at end of file +Parts::Parts(std::vector parts): SystemElementContainer("parts", parts) {} diff --git a/src/system/parts.hpp b/src/system/parts.hpp index d3485d1..ba3deae 100644 --- a/src/system/parts.hpp +++ b/src/system/parts.hpp @@ -4,10 +4,16 @@ #include "syselmts.hpp" #include "pins.hpp" +#pragma once +class Module; ///< Forward declaration of the Module class. + class Part : public SystemElementContainer { public: Part(std::string name); + ~Part(); + Module *prnt; ///< Pointer to the parent module. + void add(Pin *pin) override; }; class Parts : public SystemElementContainer diff --git a/src/system/pins.cpp b/src/system/pins.cpp index fa4fb6a..64a1b50 100644 --- a/src/system/pins.cpp +++ b/src/system/pins.cpp @@ -1,8 +1,11 @@ #include "pins.hpp" +#include "parts.hpp" +#include "signals.hpp" -Pin::Pin(std::string name) : SystemElement(name), sig(nullptr) {}; +Pin::Pin(std::string name) : SystemElement(name), sig(nullptr), prnt(nullptr) {}; -bool Pin::connected() { +bool Pin::connected() +{ return sig != nullptr; } @@ -11,6 +14,6 @@ void Pin::connect(Signal *signal) sig = signal; } -Pins::Pins(void): SystemElementContainer("pins") {} +Pins::Pins(void) : SystemElementContainer("pins") {} -Pins::Pins(std::vector pins): SystemElementContainer("pins", pins) {} \ No newline at end of file +Pins::Pins(std::vector pins) : SystemElementContainer("pins", pins) {} \ No newline at end of file diff --git a/src/system/pins.hpp b/src/system/pins.hpp index dfa3169..fe51d76 100644 --- a/src/system/pins.hpp +++ b/src/system/pins.hpp @@ -2,13 +2,19 @@ #define _PINS_HPP_ #include "syselmts.hpp" -#include "signals.hpp" + +#pragma once +class Part; + +#pragma once +class Signal; class Pin : public SystemElement { Signal *sig; public: Pin(std::string name); + Part *prnt; ///< Pointer to the parent part. bool connected(); void connect(Signal *signal); }; diff --git a/src/system/signals.cpp b/src/system/signals.cpp index 80071c9..cdea7d2 100644 --- a/src/system/signals.cpp +++ b/src/system/signals.cpp @@ -1,11 +1,27 @@ #include "signals.hpp" +#include "parts.hpp" -Signal::Signal(std::string name) : SystemElementContainer(name) {}; +Signal::Signal(std::string name) : SystemElementContainer(name), prnt(nullptr) {}; + +void Signal::add(Pin *pin) +{ + string pname = pin->prnt->name + "." + pin->name; + SystemElementContainer::add(pname, pin); +} Signals::Signals(void): SystemElementContainer("signals") {} Signals::Signals(std::vector signals): SystemElementContainer("signals", signals) {} -Signals::~Signals() {} +Signals::~Signals() { + for (const auto& [key, value] : content) { + delete value; + } +} +void Signals::add(Signal *signal) +{ + SystemElementContainer::add(signal); + signal->prnt = this; +} \ No newline at end of file diff --git a/src/system/signals.hpp b/src/system/signals.hpp index 2daeb11..dac0233 100644 --- a/src/system/signals.hpp +++ b/src/system/signals.hpp @@ -2,14 +2,17 @@ #define _SIGNALS_HPP_ #include "syselmts.hpp" +#include "pins.hpp" #pragma once -class Pin; +class Signals; class Signal : public SystemElementContainer { public: + Signals *prnt; ///< Pointer to the parent signals object. Signal(std::string name); + void add(Pin *pin) override; }; class Signals : public SystemElementContainer @@ -17,6 +20,7 @@ class Signals : public SystemElementContainer public: Signals(void); Signals(std::vector signals); + void add(Signal *signal) override; ~Signals(); }; diff --git a/src/system/syselmts.hpp b/src/system/syselmts.hpp index 92a7259..cf3625f 100644 --- a/src/system/syselmts.hpp +++ b/src/system/syselmts.hpp @@ -32,13 +32,13 @@ template class SystemElementContainer : public SystemElement { public: - using MapType = unordered_map; // Map to store elements by name. - using iterator = typename MapType::iterator; // Iterator for the map. + using MapType = unordered_map; // Map to store elements by name. + using iterator = typename MapType::iterator; // Iterator for the map. using const_iterator = typename MapType::const_iterator; // Const iterator for the map. -private: +protected: unsigned int iter_count; // Counter for iterations (currently unused). - MapType content; // Container for storing elements. + MapType content; // Container for storing elements. /** * @brief Adds elements from a map to the container. @@ -69,6 +69,35 @@ public: add(elements); } + /** + * @brief Adds a single element to the container with a given name. + * @param name Name of the element to add. + * @param element Pointer to the element to add. + * @throws runtime_error If the element's name is empty or already exists. + */ + virtual void add(string name, T *element) + { + if ("" == name) + { + throw runtime_error("System elements with empty names are forbidden"); + } + if (exists(name)) + { + throw runtime_error("System elements of same names are forbidden"); + } + content.insert({name, element}); + } + + /** + * @brief Adds a single element to the container. + * @param element Pointer to the element to add. + * @throws runtime_error If the element's name is empty or already exists. + */ + virtual void add(T *element) + { + add (element->name, element); + } + /** * @brief Checks if an element exists in the container. * @param name Name of the element to check. @@ -87,24 +116,6 @@ public: } } - /** - * @brief Adds a single element to the container. - * @param element Pointer to the element to add. - * @throws runtime_error If the element's name is empty or already exists. - */ - void add(T *element) - { - if ("" == element->name) - { - throw runtime_error("System elements with empty names are forbidden"); - } - if (exists(element->name)) - { - throw runtime_error("System elements of same names are forbidden"); - } - content.insert({element->name, element}); - } - /** * @brief Adds elements from another container. * @param elements Pointer to another container. @@ -150,14 +161,15 @@ public: * @param name Name of the element. * @return Pointer to the merged or newly created element. */ - T* merge(string name) + T *merge(string name) { - if (exists(name)) { + if (exists(name)) + { return get(name); } else { - T* ret = new T(name); + T *ret = new T(name); add(ret); return ret; } @@ -167,25 +179,25 @@ public: * @brief Returns an iterator to the beginning of the container. * @return Iterator to the beginning. */ - iterator begin() { return content.begin(); } + iterator begin() { return std::begin(content); } /** * @brief Returns an iterator to the end of the container. * @return Iterator to the end. */ - iterator end() { return content.end(); } + iterator end() { return std::end(content); } /** * @brief Returns a constant iterator to the beginning of the container. * @return Constant iterator to the beginning. */ - const_iterator begin() const { return content.begin(); } + const_iterator begin() const { return std::begin(content); } /** * @brief Returns a constant iterator to the end of the container. * @return Constant iterator to the end. */ - const_iterator end() const { return content.end(); } + const_iterator end() const { return std::end(content); } }; #endif \ No newline at end of file diff --git a/src/system/system.cpp b/src/system/system.cpp index d0f2e34..a92fb4a 100644 --- a/src/system/system.cpp +++ b/src/system/system.cpp @@ -1,28 +1,43 @@ #include "system.hpp" + +#include "connect.hpp" +#include "modules.hpp" #include "imports/import_mentor.hpp" -System::System(): mods(nullptr), conns(nullptr) { +System::System() : mods(nullptr), conns(nullptr) +{ mods = new Modules(); conns = new Connections(); } -System::~System() { +System::~System() +{ delete mods; delete conns; } -void System::Load(std::string module_name, std::string file_name, ImportType type) { +void System::Load(std::string module_name, std::string file_name, ImportType type) +{ ImportBase *imp; + Module *mod = nullptr; Parts *prts = nullptr; - Module mod = Module(module_name); + if (!mods->exists(module_name)) + { + mod = new Module(module_name); + mods->add(mod); + } + else + { + mod = mods->get(module_name); + } switch (type) { - case ImportType::IMPORT_MENTOR : + case ImportType::IMPORT_MENTOR: imp = new ImportMentor(file_name); - imp->parse(); + imp->parse(mod->signals); prts = imp->parts(); - mod.add(prts); + mod->add(prts); break; default: diff --git a/src/system/system.hpp b/src/system/system.hpp index ed391c2..ea4e965 100644 --- a/src/system/system.hpp +++ b/src/system/system.hpp @@ -1,8 +1,12 @@ #ifndef _SYSTEM_HPP_ #define _SYSTEM_HPP_ -#include "connect.hpp" -#include "modules.hpp" +#include "imports/import_base.hpp" + +#pragma once +class Modules; ///< Forward declaration of the Modules class. +class Connections; ///< Forward declaration of the Connections class. + enum class ImportType { IMPORT_MENTOR,