refactoring. Everything is pointer.

This commit is contained in:
2025-04-21 12:18:26 +02:00
parent 3b8bb88fbb
commit d8122d19a2
16 changed files with 144 additions and 85 deletions

View File

@@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.12) cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(essim project(essim
LANGUAGES CXX LANGUAGES CXX
VERSION 0.1 VERSION 0.1

View File

@@ -4,27 +4,31 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include "system/connect.hpp"
#include "system/parts.hpp" #include "system/parts.hpp"
#include "system/signals.hpp"
class ImportBase class ImportBase
{ {
protected: protected:
Parts *prts; Parts *prts;
virtual void parse(std::fstream lines) = 0; Signals *sigs;
std::fstream file_lines;
public: public:
ImportBase() ImportBase(std::string file_name) : file_lines(std::fstream(file_name))
{ {
prts = new Parts(); prts = new Parts();
} sigs = new Signals();
void load(std::string file_name) {
std::ifstream file(file_name);
}; };
virtual void parse() = 0;
Parts * parts() Parts * parts()
{ {
return prts; return prts;
} }
Signals * signals()
{
return sigs;
}
virtual ~ImportBase() = default;
}; };
#endif // _IMPORT_BASE_HPP_ #endif // _IMPORT_BASE_HPP_

View File

@@ -1,67 +1,93 @@
#include "import_mentor.hpp" #include "import_mentor.hpp"
#include "system/parts.hpp"
#include "system/pins.hpp" #include "system/pins.hpp"
#include "system/parts.hpp"
#include <vector>
#include <string> #include <string>
#include <regex> #include <regex>
enum class State { using namespace std;
enum class State
{
NO_PART, NO_PART,
IS_PART, IS_PART,
}; };
ImportMentor::ImportMentor(): ImportBase() { ImportMentor::ImportMentor(string filename) : ImportBase(filename) {}
} ImportMentor::~ImportMentor()
{
ImportMentor::~ImportMentor() {
ImportBase::~ImportBase(); ImportBase::~ImportBase();
} }
void ImportMentor::parse(std::fstream lines) { void ImportMentor::parse()
std::string line;
auto state = State::NO_PART;
const std::regex name_regex("\\'([^\\']*)\\'");
const std::regex part_regex("^COMP:");
const std::regex pin_regex("^Explicit Pin:");
while (std::getline(lines, line))
{ {
string line;
auto state = State::NO_PART;
const regex name_regex("'([^'\\s][^']*)'");
const regex part_regex("^COMP:");
const regex pin_regex("^\\s*Explicit Pin:");
bool is_name_match = false;
bool is_part_match = false;
bool is_pin_match = false;
Part *prt = nullptr; Part *prt = nullptr;
std::smatch name_match; while (getline(file_lines, line))
bool is_name_match = std::regex_match(line, name_match, part_regex); {
bool is_part_match = std::regex_match(line, part_regex); vector<string> names;
bool is_pin_match = std::regex_match(line, pin_regex); for (sregex_iterator it(line.begin(), line.end(), name_regex), end; it != end; ++it)
{
names.push_back((*it)[1]);
}
is_name_match = (names.size() > 0);
is_part_match = regex_search(line, part_regex);
is_pin_match = regex_search(line, pin_regex);
switch (state) switch (state)
{ {
case State::NO_PART: case State::NO_PART:
if (is_part_match) { if (is_part_match)
if (is_name_match) { {
prt = new Part(name_match[0]); if (is_name_match)
{
prt = new Part(names[1]);
state = State::IS_PART; state = State::IS_PART;
} }
} }
break; break;
default: default:
if (is_part_match) { if (is_part_match)
if (is_name_match) { {
prts->add(*prt); if (is_name_match)
prt = new Part(name_match[0]); {
prts->add(prt);
prt = new Part(names[1]);
} }
} }
else if (is_pin_match) else if (is_pin_match)
{ {
if (is_name_match) { if (is_name_match)
auto pin = new Pin(name_match[0]); {
prt->add(*pin); auto pin = new Pin(names[0]);
Signal *s = nullptr;
prt->add(pin);
if (!sigs->exists(names[2]))
{
s = new Signal(names[2]);
sigs->add(s);
} else {
s = sigs->get(names[2]);
};
pin->connect(s);
} }
} }
break; break;
} }
if (state == State::IS_PART) { }
prts->add(*prt); if (state == State::IS_PART)
} {
prts->add(prt);
} }
} }

View File

@@ -7,9 +7,9 @@
class ImportMentor : public ImportBase class ImportMentor : public ImportBase
{ {
void parse(std::fstream lines);
public: public:
ImportMentor(); ImportMentor(std::string filename);
void parse() override;
~ImportMentor(); ~ImportMentor();
}; };

View File

@@ -1,18 +1,10 @@
#include "system/signals.hpp" #include "system/system.hpp"
using namespace std; using namespace std;
int main() { int main() {
auto sigs = Signals(); System sys = System();
Signal *sig = nullptr; string ment_filename = "/home/francois/Projets/twinsys/test/BPB-2177_Netlist_2_3.qcv";
int i=0;
string sname = "";
while (i < 10) {
sname = "signal" + to_string(i);
sig = new Signal(sname);
sigs.add(*sig);
i++;
}
sys.Load("bpb", ment_filename, ImportType::IMPORT_MENTOR);
}; };

View File

@@ -4,4 +4,4 @@ Connection::Connection(std::string name) : SystemElement(name) {};
Connections::Connections(void): SystemElementContainer<Connection>("connections") {} Connections::Connections(void): SystemElementContainer<Connection>("connections") {}
Connections::Connections(std::vector<Connection> conns): SystemElementContainer<Connection>("connections", conns) {} Connections::Connections(std::vector<Connection *> conns): SystemElementContainer<Connection>("connections", conns) {}

View File

@@ -13,7 +13,7 @@ class Connections : public SystemElementContainer<Connection>
{ {
public: public:
Connections(void); Connections(void);
Connections(std::vector<Connection> connections); Connections(std::vector<Connection *> connections);
}; };
#endif // _CONNECT_HPP_ #endif // _CONNECT_HPP_

View File

@@ -1,7 +1,7 @@
#include "modules.hpp" #include "modules.hpp"
Module::Module(std::string name) : SystemElementContainer<Part>(name) {}; Module::Module(std::string name) : SystemElementContainer<Part>(name) {}
Modules::Modules(void): SystemElementContainer<Module>("modules") {} Modules::Modules(void): SystemElementContainer<Module>("modules") {}
Modules::Modules(std::vector<Module> modules): SystemElementContainer<Module>("modules", modules) {} Modules::Modules(std::vector<Module *> modules): SystemElementContainer<Module>("modules", modules) {}

View File

@@ -3,10 +3,12 @@
#include "syselmts.hpp" #include "syselmts.hpp"
#include "parts.hpp" #include "parts.hpp"
#include "signals.hpp"
class Module : public SystemElementContainer<Part> class Module : public SystemElementContainer<Part>
{ {
public: public:
Signals sigs;
Module(std::string name); Module(std::string name);
}; };
@@ -14,7 +16,7 @@ class Modules : public SystemElementContainer<Module>
{ {
public: public:
Modules(void); Modules(void);
Modules(std::vector<Module> pins); Modules(std::vector<Module *> pins);
}; };
#endif // _MODULES_HPP_ #endif // _MODULES_HPP_

View File

@@ -4,4 +4,4 @@ Part::Part(std::string name) : SystemElementContainer<Pin>(name) {};
Parts::Parts(void): SystemElementContainer<Part>("parts") {} Parts::Parts(void): SystemElementContainer<Part>("parts") {}
Parts::Parts(std::vector<Part> parts): SystemElementContainer<Part>("parts", parts) {} Parts::Parts(std::vector<Part *> parts): SystemElementContainer<Part>("parts", parts) {}

View File

@@ -14,7 +14,7 @@ class Parts : public SystemElementContainer<Part>
{ {
public: public:
Parts(void); Parts(void);
Parts(std::vector<Part> parts); Parts(std::vector<Part *> parts);
}; };
#endif // _PARTS_HPP_ #endif // _PARTS_HPP_

View File

@@ -1,7 +1,16 @@
#include "pins.hpp" #include "pins.hpp"
Pin::Pin(std::string name) : SystemElement(name) {}; Pin::Pin(std::string name) : SystemElement(name), sig(nullptr) {};
bool Pin::connected() {
return sig != nullptr;
}
void Pin::connect(Signal *signal)
{
sig = signal;
}
Pins::Pins(void): SystemElementContainer<Pin>("pins") {} Pins::Pins(void): SystemElementContainer<Pin>("pins") {}
Pins::Pins(std::vector<Pin> pins): SystemElementContainer<Pin>("pins", pins) {} Pins::Pins(std::vector<Pin *> pins): SystemElementContainer<Pin>("pins", pins) {}

View File

@@ -2,18 +2,22 @@
#define _PINS_HPP_ #define _PINS_HPP_
#include "syselmts.hpp" #include "syselmts.hpp"
#include "signals.hpp"
class Pin : public SystemElement class Pin : public SystemElement
{ {
Signal *sig;
public: public:
Pin(std::string name); Pin(std::string name);
bool connected();
void connect(Signal *signal);
}; };
class Pins : public SystemElementContainer<Pin> class Pins : public SystemElementContainer<Pin>
{ {
public: public:
Pins(void); Pins(void);
Pins(std::vector<Pin> pins); Pins(std::vector<Pin *> pins);
}; };
#endif // _PINS_HPP_ #endif // _PINS_HPP_

View File

@@ -5,5 +5,5 @@ Signal::Signal(std::string name) : SystemElementContainer<Pin>(name) {};
Signals::Signals(void): SystemElementContainer<Signal>("signals") {} Signals::Signals(void): SystemElementContainer<Signal>("signals") {}
Signals::Signals(std::vector<Signal> signals): SystemElementContainer<Signal>("signals", signals) {} Signals::Signals(std::vector<Signal *> signals): SystemElementContainer<Signal>("signals", signals) {}

View File

@@ -2,7 +2,9 @@
#define _SIGNALS_HPP_ #define _SIGNALS_HPP_
#include "syselmts.hpp" #include "syselmts.hpp"
#include "pins.hpp"
#pragma once
class Pin;
class Signal : public SystemElementContainer<Pin> class Signal : public SystemElementContainer<Pin>
{ {
@@ -14,7 +16,7 @@ class Signals : public SystemElementContainer<Signal>
{ {
public: public:
Signals(void); Signals(void);
Signals(std::vector<Signal> signals); Signals(std::vector<Signal *> signals);
}; };
#endif // _SIGNALS_HPP_ #endif // _SIGNALS_HPP_

View File

@@ -6,65 +6,82 @@
#include <unordered_map> #include <unordered_map>
#include <stdexcept> #include <stdexcept>
using namespace std;
class SystemElement class SystemElement
{ {
public: public:
std::string name; string name;
SystemElement(std::string name) : name(name) {}; SystemElement(string name) : name(name) {};
}; };
template <typename T> template <typename T>
class SystemElementContainer : public SystemElement class SystemElementContainer : public SystemElement
{ {
private: private:
static_assert(std::is_base_of<SystemElement, T>::value, "T shall be a system element descendant !"); //static_assert(is_base_of<SystemElement, T>::value, "T shall be a system element descendant !");
unsigned int iter_count; unsigned int iter_count;
std::unordered_map<std::string, T> content; unordered_map<string, T*> content;
bool name_exists(std::string name) void add(unordered_map<string, T*> el_content)
{ {
return get(name) != nullptr; for (const auto &[key, value] : el_content)
{
add(value);
}
} }
public: public:
SystemElementContainer(std::string name) : SystemElement(name) {}; SystemElementContainer(string name) : SystemElement(name) {};
SystemElementContainer(std::string name, std::vector<T> elements) : SystemElement(name) SystemElementContainer(string name, vector<T *> elements) : SystemElement(name)
{ {
add(elements); add(elements);
} }
void add(T element) bool exists(string name)
{ {
if ("" == element.name) try
{ {
throw std::runtime_error("System elements with empty names are forbidden"); get(name);
return true;
} }
if (name_exists(element.name)) catch (const exception &e)
{ {
throw std::runtime_error("System elements of same names are forbidden"); return false;
} }
content.insert({element.name, element});
} }
void add(SystemElementContainer<T> elements) void add(T* element)
{ {
add(elements.content); if ("" == element->name)
{
throw runtime_error("System elements with empty names are forbidden");
} }
void add(std::vector<T> elements) if (exists(element->name))
{
throw runtime_error("System elements of same names are forbidden");
}
content.insert({element->name, element});
}
void add(SystemElementContainer<T> *elements)
{
add(elements->content);
}
void add(vector<T*> elements)
{ {
for (auto &element : elements) for (auto &element : elements)
{ {
add(element); add(element);
} }
} }
T *get(std::string name) T *get(string name)
{ {
auto it = content.find(name); auto it = content.find(name);
if (it != content.end()) if (it != content.end())
{ {
return &it->second; return it->second;
} }
else else
{ {
return nullptr; throw runtime_error("System elements not found");
} }
} }
}; };