Clean up and document ScriptFactory, and add a function to get the full wildcard string for all registered automation engines

Originally committed to SVN as r5624.
This commit is contained in:
Thomas Goyne 2011-09-28 19:46:05 +00:00
parent 7680d6c246
commit 53c0b6ff3a
6 changed files with 97 additions and 168 deletions

View file

@ -994,123 +994,63 @@ namespace Automation4 {
} }
// ScriptFactory // ScriptFactory
/// DOCME
std::vector<ScriptFactory*> *ScriptFactory::factories = 0; std::vector<ScriptFactory*> *ScriptFactory::factories = 0;
ScriptFactory::ScriptFactory(wxString engine_name, wxString filename_pattern)
/// @brief DOCME : engine_name(engine_name)
/// @return , filename_pattern(filename_pattern)
///
const wxString& ScriptFactory::GetEngineName() const
{ {
return engine_name;
} }
/// @brief DOCME
/// @return
///
const wxString& ScriptFactory::GetFilenamePattern() const
{
return filename_pattern;
}
/// @brief DOCME
/// @param factory
///
void ScriptFactory::Register(ScriptFactory *factory) void ScriptFactory::Register(ScriptFactory *factory)
{ {
if (!factories) GetFactories();
factories = new std::vector<ScriptFactory*>();
if (find(factories->begin(), factories->end(), factory) != factories->end())
throw "Automation 4: Attempt to register the same script factory multiple times. This should never happen.";
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) {
if (*i == factory) {
throw "Automation 4: Attempt to register the same script factory multiple times. This should never happen.";
}
}
factories->push_back(factory); factories->push_back(factory);
} }
/// @brief DOCME
/// @param factory
/// @return
///
void ScriptFactory::Unregister(ScriptFactory *factory) void ScriptFactory::Unregister(ScriptFactory *factory)
{ {
if (!factories) if (!factories) return;
factories = new std::vector<ScriptFactory*>();
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) { std::vector<ScriptFactory*>::iterator i = find(factories->begin(), factories->end(), factory);
if (*i == factory) { if (i != factories->end()) {
factories->erase(i); delete *i;
if (factories->empty()) delete factories; factories->erase(i);
return;
}
} }
} }
Script* ScriptFactory::CreateFromFile(wxString const& filename, bool log_errors)
/// @brief DOCME
/// @param filename
/// @param log_errors
/// @return
///
Script* ScriptFactory::CreateFromFile(const wxString &filename, bool log_errors)
{ {
if (!factories) GetFactories();
factories = new std::vector<ScriptFactory*>();
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) { for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) {
try { Script *s = (*i)->Produce(filename);
Script *s = (*i)->Produce(filename); if (s) {
if (s) { if (!s->GetLoadedState() && log_errors)
if (!s->GetLoadedState() && log_errors) { wxLogError(_("An Automation script failed to load. File name: '%s', error reported: %s"), filename, s->GetDescription());
wxLogError(_("An Automation script failed to load. File name: '%s', error reported:"), filename); return s;
wxLogError(s->GetDescription());
}
return s;
}
}
catch (Script *e) {
// This was the wrong script factory, but it throwing a Script object means it did know what to do about the file
// Use this script object
return e;
} }
} }
if (log_errors) {
wxLogWarning(_("The file was not recognised as an Automation script: %s"), filename); if (log_errors)
} wxLogError(_("The file was not recognised as an Automation script: %s"), filename);
return new UnknownScript(filename); return new UnknownScript(filename);
} }
bool ScriptFactory::CanHandleScriptFormat(wxString const& filename)
/// @brief DOCME
/// @param filename
/// @return
///
bool ScriptFactory::CanHandleScriptFormat(const wxString &filename)
{ {
using std::tr1::placeholders::_1;
// Just make this always return true to bitch about unknown script formats in autoload // Just make this always return true to bitch about unknown script formats in autoload
GetFactories();
if (!factories) return find_if(factories->begin(), factories->end(),
factories = new std::vector<ScriptFactory*>(); bind(&wxString::Matches, filename, bind(&ScriptFactory::GetFilenamePattern, _1))) != factories->end();
for (std::vector<ScriptFactory*>::iterator i = factories->begin(); i != factories->end(); ++i) {
wxString pattern = (*i)->GetFilenamePattern();
if (filename.Matches(pattern)) return true;
}
return false;
} }
/// @brief DOCME
/// @return
///
const std::vector<ScriptFactory*>& ScriptFactory::GetFactories() const std::vector<ScriptFactory*>& ScriptFactory::GetFactories()
{ {
if (!factories) if (!factories)
@ -1119,6 +1059,33 @@ namespace Automation4 {
return *factories; return *factories;
} }
wxString ScriptFactory::GetWildcardStr()
{
GetFactories();
wxString fnfilter, catchall;
for (size_t i = 0; i < factories->size(); ++i) {
const ScriptFactory *fact = (*factories)[i];
if (fact->GetEngineName().empty() || fact->GetFilenamePattern().empty())
continue;
fnfilter = wxString::Format("%s%s scripts (%s)|%s|", fnfilter, fact->GetEngineName(), fact->GetFilenamePattern(), fact->GetFilenamePattern());
catchall += fact->GetFilenamePattern() + ";";
}
#ifdef __WINDOWS__
fnfilter += "All files|*.*";
#else
fnfilter += "All files|*";
#endif
if (!catchall.empty())
catchall.RemoveLast();
if (factories->size() > 1)
fnfilter = "All supported scripts|" + catchall + "|" + fnfilter;
return fnfilter;
}
// UnknownScript // UnknownScript

View file

@ -511,42 +511,48 @@ namespace Automation4 {
void Reload(); void Reload();
}; };
/// DOCME
/// @class ScriptFactory
/// @brief DOCME
///
/// DOCME
class ScriptFactory { class ScriptFactory {
private: /// Vector of loaded script engines
/// DOCME
static std::vector<ScriptFactory*> *factories; static std::vector<ScriptFactory*> *factories;
wxString engine_name;
wxString filename_pattern;
/// Load a file, or return NULL if the file is not in a supported
/// format. If the file is in a supported format but is invalid, a
/// script should be returned which returns false from IsLoaded and
/// an appropriate error message from GetDescription.
///
/// This is private as it should only ever be called through
/// CreateFromFile
virtual Script* Produce(wxString const& filename) const = 0;
protected: protected:
ScriptFactory(wxString engine_name, wxString filename_pattern);
/// @brief DOCME
///
ScriptFactory() { }
/// @brief DOCME
///
virtual ~ScriptFactory() { } virtual ~ScriptFactory() { }
/// DOCME
wxString engine_name;
/// DOCME
wxString filename_pattern;
public: public:
virtual Script* Produce(const wxString &filename) const = 0; /// Name of this automation engine
const wxString& GetEngineName() const; const wxString& GetEngineName() const { return engine_name; }
const wxString& GetFilenamePattern() const; /// Extension which this engine supports
const wxString& GetFilenamePattern() const { return filename_pattern; }
/// Register an automation engine. Calling code retains ownership of pointer
static void Register(ScriptFactory *factory); static void Register(ScriptFactory *factory);
/// Unregister and delete an automation engine
static void Unregister(ScriptFactory *factory); static void Unregister(ScriptFactory *factory);
static Script* CreateFromFile(const wxString &filename, bool log_errors); /// Is there an automation engine registered which can open the file?
static bool CanHandleScriptFormat(const wxString &filename); static bool CanHandleScriptFormat(wxString const& filename);
/// Get the full wildcard string for all loaded engines
static wxString GetWildcardStr();
/// Load a script from a file
/// @param filename Script to load
/// @param log_errors Should load errors be displayed?
/// @return Always returns a valid Script, even if no engine could load the file
static Script* CreateFromFile(wxString const& filename, bool log_errors);
static const std::vector<ScriptFactory*>& GetFactories(); static const std::vector<ScriptFactory*>& GetFactories();
}; };

