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)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(essim
LANGUAGES CXX
VERSION 0.1

View File

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

View File

@@ -1,67 +1,93 @@
#include "import_mentor.hpp"
#include "system/parts.hpp"
#include "system/pins.hpp"
#include "system/parts.hpp"
#include <vector>
#include <string>
#include <regex>
enum class State {
using namespace std;
enum class State
{
NO_PART,
IS_PART,
};
ImportMentor::ImportMentor(): ImportBase() {
ImportMentor::ImportMentor(string filename) : ImportBase(filename) {}
}
ImportMentor::~ImportMentor() {
ImportMentor::~ImportMentor()
{
ImportBase::~ImportBase();
}
void ImportMentor::parse(std::fstream lines) {
std::string line;
void ImportMentor::parse()
{
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:");
const regex name_regex("'([^'\\s][^']*)'");
const regex part_regex("^COMP:");
const regex pin_regex("^\\s*Explicit Pin:");
while (std::getline(lines, line))
bool is_name_match = false;
bool is_part_match = false;
bool is_pin_match = false;
Part *prt = nullptr;
while (getline(file_lines, line))
{
Part *prt = nullptr;
std::smatch name_match;
bool is_name_match = std::regex_match(line, name_match, part_regex);
bool is_part_match = std::regex_match(line, part_regex);
bool is_pin_match = std::regex_match(line, pin_regex);
vector<string> names;
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)
{
case State::NO_PART:
if (is_part_match) {
if (is_name_match) {
prt = new Part(name_match[0]);
if (is_part_match)
{
if (is_name_match)
{
prt = new Part(names[1]);
state = State::IS_PART;
}
}
break;
default:
if (is_part_match) {
if (is_name_match) {
prts->add(*prt);
prt = new Part(name_match[0]);
if (is_part_match)
{
if (is_name_match)
{
prts->add(prt);
prt = new Part(names[1]);
}
}
else if (is_pin_match)
{
if (is_name_match) {
auto pin = new Pin(name_match[0]);
prt->add(*pin);
if (is_name_match)
{
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;
}
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
{
void parse(std::fstream lines);
public:
ImportMentor();
ImportMentor(std::string filename);
void parse() override;
~ImportMentor();
};

View File

@@ -1,18 +1,10 @@
#include "system/signals.hpp"
#include "system/system.hpp"
using namespace std;
int main() {
auto sigs = Signals();
Signal *sig = nullptr;
int i=0;
string sname = "";
while (i < 10) {
sname = "signal" + to_string(i);
sig = new Signal(sname);
sigs.add(*sig);
i++;
}
System sys = System();
string ment_filename = "/home/francois/Projets/twinsys/test/BPB-2177_Netlist_2_3.qcv";
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(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:
Connections(void);
Connections(std::vector<Connection> connections);
Connections(std::vector<Connection *> connections);
};
#endif // _CONNECT_HPP_

View File

@@ -1,7 +1,7 @@
#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(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 "parts.hpp"
#include "signals.hpp"
class Module : public SystemElementContainer<Part>
{
public:
Signals sigs;
Module(std::string name);
};
@@ -14,7 +16,7 @@ class Modules : public SystemElementContainer<Module>
{
public:
Modules(void);
Modules(std::vector<Module> pins);
Modules(std::vector<Module *> pins);
};
#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(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:
Parts(void);
Parts(std::vector<Part> parts);
Parts(std::vector<Part *> parts);
};
#endif // _PARTS_HPP_

View File

@@ -1,7 +1,16 @@
#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(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_
#include "syselmts.hpp"
#include "signals.hpp"
class Pin : public SystemElement
{
Signal *sig;
public:
Pin(std::string name);
bool connected();
void connect(Signal *signal);
};
class Pins : public SystemElementContainer<Pin>
{
public:
Pins(void);
Pins(std::vector<Pin> pins);
Pins(std::vector<Pin *> pins);
};
#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(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_
#include "syselmts.hpp"
#include "pins.hpp"
#pragma once
class Pin;
class Signal : public SystemElementContainer<Pin>
{
@@ -14,7 +16,7 @@ class Signals : public SystemElementContainer<Signal>
{
public:
Signals(void);
Signals(std::vector<Signal> signals);
Signals(std::vector<Signal *> signals);
};
#endif // _SIGNALS_HPP_

View File

@@ -6,65 +6,82 @@
#include <unordered_map>
#include <stdexcept>
using namespace std;
class SystemElement
{
public:
std::string name;
SystemElement(std::string name) : name(name) {};
string name;
SystemElement(string name) : name(name) {};
};
template <typename T>
class SystemElementContainer : public SystemElement
{
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;
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:
SystemElementContainer(std::string name) : SystemElement(name) {};
SystemElementContainer(std::string name, std::vector<T> elements) : SystemElement(name)
SystemElementContainer(string name) : SystemElement(name) {};
SystemElementContainer(string name, vector<T *> elements) : SystemElement(name)
{
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");
}
if (exists(element->name))
{
throw runtime_error("System elements of same names are forbidden");
}
content.insert({element->name, element});
}
void add(std::vector<T> elements)
void add(SystemElementContainer<T> *elements)
{
add(elements->content);
}
void add(vector<T*> elements)
{
for (auto &element : elements)
{
add(element);
}
}
T *get(std::string name)
T *get(string name)
{
auto it = content.find(name);
if (it != content.end())
{
return &it->second;
return it->second;
}
else
{
return nullptr;
throw runtime_error("System elements not found");
}
}
};