- Timestamp:
- Jul 31, 2013, 11:20:11 AM (11 years ago)
- Location:
- branches/2.0-beta1
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.0-beta1/CODING_STYLE.txt
r89 r146 1 Fo r the aspects not explained in this document, follow the coding style described in:2 http:// llvm.org/docs/CodingStandards.html1 Follow the coding style described in: 2 http://www.webkit.org/coding/coding-style.html 3 3 4 == #include Style == 5 * Avoid #includes in the header files. Use forward declarations if possible. 6 * Immediately after the header file comment (and include guards if working on a header file), the minimal list of #includes required by the file should be listed. We prefer these #includes to be listed in this order: 7 1) Main Module Header 8 2) Local/Private Headers 9 3) <Pacpus/...> 10 4) System #includes 11 * do not use 'using ...;' in header files 12 13 == Mechanical Source Issues == 14 * use standard header-guard: 15 #ifndef PACPUS_EXAMPLE_H 16 #define PACPUS_EXAMPLE_H 17 ... 18 #endif // PACPUS_EXAMPLE_H 19 * use C++-style comments (//) 20 * ALWAYS use curly brackets with statements if, for, etc. 21 * use curly brackets {} as follows: 22 * on the same line with: 23 - namespace { 24 - statements if, for, etc. 25 if (cond) { 26 } else if (cond2) { 27 } else { 28 } 29 * on the next line with: 30 - classes 31 - function and method definitions 32 - local scope blocks 33 class A 34 { 35 void fun A; 36 37 struct AB 38 { 39 }; 40 41 void fun A 42 { 43 } 44 }; 45 * write Doxygen-style comments as much as possible, put it in the header file if possible, add developer-only comments in source file 46 * use Java-style Doxygen comments, i.e. @returns over \returns 47 * use preprocessor directives for long-range comments (#if 0 .... #endif) 48 * indent with 4 spaces, do not use tabs 49 * do not indent namespaces 50 * use camelCaseNotation / CamelCaseNotation 51 * end namespace with its name in a comment 52 * start with a lowercase letter for local variables, namespaces, function names, method names, ... 53 * start with an Uppercase for classes, structs, ... 54 * prefix member variables with 'm' 55 * prefix (static) const variables with 'k' 56 * prefix static (non-const) variables with 's' 57 * separate member methods from member variables with a repetion of 'protected:' / 'private:' 58 59 ======================================== 60 Example.h: 61 ======================================== 62 /// File description 63 64 #ifndef PACPUS_EXAMPLE_H 65 #define PACPUS_EXAMPLE_H 66 67 #include "BaseClass.h" 68 69 namespace pacpus { 70 /// Constant description 71 static const double kConstA; 72 /// Static variable description 73 static int sStaticA; 74 75 /// Brief description of this class 76 /// 77 /// Somewhat longer description of this class 78 class SomeClass 79 : public BaseClass 80 { 81 public: 82 SomeClass 83 84 private: 85 void methodA(); 86 87 private: 88 int mMemberA; 89 }; 90 91 } // namespace pacpus 92 93 #endif // PACPUS_EXAMPLE_H 94 95 ======================================== 96 Example.cpp: 97 ======================================== 98 #include "Example.h" 99 100 #include "Something.h" 101 #include "SomethingElse.h" 102 103 #include <Pacpus/kernel/DbiteFile.h> 104 105 #include <iostream> 106 107 using namespace pacpus; 108 109 const double kConstA = 3.14159265; 110 111 SomeClass::SomeClass() 112 : BaseClass() 113 { 114 int someIntegerVariable; 4 If possible, use clang-format tool to format your code: 5 // in-place formatting 6 clang-format -i --style="WebKit" INPUT_FILE 115 7 116 if (a) { 117 a(); 118 } else { 119 b(); 120 } 121 122 if (isConditionSatisfied()) { 123 doSomething(); 124 return 0; 125 } 126 return 1; 127 } 8 // out-of-place formatting 9 clang-format --style="WebKit" INPUT_FILE > OUTPUT_FILE -
branches/2.0-beta1/include/Pacpus/kernel/ComponentManager.h
r89 r146 38 38 39 39 /// Singleton recording the components and managing them. 40 class ComponentManager40 class PACPUSLIB_API ComponentManager 41 41 { 42 42 friend class ComponentBase; … … 45 45 /// @returns a pointer to the ComponentManager object 46 46 /// @deprecated Use getInstance() 47 PACPUS_DEPRECATED_MSG( static PACPUSLIB_APIComponentManager * create(), "use 'getInstance()'" );47 PACPUS_DEPRECATED_MSG( static ComponentManager * create(), "use 'getInstance()'" ); 48 48 49 49 /// Returns an instance of the singleton ComponentManager class. 50 50 /// @returns Pointer to the ComponentManager singleton. 51 static PACPUSLIB_APIComponentManager* getInstance();51 static ComponentManager* getInstance(); 52 52 53 53 /// Destroys the ComponentManager singleton. 54 54 /// 55 55 /// After this call, every pointer to the ComponentManager becomes invalid. 56 static PACPUSLIB_APIvoid destroy();56 static void destroy(); 57 57 58 58 /// Automatic deleter class. … … 72 72 * @return Number of components loaded by the manager. 73 73 */ 74 PACPUSLIB_APIstd::size_t loadComponents(const QString& file);74 std::size_t loadComponents(const QString& file); 75 75 76 76 /** Start all the components 77 77 * @return True if all the component has been started, otherwise false. 78 78 */ 79 PACPUSLIB_APIbool start();79 bool start(); 80 80 81 81 /** Start only the component passed in parameter. … … 83 83 * @return True if the component exists and has been started, otherwise false. 84 84 */ 85 PACPUSLIB_APIbool start(const QString& component);85 bool start(const QString& component); 86 86 87 87 /** Stop all the components 88 88 * @return True if all the component has been stopped, otherwise false. 89 89 */ 90 PACPUSLIB_APIbool stop();90 bool stop(); 91 91 92 92 /** Stop only the component passed in parameter. … … 94 94 * @return True if the component has been stopped, otherwise false. 95 95 */ 96 PACPUSLIB_APIbool stop(const QString& component);96 bool stop(const QString& component); 97 97 98 98 /** Get a pointer to the component referred by @em name. … … 100 100 * @return Pointer to the component if it exists, otherwise @em NULL. 101 101 */ 102 PACPUSLIB_APIComponentBase* getComponent(const QString& name);102 ComponentBase* getComponent(const QString& name); 103 103 104 104 /** Get the list of all the names of the component known by the manager. 105 105 * @return List of all the component's name. 106 106 */ 107 PACPUSLIB_APIQStringList getAllComponentsName() const;107 QStringList getAllComponentsName() const; 108 108 109 109 /** Load a new plugin from the file filename (it may be a .so/.dll file) … … 111 111 * @return True if the plugin has been loaded, otherwise false. 112 112 */ 113 PACPUSLIB_APIbool loadPlugin(const QString& filename);113 bool loadPlugin(const QString& filename); 114 114 115 115 private: 116 bool stop(ComponentBase* component) const; 117 116 118 /// Create a new component of type 'type' and with the name 'name' 117 119 bool createComponent(const QString& type, const QString& name); … … 122 124 bool registerComponentFactory(ComponentFactoryBase* addr, const QString& type); 123 125 124 bool un RegisterComponent(const QString& name);125 bool un RegisterComponentFactory(const QString& type);126 bool unregisterComponent(const QString& name); 127 bool unregisterComponentFactory(const QString& type); 126 128 127 129 // Allow 2 functions to access to private members of ComponentManager -
branches/2.0-beta1/include/Pacpus/kernel/PacpusEvent.h
r131 r146 42 42 43 43 public: 44 PacpusEvent(PacpusEventType type, road_time_t t = road_time(), road_timerange_t tr = 0):QEvent(QEvent::Type(type)),t_(t),tr_(tr) {} 45 virtual ~PacpusEvent() {} 44 PacpusEvent(PacpusEventType type, road_time_t t = road_time(), road_timerange_t tr = 0) 45 : QEvent(QEvent::Type(type)) 46 , t_(t) 47 , tr_(tr) 48 {} 49 virtual ~PacpusEvent() 50 {} 46 51 47 virtual QDataStream& streamOut(QDataStream& out) {return out;}; // NOTE virtual pure ?? 48 virtual QDataStream& streamIn(QDataStream& in) {return in;}; 52 // NOTE virtual pure ?? 53 virtual QDataStream& streamOut(QDataStream& out) 54 { 55 return out; 56 } 57 virtual QDataStream& streamIn(QDataStream& in) 58 { 59 return in; 60 } 49 61 50 p ublic: // TODO make protected62 protected: // TODO make protected 51 63 road_time_t t_; 52 64 road_timerange_t tr_; … … 63 75 QDataStream& streamIn(QDataStream& in) {return in >> (quint64&)t_ >> tr_ /*>> data_*/;} 64 76 65 p ublic: // TODO make protected77 protected: 66 78 T data_; 67 79 }; … … 70 82 { 71 83 public: 72 PacpusGenericEvent(PacpusEventType type, char* data, size_t size):PacpusEvent(type) { 73 data_ = (char*)malloc(size); 84 PacpusGenericEvent(PacpusEventType type, char* data, size_t size) 85 : PacpusEvent(type) 86 { 87 data_ = new char[size]; 74 88 memcpy(data_,data,size); 75 89 _size = size; 76 90 77 91 } 78 virtual ~PacpusGenericEvent() {free(data_);} 92 93 virtual ~PacpusGenericEvent() 94 { 95 delete[] data_; 96 } 97 79 98 char* data_; 80 99 size_t _size; -
branches/2.0-beta1/include/Pacpus/kernel/XmlConfigFile.h
r89 r146 27 27 28 28 #include <QDomElement> 29 #include <QFile> 29 30 #include <QMutex> 30 31 #include <QStringList> … … 47 48 /// @todo Documentation 48 49 static void destroy(); 50 51 /// @returns a list of all names of components declared in the XML tree 52 QStringList getAllComponentsNames() const; 49 53 /// @todo Documentation 50 QDomElement getComponent(QString name); 51 /// @returns a list of all names of components declared in the XML tree 52 QStringList getAllComponentsNames(); 54 QDomElement getComponent(QString name) const; 55 56 // TODO: QStringList getAllConnectionsNames() const; 57 QDomElement getConnection(QString connectionName) const; 58 53 59 /// @todo Documentation 54 QStringList getAllPlugins(); 60 QStringList getAllPluginsNames(); 61 55 62 /// @todo Documentation 56 63 int loadFile(QString fileName); 57 58 QDomNodeList getAllComponents();59 60 QDomNodeList getAllConnections();61 62 QDomElement getConnection(QString connectionName);63 64 64 65 /// @todo Documentation 65 66 /// not used 66 67 void saveFile(QString fileName); 68 67 69 /// @todo Documentation 68 70 /// not used 69 71 void addComponent(QDomElement component); 72 70 73 /// @todo Documentation 74 /// @deprecated Use removeComponent() 71 75 /// not used 72 void delComponent(QDomElement component); 76 PACPUS_DEPRECATED_MSG( void delComponent(QDomElement component), "use removeComponent()" ); 77 void removeComponent(QDomElement component); 73 78 74 79 protected: 80 QDomNodeList getAllComponents() const; 81 QDomNodeList getAllConnections() const; 82 QDomNodeList getAllPlugins(); 83 75 84 private: 76 85 XmlConfigFile(); 77 86 ~XmlConfigFile(); 78 87 79 static XmlConfigFile * _xmlConfigFile;80 81 88 QDomElement createComponent(QString name); 82 89 90 QString libraryExtension() const; 91 QString libraryPrefix() const; 92 QString libraryPostfix() const; 93 83 94 private: 84 QDomDocument _document; 85 QFile * _file; 86 QMutex _mutex; 95 static XmlConfigFile * m_xmlConfigFile; 96 97 QDomDocument m_document; 98 QFile m_file; 99 QMutex m_mutex; 87 100 88 int _numberOfComponents; 101 QString m_libraryExtension; 102 QString m_libraryPrefix; 103 QString m_libraryPostfix; 104 105 int m_numberOfComponents; 89 106 }; 90 107 -
branches/2.0-beta1/src/DBITEPlayer/src/main.cpp
r145 r146 41 41 42 42 #include <cassert> 43 #include <QApplication>44 43 45 44 using namespace pacpus; … … 58 57 59 58 static ComponentFactory<DbtPlyEngine> factoryDbtPlyEngine("DbtPlyEngine"); 60 Q_UNUSED(factoryDbtPlyEngine);59 (void) factoryDbtPlyEngine; // unused 61 60 static ComponentFactory<DbtPlyTrigger> factoryDbtPlyTrigger("DbtPlyTrigger"); 62 Q_UNUSED(factoryDbtPlyTrigger);61 (void) factoryDbtPlyTrigger; // unused 63 62 static ComponentFactory<DbtPlyUserInterface> factoryDbtPlyUserInterface("DbtPlyUserInterface"); 64 Q_UNUSED(factoryDbtPlyUserInterface);63 (void) factoryDbtPlyUserInterface; // unused 65 64 66 65 string configFilePath; -
branches/2.0-beta1/src/PacpusLib/ComponentManager.cpp
r141 r146 27 27 { 28 28 LOG_TRACE("getInstance()"); 29 LOG_TRACE("before: mInstance = " << mInstance); 30 31 if (!mInstance) 32 { 29 LOG_TRACE("before: mInstance = " << mInstance); 30 31 if (!mInstance) { 33 32 LOG_INFO("creating new instance..."); 34 33 mInstance = new ComponentManager(); … … 36 35 } 37 36 38 LOG_TRACE("after :mInstance = " << mInstance);37 LOG_TRACE("after : mInstance = " << mInstance); 39 38 return mInstance; 40 39 } … … 44 43 LOG_TRACE("destroy"); 45 44 45 LOG_TRACE("before: mInstance = " << mInstance); 46 46 delete mInstance; 47 47 mInstance = NULL; 48 LOG_TRACE("after : mInstance = " << mInstance); 48 49 } 49 50 … … 60 61 LOG_TRACE("destructor"); 61 62 62 QMutableMapIterator<ComponentMap::key_type, ComponentMap::mapped_type> it(componentMap_);63 while (it.hasNext())64 unRegisterComponent(it.next().key());63 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) { 64 bool unregisteredSuccessfully = unregisterComponent(it.key()); 65 } 65 66 66 67 LOG_DEBUG("component manager was deleted"); … … 83 84 } 84 85 85 bool ComponentManager::unRegisterComponentFactory(const QString& type) 86 { 87 LOG_TRACE("unRegisterComponentFactory(type="<< type << ")"); 88 89 if (!factoryMap_.contains(type)) 90 { 86 bool ComponentManager::unregisterComponentFactory(const QString& type) 87 { 88 LOG_TRACE("unregisterComponentFactory(type="<< type << ")"); 89 90 if (!factoryMap_.contains(type)) { 91 91 LOG_WARN("cannot unregister component factory '" << type << "'. It was not registered"); 92 92 return false; … … 115 115 } 116 116 117 bool ComponentManager::unRegisterComponent(const QString& name) 118 { 119 LOG_TRACE("unRegisterComponent(name="<< name << ")"); 120 121 if (!componentMap_.contains(name)) 122 { 117 bool ComponentManager::unregisterComponent(const QString& name) 118 { 119 LOG_TRACE("unregisterComponent(name="<< name << ")"); 120 121 if (!componentMap_.contains(name)) { 123 122 LOG_WARN("cannot unregister component '" << name << "'. It was not registered"); 124 123 return false; … … 126 125 127 126 // FIXME: delete component 128 //delete componentMap_[name]; 129 componentMap_.remove(name); 127 ComponentBase* component = componentMap_.value(name, NULL); 128 //delete component; 129 130 // FIXME: remove from map (causes segfault in QMap::qMapLessThanKey on Windows) 131 //componentMap_.remove(name); 130 132 LOG_INFO("unregistered component '" << name << "'"); 131 133 … … 137 139 LOG_TRACE("createComponent(type=" << type << ", " << "name="<< name << ")"); 138 140 139 FactoryMap::iterator it = factoryMap_.find(type);140 if (it != factoryMap_.end())141 {142 (*it)->addComponent(name);141 if (factoryMap_.contains(type)) { 142 ComponentFactoryBase* factory = factoryMap_.value(type); 143 assert(factory); 144 factory->addComponent(name); 143 145 return true; 144 146 } … … 211 213 { 212 214 // Load the plugins containing the components 213 QStringList plugins = xmlTree_->getAllPlugins ();215 QStringList plugins = xmlTree_->getAllPluginsNames(); 214 216 Q_FOREACH (QString plugin, plugins) { 215 217 if (!loadPlugin(plugin)) { 216 218 LOG_WARN("cannot load plugin '" << plugin << "'"); 219 } else { 220 LOG_INFO("successfully loaded plugin '" << plugin << "'"); 217 221 } 218 222 } … … 296 300 297 301 for (int i = 0; i < connectionsNodeList.size(); ++i) { 298 299 302 cfg.localCopy(connectionsNodeList.item(i).toElement()); 300 303 QString connectionInput = cfg.getConnectionInput(); … … 303 306 int connectionPriority = cfg.getConnectionPriority(); 304 307 305 306 308 //TODO set connection mode from string 307 309 … … 309 311 //InputInterfaceBase::NeverSkip; 310 312 //InputInterfaceBase::TimeBounded; 311 312 313 313 314 if (!createConnection(connectionOutput, connectionInput, connectionType,connectionPriority)) { … … 325 326 326 327 bool result = true; 327 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it )328 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) { 328 329 result &= start(it.key()); 330 } 329 331 330 332 return result; … … 355 357 356 358 bool result = true; 357 for (ComponentMap::iterator it = componentMap_.begin(); it != componentMap_.end(); ++it) 358 result &= stop(it.key()); 359 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) { 360 result &= stop(*it); 361 } 359 362 360 363 return result; 361 364 } 362 365 366 bool ComponentManager::stop(ComponentBase* component) const 367 { 368 if (!component) { 369 LOG_WARN("NULL component pointer"); 370 return false; 371 } 372 if (!component->stopComponent()) { 373 return false; 374 } 375 return true; 376 } 377 363 378 bool ComponentManager::stop(const QString& componentName) 364 379 { … … 366 381 367 382 ComponentBase* component = getComponent(componentName); 368 if (!component) 369 { 383 if (!component) { 370 384 LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist"); 371 385 return false; … … 373 387 374 388 LOG_INFO("stopping component '" << componentName << "'..."); 375 if (! component->stopComponent()) {389 if (!stop(component)) { 376 390 LOG_WARN("cannot stop component '" << componentName << "'" << ". It can be already stopped"); 377 391 } -
branches/2.0-beta1/src/PacpusLib/Log.cpp
r144 r146 46 46 { 47 47 if (0 == niftyCounter++) { 48 LOG_INFO("LogConfigurator constructor"); 48 49 init_log_factories(); 49 50 } … … 54 55 if (0 == --niftyCounter) { 55 56 // clean up 57 LOG_INFO("LogConfigurator destructor"); 56 58 } 57 59 } … … 71 73 logging::core::get()->set_filter 72 74 ( 75 #ifdef NDEBUG 76 // release 73 77 logging::trivial::severity >= logging::trivial::debug 78 #else 79 // debug 80 logging::trivial::severity >= logging::trivial::trace 81 #endif 74 82 ); 75 83 … … 95 103 // Create a backend and attach a couple of streams to it 96 104 boost::shared_ptr< sinks::text_ostream_backend > backend = 97 make_shared< sinks::text_ostream_backend >( 98 //keywords::format = "[%TimeStamp%]: %Message%" 99 ); 105 make_shared< sinks::text_ostream_backend >(); 100 106 backend->add_stream( 101 107 shared_ptr< std::ostream >(&std::clog, logging::empty_deleter()) … … 113 119 logging::trivial::severity >= logging::trivial::info 114 120 ); 121 115 122 sink->set_formatter 116 123 ( -
branches/2.0-beta1/src/PacpusLib/XmlConfigFile.cpp
r110 r146 6 6 7 7 #include <Pacpus/kernel/XmlConfigFile.h> 8 8 9 #include <Pacpus/kernel/Log.h> 9 10 … … 11 12 #include <QTextStream> 12 13 14 //////////////////////////////////////////////////////////////////////////////// 15 13 16 using namespace pacpus; 14 17 using namespace std; … … 16 19 DECLARE_STATIC_LOGGER("pacpus.core.XmlConfigFile"); 17 20 18 XmlConfigFile * XmlConfigFile:: _xmlConfigFile = NULL;21 XmlConfigFile * XmlConfigFile::m_xmlConfigFile = NULL; 19 22 20 23 static const string kPropertyPluginList = "list"; … … 22 25 static const string kXmlConfigFilename = "pacpus_config.xml"; 23 26 24 #define rootSection "pacpus" 25 #define componentSection "components" 26 #define componentNode "component" 27 #define connectionSection "connections" 28 #define connectionNode "connection" 29 #define parameterSection "parameters" 30 #define pluginNode "plugin" 31 #define nameAttribute "name" 32 #define libAttribute "lib" 33 27 static const char * kRootSection = "pacpus"; 28 29 static const char * kComponentSection = "components"; 30 static const char * kConnectionSection = "connections"; 31 static const char * kParameterSection = "parameters"; 32 33 static const char * kComponentNode = "component"; 34 static const char * kConnectionNode = "connection"; 35 static const char * kPluginNode = "plugin"; 36 37 static const char * kNameAttribute = "name"; // <component name="MyComponent1" type="MyComponentType"/> 38 static const char * kLibAttribute = "lib"; // <plugin lib="MyLibrary.dll"/> 39 static const char * kExtensionAttribute = "extension"; // <parameter extension=".dll"> 40 static const char * kPrefixAttribute = "prefix"; // <parameter prefix="lib"> 41 static const char * kPostfixAttribute = "postfix"; // <parameter postfix="_d"> 42 43 //////////////////////////////////////////////////////////////////////////////// 44 // helper method 45 QDomNode getNamedItemFromDomDocument(const QDomDocument & document, const char * itemName); 46 47 //////////////////////////////////////////////////////////////////////////////// 48 49 // CTOR/DTOR 34 50 XmlConfigFile::XmlConfigFile() 35 : _file(NULL) 36 , _numberOfComponents(0) 51 : m_numberOfComponents(0) 37 52 { 38 53 LOG_TRACE("constructor"); 39 54 40 55 // create the root of the XML tree 41 _document.appendChild(_document.createElement(rootSection));56 m_document.appendChild(m_document.createElement(kRootSection)); 42 57 // create the sections 43 _document.documentElement().appendChild(_document.createElement(parameterSection)); 44 _document.documentElement().appendChild(_document.createElement(componentSection)); 45 _document.documentElement().appendChild(_document.createElement(connectionSection)); 46 _file = new QFile(kXmlConfigFilename.c_str()); 47 if (NULL != _file) { 48 LOG_INFO("XML document " << kXmlConfigFilename.c_str() << " was created"); 58 m_document.documentElement().appendChild(m_document.createElement(kParameterSection)); 59 m_document.documentElement().appendChild(m_document.createElement(kComponentSection)); 60 m_document.documentElement().appendChild(m_document.createElement(kConnectionSection)); 61 m_file.setFileName(kXmlConfigFilename.c_str()); 62 bool isOpenedSuccessfully = m_file.open(QIODevice::ReadWrite); // FIXME: ReadOnly ? 63 if (isOpenedSuccessfully) { 64 LOG_INFO("XML document " << kXmlConfigFilename << " was created"); 49 65 } else { 50 LOG_WARN("cannot create XML document " << kXmlConfigFilename.c_str()); 66 LOG_ERROR("cannot open XML document " << kXmlConfigFilename); 67 throw "cannot open XML document file"; 51 68 } 52 69 } … … 57 74 } 58 75 76 // CREATE/DESTROY 59 77 XmlConfigFile * XmlConfigFile::create() 60 78 { 61 if (NULL == _xmlConfigFile) {62 _xmlConfigFile = new XmlConfigFile();63 } 64 return _xmlConfigFile;79 if (NULL ==m_xmlConfigFile) { 80 m_xmlConfigFile = new XmlConfigFile(); 81 } 82 return m_xmlConfigFile; 65 83 } 66 84 67 85 void XmlConfigFile::destroy() 68 86 { 69 delete _xmlConfigFile; 70 _xmlConfigFile = NULL; 71 } 72 87 delete m_xmlConfigFile; 88 m_xmlConfigFile = NULL; 89 } 90 91 // COMPONENTS ADD/REMOVE/CREATE 73 92 void XmlConfigFile::addComponent(QDomElement component) 74 93 { 75 _mutex.lock(); 76 // TODO change .tagName => .attribute(kPropertyComponentName.c_str()) 77 if (_document.documentElement().namedItem(componentSection).namedItem(component.attribute(nameAttribute)/*.tagName()*/).isNull()) { 78 LOG_WARN("component " << component.attribute(nameAttribute)/*tagName()*/ << " exists already in the document"); 94 QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor 95 (void) mutexLocker; // unused 96 97 // TODO: change .tagName => .attribute(kPropertyComponentName.c_str()) 98 QDomNode componentSectionNode = getNamedItemFromDomDocument(m_document, kComponentSection); 99 if (componentSectionNode.namedItem(component.attribute(kNameAttribute)/*.tagName()*/).isNull()) { 100 LOG_WARN("component " << component.attribute(kNameAttribute)/*tagName()*/ << " exists already in the document"); 79 101 } else { 80 QDomNode node = _document.documentElement().namedItem(componentSection).appendChild(component);81 ++ _numberOfComponents;102 QDomNode node = getNamedItemFromDomDocument(m_document, kComponentSection).appendChild(component); 103 ++m_numberOfComponents; 82 104 LOG_INFO("component " << node.nodeName() << " has been added to the section " 83 << _document.documentElement().namedItem(componentSection).nodeName()); 84 } 85 _mutex.unlock(); 105 << getNamedItemFromDomDocument(m_document, kComponentSection).nodeName()); 106 } 86 107 } 87 108 88 109 void XmlConfigFile::delComponent(QDomElement component) 89 110 { 90 _mutex.lock(); 91 92 QDomNode node = _document.documentElement().namedItem(componentSection).removeChild(component); 111 removeComponent(component); 112 } 113 114 void XmlConfigFile::removeComponent(QDomElement component) 115 { 116 QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor 117 (void) mutexLocker; // unused 118 119 QDomNode node = getNamedItemFromDomDocument(m_document, kComponentSection).removeChild(component); 93 120 if (node.isNull()) { 94 LOG_WARN("component " << component.attribute( nameAttribute)/*tagName()*/ << " doesn't exist in the document.");121 LOG_WARN("component " << component.attribute(kNameAttribute)/*tagName()*/ << " doesn't exist in the document."); 95 122 } else { 96 123 LOG_INFO("component " << node.nodeName() << " has been removed from the section " 97 << _document.documentElement().namedItem(componentSection).nodeName()); 98 --_numberOfComponents; 99 } 100 101 _mutex.unlock(); 124 << getNamedItemFromDomDocument(m_document, kComponentSection).nodeName()); 125 --m_numberOfComponents; 126 } 102 127 } 103 128 … … 106 131 LOG_DEBUG("creating component " << name); 107 132 108 QMutexLocker mutexLocker(&_mutex); 109 Q_UNUSED(mutexLocker); 110 return _document.createElement(name); 111 } 112 133 QMutexLocker mutexLocker(&m_mutex); // locks mutex, unlocks it in destructor 134 (void) mutexLocker; // unused 135 return m_document.createElement(name); 136 } 137 138 // FILE I/O 113 139 void XmlConfigFile::saveFile(QString fileName) 114 140 { 115 QMutexLocker mutexLocker(&_mutex); 116 Q_UNUSED(mutexLocker); 117 118 _file->setFileName(fileName); 141 QMutexLocker mutexLocker(&m_mutex); 142 (void) mutexLocker; // unused 143 144 m_file.close(); 145 assert(!m_file.isOpen()); 146 m_file.setFileName(fileName); 119 147 { 120 _file->open(QIODevice::WriteOnly); 121 { 122 QTextStream ts(_file); 123 ts << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" + _document.toString(); 124 } 125 _file->close(); 126 } 127 LOG_DEBUG("file \"" << _file->fileName() << "\" has been saved"); 148 // open file 149 bool isOpenedSuccessfully = m_file.open(QIODevice::WriteOnly); 150 if (!isOpenedSuccessfully) { 151 LOG_ERROR("cannot open file '" << m_file.fileName() << "' for writing"); 152 return; 153 } 154 QTextStream ts(&m_file); 155 ts << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; 156 ts << m_document.toString(); 157 m_file.close(); 158 } 159 LOG_DEBUG("file \"" << m_file.fileName() << "\" has been saved"); 128 160 } 129 161 130 162 int XmlConfigFile::loadFile(QString fileName) 131 163 { 132 QMutexLocker mutexLocker(&_mutex); 133 Q_UNUSED(mutexLocker); 134 135 int line = 0, col = 0; 136 QString errorMsg; 137 138 if (_numberOfComponents != 0) { 139 LOG_WARN("XML document contained " << _numberOfComponents << " components that will be lost!"); 140 } 141 142 _file->setFileName(fileName); 143 if (!_file->open( QIODevice::ReadOnly )) { 144 LOG_ERROR("cannot open file " << fileName); 145 return 0; 146 } 147 if (!_document.setContent( _file, true, &errorMsg, &line, &col )) { 148 LOG_ERROR("cannot get data into the file " << fileName); 149 LOG_ERROR(errorMsg << " at position " << line << ":" << col << " (line:col)"); 150 _file->close(); 151 return 0; 152 } 153 _file->close(); 164 // lock access 165 QMutexLocker mutexLocker(&m_mutex); 166 (void) mutexLocker; // unused 167 168 // check if there are components already 169 if (0 != m_numberOfComponents) { 170 LOG_WARN("XML document contained " << m_numberOfComponents << " components that will be lost!"); 171 } 172 173 m_file.close(); 174 assert(!m_file.isOpen()); 175 m_file.setFileName(fileName); 176 { 177 // open file 178 bool isOpenedSuccessfully = m_file.open(QIODevice::ReadOnly); 179 if (!isOpenedSuccessfully) { 180 LOG_ERROR("cannot open file '" << m_file.fileName() << "' for reading"); 181 return 0; 182 } 183 // read and parse input XML file 184 QString errorMsg; 185 int errorLine = 0; 186 int errorColumn = 0; 187 if (!m_document.setContent(&m_file, /*namespaceProcessing=*/true, &errorMsg, &errorLine, &errorColumn)) { 188 LOG_ERROR("cannot parse XML file " << m_file.fileName()); 189 LOG_ERROR(errorMsg << " at " << errorLine << ":" << errorColumn << " (line:col)"); 190 m_file.close(); 191 return 0; 192 } 193 // close file 194 m_file.close(); 195 } 154 196 155 197 // get the number of components in the loaded tree 156 _numberOfComponents = _document.documentElement().namedItem(componentSection).childNodes().count();157 158 LOG_INFO("XML file \"" << fileName << "\" has been loaded. Number of components = " <<_numberOfComponents);198 m_numberOfComponents = getAllComponents().count(); 199 200 LOG_INFO("XML file \"" << m_file.fileName() << "\" has been loaded. Number of components = " << m_numberOfComponents); 159 201 LOG_DEBUG("XML file content:\n" 160 << "BEGIN============================================================================\n" 161 << _document.toString() 162 << "END==============================================================================\n" 163 ); 164 165 return _numberOfComponents; 166 } 167 168 QDomElement XmlConfigFile::getComponent(QString componentName) 202 << "BEGIN============================================================================\n" 203 << m_document.toString() 204 << "END==============================================================================\n" 205 ); 206 207 return m_numberOfComponents; 208 } 209 210 // COMPONENTS 211 QDomNodeList XmlConfigFile::getAllComponents() const 212 { 213 // get components section 214 QDomElement componentsElement = getNamedItemFromDomDocument(m_document, kComponentSection).toElement(); 215 if (componentsElement.isNull()) { 216 LOG_WARN("there is no '" << kComponentSection << "' section in the XML"); 217 return QDomNodeList(); 218 } 219 220 // get component nodes 221 return componentsElement.elementsByTagName(kComponentNode); 222 } 223 224 QStringList XmlConfigFile::getAllComponentsNames() const 225 { 226 // get component nodes 227 QDomNodeList componentNodes = getAllComponents(); 228 // get component names 229 QStringList componentNameList; 230 for (int i = 0; i < componentNodes.size(); ++i) { 231 QDomElement componentElement = componentNodes.at(i).toElement(); 232 componentNameList.append(componentElement.attribute(kNameAttribute)); 233 } 234 return componentNameList; 235 } 236 237 QDomElement XmlConfigFile::getComponent(QString componentName) const 169 238 { 170 239 LOG_DEBUG("getting component " << componentName); 171 240 172 QDomNodeList nodeList = _document.documentElement().namedItem(componentSection).toElement().elementsByTagName(componentNode);173 for (int i = 0; i < _numberOfComponents; i++) {174 175 if (nodeList.at(i).toElement().attribute(nameAttribute) == componentName)176 return nodeList.at(i).toElement();177 }178 179 LOG_WARN("cannot get component " << componentName << ": document does not contain the component");241 QDomNodeList componentNodes = getAllComponents(); 242 for (int i = 0; i < componentNodes.size(); ++i) { 243 QDomElement componentElement = componentNodes.at(i).toElement(); 244 if (componentName == componentElement.attribute(kNameAttribute)) { 245 return componentElement; 246 } 247 } 248 LOG_WARN("cannot get component " << componentName << ": document does not contain a component with this name"); 180 249 181 250 return QDomElement(); 182 251 } 183 252 184 QStringList XmlConfigFile::getAllComponentsNames() 185 { 186 QStringList componentNameList; 187 QDomNodeList nodeList = _document.documentElement().namedItem(componentSection).toElement().elementsByTagName(componentNode); 188 for (int i = 0; i < _numberOfComponents; i++) { 189 componentNameList.append(nodeList.at(i).toElement().attribute(nameAttribute)); 190 } 191 return componentNameList; 192 } 193 194 QDomNodeList XmlConfigFile::getAllComponents() 195 { 196 return _document.documentElement().namedItem(componentSection).toElement().elementsByTagName(componentNode); 197 } 198 199 QStringList XmlConfigFile::getAllPlugins() 200 { 201 QDomNodeList nodeList = _document.documentElement().namedItem(parameterSection).toElement().elementsByTagName(pluginNode); 202 QStringList stringList; 203 if(nodeList.size() == 0) { 204 LOG_WARN("no plugins were specified"); 205 } else 206 for(int i = 0; i< nodeList.size(); ++i) 207 stringList.append(nodeList.at(i).toElement().attribute(libAttribute)); 208 209 return stringList; 210 } 211 212 QDomNodeList XmlConfigFile::getAllConnections() 213 { 214 return _document.documentElement().namedItem(connectionSection).childNodes(); 215 } 253 // CONNECTIONS 254 QDomNodeList XmlConfigFile::getAllConnections() const 255 { 256 // get connections section 257 QDomElement connectionsElement = getNamedItemFromDomDocument(m_document, kParameterSection).toElement(); 258 if (connectionsElement.isNull()) { 259 LOG_WARN("there is no '" << kParameterSection << "' section in the XML"); 260 return QDomNodeList(); 261 } 262 263 // get connection nodes 264 //return connectionsElement.elementsByTagName(kConnectionNode); 265 return getNamedItemFromDomDocument(m_document, kConnectionSection).childNodes(); 266 } 267 268 QDomElement XmlConfigFile::getConnection(QString name) const 269 { 270 LOG_DEBUG("getting connection " << name); 271 272 QDomNodeList connectionNodes = getAllConnections(); 273 for (int i = 0; i < connectionNodes.size(); ++i) { 274 QDomElement connectionElement = connectionNodes.at(i).toElement(); 275 // TODO: name by attribute 'name' 276 if (name == connectionElement.attribute(kNameAttribute)) { 277 return connectionElement; 278 } 279 } 280 LOG_WARN("cannot get connection " << name << ": document does not contain a connection with this name"); 281 282 return QDomElement(); 283 } 284 285 // PLUGINS 286 QDomNodeList XmlConfigFile::getAllPlugins() 287 { 288 // get parameters section 289 QDomElement parametersElement = getNamedItemFromDomDocument(m_document, kParameterSection).toElement(); 290 if (parametersElement.isNull()) { 291 LOG_WARN("there is no '" << kParameterSection << "' section in the XML"); 292 return QDomNodeList(); 293 } 294 295 // get attributes 296 m_libraryExtension = parametersElement.attribute(kExtensionAttribute); 297 if (!m_libraryExtension.isEmpty()) { 298 // prefix with a dot '.' if there is no one 299 if ('.' != m_libraryExtension.at(0)) { 300 m_libraryExtension = '.' + m_libraryExtension; 301 } 302 } 303 m_libraryPrefix = parametersElement.attribute(kPrefixAttribute); 304 m_libraryPostfix = parametersElement.attribute(kPostfixAttribute); 305 306 // get plugin nodes 307 return parametersElement.elementsByTagName(kPluginNode); 308 } 309 310 QStringList XmlConfigFile::getAllPluginsNames() 311 { 312 QDomNodeList pluginList = getAllPlugins(); 313 if (0 == pluginList.size()) { 314 LOG_ERROR("no plugins were specified"); 315 return QStringList(); 316 } 317 318 // get plugin library paths 319 QStringList pluginLibraryNames; 320 for (int i = 0; i < pluginList.size(); ++i) { 321 QDomElement pluginElement = pluginList.at(i).toElement(); 322 QString libraryFileName = libraryPrefix() + pluginElement.attribute(kLibAttribute) + libraryPostfix() + libraryExtension(); 323 pluginLibraryNames.append(libraryFileName); 324 } 325 return pluginLibraryNames; 326 } 327 328 QString XmlConfigFile::libraryExtension() const 329 { 330 return m_libraryExtension; 331 } 332 333 QString XmlConfigFile::libraryPrefix() const 334 { 335 return m_libraryPrefix; 336 } 337 338 QString XmlConfigFile::libraryPostfix() const 339 { 340 return m_libraryPostfix; 341 } 342 343 //////////////////////////////////////////////////////////////////////////////// 344 // HELPER METHODS 345 QDomNode getNamedItemFromDomDocument(const QDomDocument & document, const char * itemName) 346 { 347 return document.documentElement().namedItem(itemName); 348 }
Note:
See TracChangeset
for help on using the changeset viewer.