View file

@ -957,28 +957,12 @@ namespace Automation4 {
} }
} }
/// @brief // Factory methods LuaScriptFactory::LuaScriptFactory()
/// : ScriptFactory("Lua", "*.lua")
LuaScriptFactory::LuaScriptFactory() {}
/// @brief DOCME
///
LuaScriptFactory::~LuaScriptFactory() {}
/// @brief DOCME
///
void LuaScriptFactory::RegisterFactory ()
{ {
engine_name = "Lua";
filename_pattern = "*.lua";
Register(this); Register(this);
} }
/// @brief DOCME
/// @param filename
///
Script* LuaScriptFactory::Produce(const wxString &filename) const Script* LuaScriptFactory::Produce(const wxString &filename) const
{ {
// Just check if file extension is .lua // Just check if file extension is .lua
@ -989,7 +973,6 @@ namespace Automation4 {
return 0; return 0;
} }
} }
}; };
#endif // WITH_AUTO4_LUA #endif // WITH_AUTO4_LUA

View file

@ -34,27 +34,17 @@
/// @ingroup scripting /// @ingroup scripting
/// ///
#include "auto4_base.h" #include "auto4_base.h"
/// DOCME
namespace Automation4 { namespace Automation4 {
/// DOCME /// DOCME
/// @class LuaScriptFactory /// @class LuaScriptFactory
/// @brief DOCME /// @brief DOCME
/// ///
/// DOCME /// DOCME
class LuaScriptFactory : public ScriptFactory { class LuaScriptFactory : public ScriptFactory {
Script* Produce(const wxString &filename) const;
public: public:
LuaScriptFactory(); LuaScriptFactory();
~LuaScriptFactory();
void RegisterFactory ();
Script* Produce(const wxString &filename) const;
}; };
}; };

