Skip to content

Commit

Permalink
netlist improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tectu committed Nov 27, 2022
1 parent f8df442 commit 4ba0dcc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 56 deletions.
6 changes: 3 additions & 3 deletions demo/netlist/model.cpp
Expand Up @@ -18,16 +18,16 @@ void Model::setNetlist(const QSchematic::Netlist<Operation*, OperationConnector*
clear();

// Make sure that there are nets available
if ( netlist.nets().empty() ) {
if ( netlist.nets.empty() ) {
return;
}

// Create three model
{
Q_ASSERT( _rootItem );
beginInsertRows( QModelIndex(), 0, static_cast<int>( netlist.nets().size() )-1 );
beginInsertRows( QModelIndex(), 0, static_cast<int>( netlist.nets.size() )-1 );

for ( const auto& net : netlist.nets() ) {
for ( const auto& net : netlist.nets ) {

// Net
TreeItem* netItem = new TreeItem( { net.name, pointerToString( &net ) } );
Expand Down
65 changes: 28 additions & 37 deletions qschematic/netlist.h
Expand Up @@ -20,7 +20,11 @@ namespace QSchematic
class Node;
class Connector;

template<typename TWire = Wire*, typename TNode = Node*, typename TConnector = Connector*>
template<
typename TWire = Wire*,
typename TNode = Node*,
typename TConnector = Connector*
>
struct Net
{
QString name;
Expand All @@ -30,34 +34,42 @@ namespace QSchematic
std::map<TConnector, TNode> connectorNodePairs;
};

template<typename TNode = Node*, typename TConnector = Connector*, typename TWire = Wire*, typename TNet = Net<TWire, TNode, TConnector>>
template<
typename TNode = Node*,
typename TConnector = Connector*,
typename TWire = Wire*,
typename TNet = Net<TWire, TNode, TConnector>
>
class Netlist
{
public:
Netlist( ) = default;
std::vector<TNode> nodes;
std::vector<TNet> nets;

Netlist() = default;
Netlist(const Netlist& other) = default;
Netlist(Netlist&& other) = default;
virtual ~Netlist() = default;

Netlist<TNode, TConnector, TWire, TNet>& operator=(const Netlist<TNode, TConnector, TWire, TNet>& rhs) = default;

QJsonObject toJson() const
QJsonObject
toJson() const
{
QJsonObject object;

// Nets
QJsonArray netsArray;
for (const auto& net : _nets) {
for (const auto& net : nets) {
QJsonObject netObject;

// Net name
netObject.insert("name", net.name);

// Connectors
QJsonArray connectorsArray;
for (const auto& connector : net.connectors) {
for (const auto& connector : net.connectors)
connectorsArray.append(connector->label()->text());
}
netObject.insert("connectors", connectorsArray);

// ConnectorNodePairs
Expand All @@ -76,27 +88,16 @@ namespace QSchematic
return object;
}

void set( std::vector<TNode>&& nodes, std::vector<TNet>&& nets )
{
_nodes = std::move( nodes );
_nets = std::move( nets );
}

std::vector<TNet> nets() const
{
return _nets;
}

std::forward_list<TNet> netsWithNode(const TNode node) const
std::forward_list<TNet>
netsWithNode(const TNode node) const
{
// Sanity check
if (!node) {
if (!node)
return { };
}

// Loop
std::forward_list<TNet> nets;
for (auto& net : _nets) {
for (auto& net : nets) {
for (auto& connectorWithNode : net.connectorWithNodes) {
if (connectorWithNode._node == node) {
nets << net;
Expand All @@ -108,32 +109,22 @@ namespace QSchematic
return nets;
}

std::optional<TNet> netFromConnector(const TConnector connector) const
std::optional<TNet>
netFromConnector(const TConnector connector) const
{
// Sanity check
if (not connector) {
if (connector)
return std::nullopt;
}

// Loop
for (auto& net : _nets) {
for (auto& net : nets) {
for (auto& c : net.connectors) {
if (c == connector) {
if (c == connector)
return net;
}
}
}

return std::nullopt;
}

std::vector<TNode> nodes() const
{
return _nodes;
}

private:
std::vector<TNode> _nodes;
std::vector<TNet> _nets;
};
}
34 changes: 18 additions & 16 deletions qschematic/netlistgenerator.h
Expand Up @@ -15,8 +15,15 @@ namespace QSchematic
class NetlistGenerator
{
public:
template<typename TNode = Node*, typename TConnector = Connector*, typename TWire = Wire*, typename TNet = Net<TWire, TNode, TConnector>>
static bool generate(Netlist<TNode, TConnector, TWire, TNet>& netlist, const Scene& scene)
template<
typename TNode = Node*,
typename TConnector = Connector*,
typename TWire = Wire*,
typename TNet = Net<TWire, TNode, TConnector>
>
static
bool
generate(Netlist<TNode, TConnector, TWire, TNet>& netlist, const Scene& scene)
{
struct GlobalNet
{
Expand All @@ -26,11 +33,10 @@ namespace QSchematic

// Add all nodes
std::vector<TNode> nodes;
for ( const auto& node : scene.nodes() ) {
for (const auto& node : scene.nodes()) {
// Sanity check
if ( !node ) {
if (!node)
continue;
}

nodes.push_back( static_cast<TNode>( node.get() ) );
}
Expand All @@ -43,9 +49,8 @@ namespace QSchematic
auto wireNet = std::dynamic_pointer_cast<WireNet>(net);

// Sanity check
if (!wireNet) {
if (!wireNet)
continue;
}

// Figure out whether we need a new global net
bool createNewNet = true;
Expand All @@ -68,9 +73,8 @@ namespace QSchematic
newGlobalNet.name = wireNet->name();

// Prevent empty names
if (newGlobalNet.name.isEmpty()) {
if (newGlobalNet.name.isEmpty())
newGlobalNet.name = QString("N%1").arg(anonNetCounter++, 3, 10, QChar('0'));
}

globalNets.push_back(newGlobalNet);
}
Expand All @@ -91,27 +95,24 @@ namespace QSchematic
// Store wires
for ( const auto& wire : wireNet->wires()) {
TWire w = qobject_cast<TWire>( std::dynamic_pointer_cast<Wire>(wire).get() );
if ( w ) {
if (w)
net.wires.push_back( w );
}
}
}

// Build a list of all connectors
for (auto& node : scene.nodes()) {
// Convert to template node type
TNode templateNode = qgraphicsitem_cast<TNode>(node.get());
if (!templateNode) {
if (!templateNode)
continue;
}

// Loop through all Node's connectors
for (auto& connector : node->connectors()) {
// Convert to template connector type
TConnector templateConnector = qgraphicsitem_cast<TConnector>(connector.get());
if (!templateConnector) {
if (!templateConnector)
continue;
}

// Create the Connector/Node pairs
const auto* wire = scene.wire_manager()->attached_wire(connector.get());
Expand All @@ -133,7 +134,8 @@ namespace QSchematic
}

// Set the netlist
netlist.set( std::move( nodes ), std::move( nets ) );
netlist.nodes = std::move(nodes);
netlist.nets = std::move(nets);

return true;
}
Expand Down

0 comments on commit 4ba0dcc

Please sign in to comment.