修复了单例的bug

Signed-off-by: facat@lab.com <facat@lab.com>
This commit is contained in:
facat@lab.com 2014-11-26 20:49:33 +08:00
parent 53416f8f33
commit 38025aa1fc
7 changed files with 52 additions and 10 deletions

3
.gitignore vendored
View File

@ -1,6 +1,9 @@
*.pro.user*
*.bak
*.orig
*e.Debug
*e.Release
core/object_script.core.Debug
debug
release
Makefile*

View File

@ -84,7 +84,14 @@ bool ElementHashtable::goPath()
//找Terminal的东西
Terminal *terminalP=static_cast<Terminal *>(ht[terminal]);
std::cout<<terminalP->getID().toStdString()<<std::endl;
// std::cout<<terminalP->getConductingEquipment().toStdString()<<std::endl;
QString node;
node=terminalP->getConnectivityNode();
std::cout<<node.toStdString()<<std::endl;
QString anotherTerminal;
anotherTerminal=this->nodeToTerminal.value(node);
// std::cout<<"anotherTerminal"<<anotherTerminal.toStdString()<<std::endl;
Terminal *anotherTerminalP=static_cast<Terminal *>(ht[anotherTerminal]);
std::cout<<anotherTerminalP->getConductingEquipment().toStdString()<<std::endl;
}
}
return true;

View File

@ -17,6 +17,7 @@
#include "disconnector.h"
#include "powertransformer.h"
#include "synchronousmachine.h"
#include "nodetoterminal.h"
class ElementHashtable : public QObject
{
Q_OBJECT
@ -27,6 +28,7 @@ public:
private:
bool child(QXmlStreamReader &reader);
QHash<QString,BasicElementInfo *> eleHT;
NodeToTerminal nodeToTerminal;
signals:
public slots:

View File

@ -1,10 +1,13 @@
#include "nodetoterminal.h"
#include <iostream>
QHash<QString,QString> *NodeToTerminal::ht=NULL;
NodeToTerminal::NodeToTerminal()
{
if(!NodeToTerminal::ht)
{
// std::cout<<"create"<<std::endl;
NodeToTerminal::ht=new QHash<QString,QString>;
// std::cout<<this->value("").toStdString()<<std::endl;
}
}
@ -20,14 +23,24 @@ bool NodeToTerminal::contains(const QString& key)
QString NodeToTerminal::value(const QString& key)
{
// if(NodeToTerminal::ht)
// {
// //return NodeToTerminal::ht->value(key);
// return "not null";
// }
// else
// {
// return "null";
// }
// return "";
return NodeToTerminal::ht->value(key);
}
NodeToTerminal::~NodeToTerminal()
{
if(NodeToTerminal::ht)
{
delete NodeToTerminal::ht;
NodeToTerminal::ht=NULL;
}
// if(NodeToTerminal::ht)
// {
// delete NodeToTerminal::ht;
// NodeToTerminal::ht=NULL;
// }
}

View File

@ -6,13 +6,23 @@
class NodeToTerminal
{
public:
explicit NodeToTerminal();
NodeToTerminal();
~NodeToTerminal();
void add(const QString& key,const QString& value);
bool contains(const QString& key);
QString value(const QString& key);
private:
static QHash<QString,QString> *ht;
class CG // 它的唯一工作就是在析构函数中删除CSingleton的实例
{
public:
~CG()
{
if (NodeToTerminal::ht)
delete NodeToTerminal::ht;
}
};
static CG Garbo; // 定义一个静态成员,在程序结束时,系统会调用它的析构函数
static QHash<QString,QString> *ht;
};

View File

@ -1,5 +1,5 @@
#include "terminal.h"
#include <iostream>
Terminal::Terminal(QObject *parent):BasicElementInfo(parent)
{
}
@ -34,8 +34,14 @@ QString Terminal::getConductingEquipment()
return this->conductingEquipment;
}
QString Terminal::getConnectivityNode()
{
return this->connectivityNode;
}
bool Terminal::parse(QXmlStreamReader& reader)
{
return this->parseBasicInfo(reader);
}

View File

@ -9,6 +9,7 @@ public:
explicit Terminal(QObject *parent = 0);
virtual bool parse(QXmlStreamReader& reader);
QString getConductingEquipment();
QString getConnectivityNode();
protected:
virtual bool derivedParse(QXmlStreamReader& reader);
QString conductingEquipment;