View file

@ -43,9 +43,10 @@
#include "plugin_manager.h" #include "plugin_manager.h"
#include "video_provider_manager.h" #include "video_provider_manager.h"
#ifdef WITH_AUTO4_LUA
#include "auto4_lua_factory.h"
#endif
/// @brief Constructor
///
PluginManager::PluginManager() { PluginManager::PluginManager() {
init = false; init = false;
@ -55,9 +56,6 @@ PluginManager::PluginManager() {
} }
/// @brief Destructor
///
PluginManager::~PluginManager() { PluginManager::~PluginManager() {
VideoProviderFactory::Clear(); VideoProviderFactory::Clear();
AudioProviderFactory::Clear(); AudioProviderFactory::Clear();
@ -66,18 +64,11 @@ PluginManager::~PluginManager() {
SpellCheckerFactory::Clear(); SpellCheckerFactory::Clear();
#ifdef WITH_AUTO4_LUA #ifdef WITH_AUTO4_LUA
if (lua) { Automation4::ScriptFactory::Unregister(lua);
lua->Unregister(lua);
delete lua;
lua = NULL;
}
#endif #endif
} }
/// @brief Registers all built-in plugins /// @brief Registers all built-in plugins
///
void PluginManager::RegisterBuiltInPlugins() { void PluginManager::RegisterBuiltInPlugins() {
if (!init) { if (!init) {
// Managers // Managers
@ -90,7 +81,6 @@ void PluginManager::RegisterBuiltInPlugins() {
// Automation languages // Automation languages
#ifdef WITH_AUTO4_LUA #ifdef WITH_AUTO4_LUA
lua = new Automation4::LuaScriptFactory(); lua = new Automation4::LuaScriptFactory();
lua->RegisterFactory();
#endif #endif
} }

View file

@ -34,12 +34,7 @@
/// @ingroup main /// @ingroup main
/// ///
namespace Automation4 { class ScriptFactory; }
#ifdef WITH_AUTO4_LUA
#include "auto4_lua_factory.h"
#endif
/// DOCME /// DOCME
/// @class PluginManager /// @class PluginManager
@ -53,9 +48,7 @@ private:
bool init; bool init;
#ifdef WITH_AUTO4_LUA #ifdef WITH_AUTO4_LUA
Automation4::ScriptFactory *lua;
/// DOCME
Automation4::LuaScriptFactory *lua;
#endif #endif
public: public: