2010-08-02 08:31:38 +02:00
|
|
|
// Copyright (c) 2010, Thomas Goyne <plorkyeran@aegisub.org>
|
2008-03-07 03:32:29 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2008-03-07 03:32:29 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file factory_manager.h
|
|
|
|
/// @brief Template/base-class for factory classes
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2008-03-07 03:32:29 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-09-11 04:36:34 +02:00
|
|
|
#ifndef AGI_PRE
|
2010-08-03 00:14:11 +02:00
|
|
|
#include <algorithm>
|
2010-08-02 08:31:38 +02:00
|
|
|
#include <cctype>
|
2008-03-07 03:32:29 +01:00
|
|
|
#include <map>
|
2010-08-02 08:31:38 +02:00
|
|
|
#include <vector>
|
2009-09-11 04:36:34 +02:00
|
|
|
|
|
|
|
#include <wx/string.h>
|
|
|
|
#endif
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template <class func>
|
|
|
|
class FactoryBase {
|
2008-03-07 03:32:29 +01:00
|
|
|
protected:
|
2010-08-02 08:31:38 +02:00
|
|
|
typedef std::map<std::string, std::pair<bool, func> > map;
|
|
|
|
typedef typename map::iterator iterator;
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
static map *classes;
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
static void DoRegister(func function, std::string name, bool hide, std::vector<std::string> &subtypes) {
|
|
|
|
if (!classes) classes = new map;
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
if (subtypes.empty()) {
|
|
|
|
classes->insert(std::make_pair(name, std::make_pair(hide, function)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < subtypes.size(); i++) {
|
|
|
|
classes->insert(std::make_pair(name + '/' + subtypes[i], std::make_pair(hide, function)));
|
2009-05-18 07:42:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
static func Find(std::string name) {
|
|
|
|
if (!classes) return NULL;
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
iterator factory = classes->find(name);
|
|
|
|
if (factory != classes->end()) return factory->second.second;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
public:
|
|
|
|
static void Clear() {
|
|
|
|
delete classes;
|
|
|
|
}
|
|
|
|
static std::vector<std::string> GetClasses(std::string favourite="") {
|
|
|
|
std::vector<std::string> list;
|
|
|
|
if (!classes) return list;
|
|
|
|
std::string cmp;
|
|
|
|
std::transform(favourite.begin(), favourite.end(), favourite.begin(), ::tolower);
|
|
|
|
for (iterator cur=classes->begin();cur!=classes->end();cur++) {
|
|
|
|
cmp.clear();
|
|
|
|
std::transform(cur->first.begin(), cur->first.end(), std::back_inserter(cmp), ::tolower);
|
|
|
|
if (cmp == favourite) list.insert(list.begin(), cur->first);
|
|
|
|
else if (!cur->second.first) list.push_back(cur->first);
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
return list;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
virtual ~FactoryBase() {
|
|
|
|
delete classes;
|
|
|
|
}
|
|
|
|
};
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class Base>
|
|
|
|
class Factory0 : public FactoryBase<Base *(*)()> {
|
|
|
|
typedef Base *(*func)();
|
|
|
|
template<class T>
|
|
|
|
static Base* create() {
|
|
|
|
return new T;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
static Base* Create(std::string name) {
|
|
|
|
func factory = FactoryBase<func>::Find(name);
|
|
|
|
if (factory) {
|
|
|
|
return factory();
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
else {
|
|
|
|
return NULL;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class T>
|
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = std::vector<std::string>()) {
|
2011-11-07 07:18:34 +01:00
|
|
|
FactoryBase<func>::DoRegister(&Factory0<Base>::template create<T>, name, hide, subTypes);
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
};
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class Base, class Arg1>
|
|
|
|
class Factory1 : public FactoryBase<Base *(*)(Arg1)> {
|
|
|
|
typedef Base *(*func)(Arg1);
|
|
|
|
template<class T>
|
|
|
|
static Base* create(Arg1 a1) {
|
|
|
|
return new T(a1);
|
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
public:
|
2010-08-02 08:31:38 +02:00
|
|
|
static Base* Create(std::string name, Arg1 a1) {
|
|
|
|
func factory = FactoryBase<func>::Find(name);
|
|
|
|
if (factory) {
|
|
|
|
return factory(a1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class T>
|
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = std::vector<std::string>()) {
|
2011-11-07 07:18:34 +01:00
|
|
|
FactoryBase<func>::DoRegister(&Factory1<Base, Arg1>::template create<T>, name, hide, subTypes);
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
|
|
|
};
|