From 5421b5e9263c8b9c96d7344d1d631f2047e6a038 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sat, 3 Oct 2020 18:45:30 +0200 Subject: [PATCH] Cleanup Session loading (prepare for session history) --- Mixer.cpp | 8 ++--- Session.cpp | 22 +++--------- Session.h | 4 +-- SessionCreator.cpp | 89 +++++++++++++++++++++++++--------------------- SessionCreator.h | 35 +++++++++++------- SessionSource.cpp | 2 +- 6 files changed, 81 insertions(+), 79 deletions(-) diff --git a/Mixer.cpp b/Mixer.cpp index 3e62efc..f746a7e 100644 --- a/Mixer.cpp +++ b/Mixer.cpp @@ -677,10 +677,10 @@ void Mixer::load(const std::string& filename) if (sessionLoaders_.empty()) { // Start async thread for loading the session // Will be obtained in the future in update() - sessionLoaders_.emplace_back( std::async(std::launch::async, loadSession_, filename) ); + sessionLoaders_.emplace_back( std::async(std::launch::async, Session::load, filename) ); } #else - set( loadSession_(filename) ); + set( Session::load(filename) ); #endif } @@ -715,10 +715,10 @@ void Mixer::import(const std::string& filename) if (sessionImporters_.empty()) { // Start async thread for loading the session // Will be obtained in the future in update() - sessionImporters_.emplace_back( std::async(std::launch::async, loadSession_, filename) ); + sessionImporters_.emplace_back( std::async(std::launch::async, Session::load, filename) ); } #else - merge( loadSession_(filename) ); + merge( Session::load(filename) ); #endif } diff --git a/Session.cpp b/Session.cpp index bc71d20..5eb3ff6 100644 --- a/Session.cpp +++ b/Session.cpp @@ -340,25 +340,11 @@ void Session::unlock() } -Session *loadSession_(const std::string& filename) +Session *Session::load(const std::string& filename) { - Session *s = new Session; + SessionCreator creator; + creator.load(filename); - if (s) { - // actual loading of xml file - SessionCreator creator( s ); - - if (creator.load(filename)) { - // loaded ok - s->setFilename(filename); - } - else { - // error loading - delete s; - s = nullptr; - } - } - - return s; + return creator.session(); } diff --git a/Session.h b/Session.h index ccd22e5..4ff81d4 100644 --- a/Session.h +++ b/Session.h @@ -14,6 +14,8 @@ public: Session(); ~Session(); + static Session *load(const std::string& filename); + // add given source into the session SourceList::iterator addSource (Source *s); @@ -92,6 +94,4 @@ protected: }; -Session *loadSession_(const std::string& filename); - #endif // SESSION_H diff --git a/SessionCreator.cpp b/SessionCreator.cpp index 9298bc0..adfa4bc 100644 --- a/SessionCreator.cpp +++ b/SessionCreator.cpp @@ -42,25 +42,21 @@ std::string SessionCreator::info(const std::string& filename) return ret; } -SessionCreator::SessionCreator(Session *session): Visitor(), session_(session) +SessionCreator::SessionCreator(): SessionLoader(nullptr) { } -SessionCreator::~SessionCreator() -{ -} - -bool SessionCreator::load(const std::string& filename) +void SessionCreator::load(const std::string& filename) { XMLError eResult = xmlDoc_.LoadFile(filename.c_str()); if ( XMLResultError(eResult)) - return false; + return; XMLElement *header = xmlDoc_.FirstChildElement(APP_NAME); if (header == nullptr) { Log::Warning("%s is not a %s session file.", filename.c_str(), APP_NAME); - return false; + return; } int version_major = -1, version_minor = -1; @@ -68,25 +64,41 @@ bool SessionCreator::load(const std::string& filename) header->QueryIntAttribute("minor", &version_minor); if (version_major != XML_VERSION_MAJOR || version_minor != XML_VERSION_MINOR){ Log::Warning("%s is in a different versions of session file. Loading might fail.", filename.c_str()); - return false; + return; } + session_ = new Session; + // ok, ready to read sources - loadSession( xmlDoc_.FirstChildElement("Session") ); - // excellent, session was created: load optionnal config - if (session_){ - loadConfig( xmlDoc_.FirstChildElement("Views") ); - } + SessionLoader::load( xmlDoc_.FirstChildElement("Session") ); - return true; + // load optionnal config + loadConfig( xmlDoc_.FirstChildElement("Views") ); + + session_->setFilename(filename); } -void SessionCreator::loadSession(XMLElement *sessionNode) + +void SessionCreator::loadConfig(XMLElement *viewsNode) { - if (sessionNode != nullptr) { - // create a session if not provided - if (!session_) - session_ = new Session; + if (viewsNode != nullptr) { + // ok, ready to read views + SessionLoader::XMLToNode( viewsNode->FirstChildElement("Mixing"), *session_->config(View::MIXING)); + SessionLoader::XMLToNode( viewsNode->FirstChildElement("Geometry"), *session_->config(View::GEOMETRY)); + SessionLoader::XMLToNode( viewsNode->FirstChildElement("Layer"), *session_->config(View::LAYER)); + SessionLoader::XMLToNode( viewsNode->FirstChildElement("Rendering"), *session_->config(View::RENDERING)); + } +} + +SessionLoader::SessionLoader(Session *session): Visitor(), session_(session) +{ + +} + + +void SessionLoader::load(XMLElement *sessionNode) +{ + if (sessionNode != nullptr && session_ != nullptr) { int counter = 0; XMLElement* sourceNode = sessionNode->FirstChildElement("Source"); @@ -124,6 +136,11 @@ void SessionCreator::loadSession(XMLElement *sessionNode) else if ( std::string(pType) == "DeviceSource") { load_source = new DeviceSource; } + + // avoid non recognized types + if (!load_source) + continue; + // add source to session session_->addSource(load_source); } @@ -164,18 +181,8 @@ void SessionCreator::loadSession(XMLElement *sessionNode) Log::Warning("Session seems empty."); } -void SessionCreator::loadConfig(XMLElement *viewsNode) -{ - if (viewsNode != nullptr) { - // ok, ready to read views - SessionCreator::XMLToNode( viewsNode->FirstChildElement("Mixing"), *session_->config(View::MIXING)); - SessionCreator::XMLToNode( viewsNode->FirstChildElement("Geometry"), *session_->config(View::GEOMETRY)); - SessionCreator::XMLToNode( viewsNode->FirstChildElement("Layer"), *session_->config(View::LAYER)); - SessionCreator::XMLToNode( viewsNode->FirstChildElement("Rendering"), *session_->config(View::RENDERING)); - } -} -void SessionCreator::XMLToNode(tinyxml2::XMLElement *xml, Node &n) +void SessionLoader::XMLToNode(tinyxml2::XMLElement *xml, Node &n) { if (xml != nullptr){ XMLElement *node = xml->FirstChildElement("Node"); @@ -194,12 +201,12 @@ void SessionCreator::XMLToNode(tinyxml2::XMLElement *xml, Node &n) } } -void SessionCreator::visit(Node &n) +void SessionLoader::visit(Node &n) { XMLToNode(xmlCurrent_, n); } -void SessionCreator::visit(MediaPlayer &n) +void SessionLoader::visit(MediaPlayer &n) { XMLElement* mediaplayerNode = xmlCurrent_->FirstChildElement("MediaPlayer"); if (mediaplayerNode) { @@ -239,7 +246,7 @@ void SessionCreator::visit(MediaPlayer &n) } } -void SessionCreator::visit(Shader &n) +void SessionLoader::visit(Shader &n) { XMLElement* color = xmlCurrent_->FirstChildElement("color"); if ( color ) { @@ -253,7 +260,7 @@ void SessionCreator::visit(Shader &n) } } -void SessionCreator::visit(ImageShader &n) +void SessionLoader::visit(ImageShader &n) { const char *pType = xmlCurrent_->Attribute("type"); if ( std::string(pType) != "ImageShader" ) @@ -266,7 +273,7 @@ void SessionCreator::visit(ImageShader &n) } } -void SessionCreator::visit(ImageProcessingShader &n) +void SessionLoader::visit(ImageProcessingShader &n) { const char *pType = xmlCurrent_->Attribute("type"); if ( std::string(pType) != "ImageProcessingShader" ) @@ -297,7 +304,7 @@ void SessionCreator::visit(ImageProcessingShader &n) tinyxml2::XMLElementToGLM( chromakey->FirstChildElement("vec4"), n.chromakey); } -void SessionCreator::visit (Source& s) +void SessionLoader::visit (Source& s) { XMLElement* sourceNode = xmlCurrent_; const char *pName = sourceNode->Attribute("name"); @@ -324,7 +331,7 @@ void SessionCreator::visit (Source& s) xmlCurrent_ = sourceNode; } -void SessionCreator::visit (MediaSource& s) +void SessionLoader::visit (MediaSource& s) { // set uri XMLElement* uriNode = xmlCurrent_->FirstChildElement("uri"); @@ -337,7 +344,7 @@ void SessionCreator::visit (MediaSource& s) s.mediaplayer()->accept(*this); } -void SessionCreator::visit (SessionSource& s) +void SessionLoader::visit (SessionSource& s) { // set uri XMLElement* pathNode = xmlCurrent_->FirstChildElement("path"); @@ -348,7 +355,7 @@ void SessionCreator::visit (SessionSource& s) } -void SessionCreator::visit (PatternSource& s) +void SessionLoader::visit (PatternSource& s) { uint p = xmlCurrent_->UnsignedAttribute("pattern"); @@ -360,7 +367,7 @@ void SessionCreator::visit (PatternSource& s) s.setPattern(p, resolution); } -void SessionCreator::visit (DeviceSource& s) +void SessionLoader::visit (DeviceSource& s) { const char *devname = xmlCurrent_->Attribute("device"); s.setDevice(devname); diff --git a/SessionCreator.h b/SessionCreator.h index 625ce5b..dbfc1d9 100644 --- a/SessionCreator.h +++ b/SessionCreator.h @@ -6,23 +6,15 @@ class Session; -class SessionCreator : public Visitor { - - tinyxml2::XMLDocument xmlDoc_; - tinyxml2::XMLElement *xmlCurrent_; - Session *session_; - - void loadSession(tinyxml2::XMLElement *sessionNode); - void loadConfig(tinyxml2::XMLElement *viewsNode); +class SessionLoader : public Visitor { public: - SessionCreator(Session *session = nullptr); - ~SessionCreator(); - - bool load(const std::string& filename); + SessionLoader(Session *session); inline Session *session() const { return session_; } + void load(tinyxml2::XMLElement *sessionNode); + // Elements of Scene void visit(Node& n) override; @@ -51,9 +43,26 @@ public: void visit (PatternSource& s) override; void visit (DeviceSource& s) override; - static std::string info(const std::string& filename); static void XMLToNode(tinyxml2::XMLElement *xml, Node &n); + +protected: + tinyxml2::XMLElement *xmlCurrent_; + Session *session_; + }; +class SessionCreator : public SessionLoader { + + tinyxml2::XMLDocument xmlDoc_; + + void loadConfig(tinyxml2::XMLElement *viewsNode); + +public: + SessionCreator(); + + void load(const std::string& filename); + + static std::string info(const std::string& filename); +}; #endif // SESSIONCREATOR_H diff --git a/SessionSource.cpp b/SessionSource.cpp index 9016755..0f2502a 100644 --- a/SessionSource.cpp +++ b/SessionSource.cpp @@ -77,7 +77,7 @@ void SessionSource::load(const std::string &p) session_ = new Session; else // launch a thread to load the session file - sessionLoader_ = std::async(std::launch::async, loadSession_, path_); + sessionLoader_ = std::async(std::launch::async, Session::load, path_); Log::Notify("Opening %s", p.c_str()); }