summaryrefslogtreecommitdiff
path: root/decoder/ff_factory.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-07 00:07:45 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-07 00:07:45 +0000
commit9db822ad9f2e25d88520e1c61b20052e7dfca6ca (patch)
treee5375c12fc748af98f871cdb453ba48e95379a55 /decoder/ff_factory.h
parentf87b2add497d9033ef468233ea3ec0f33cc97944 (diff)
dynamic fsa ff, factory for fsa and ff shares code, factory moved to ff_factory.cc
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@483 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/ff_factory.h')
-rw-r--r--decoder/ff_factory.h105
1 files changed, 81 insertions, 24 deletions
diff --git a/decoder/ff_factory.h b/decoder/ff_factory.h
index 93681c5e..e5821d44 100644
--- a/decoder/ff_factory.h
+++ b/decoder/ff_factory.h
@@ -1,6 +1,10 @@
#ifndef _FF_FACTORY_H_
#define _FF_FACTORY_H_
+// FsaF* vs F* (regular ff/factory).
+
+//TODO: use http://www.boost.org/doc/libs/1_43_0/libs/functional/factory/doc/html/index.html ?
+
/*TODO: register state identity separately from feature function identity? as
* in: string registry for name of state somewhere, assert that same result is
* computed by all users? or, we can just require that ff sharing same state
@@ -8,49 +12,102 @@
* once. that's fine.
*/
-//TODO: use http://www.boost.org/doc/libs/1_43_0/libs/functional/factory/doc/html/index.html ?
#include <iostream>
#include <string>
#include <map>
+#include <stdexcept>
#include <boost/shared_ptr.hpp>
class FeatureFunction;
-class FFRegistry;
-class FFFactoryBase;
-extern boost::shared_ptr<FFRegistry> global_ff_registry;
-class FFRegistry {
- friend int main(int argc, char** argv);
- friend class FFFactoryBase;
- public:
- boost::shared_ptr<FeatureFunction> Create(const std::string& ffname, const std::string& param) const;
- std::string usage(std::string const& ffname,bool params=true,bool verbose=true) const;
- void DisplayList() const;
- void Register(const std::string& ffname, FFFactoryBase* factory);
- void Register(FFFactoryBase* factory);
- FFRegistry() {}
- private:
- std::map<std::string, boost::shared_ptr<FFFactoryBase> > reg_;
-};
+class FsaFeatureFunction;
-struct FFFactoryBase {
- virtual ~FFFactoryBase();
- virtual boost::shared_ptr<FeatureFunction> Create(const std::string& param) const = 0;
+
+struct UntypedFactory {
+ virtual ~UntypedFactory();
virtual std::string usage(bool params,bool verbose) const = 0;
};
+template <class FF>
+struct FactoryBase : public UntypedFactory {
+ typedef FF F;
+ typedef boost::shared_ptr<F> FP;
+
+ virtual FP Create(std::string param) const = 0;
+};
+
+/* see cdec_ff.cc for example usage: this create concrete factories to be registered */
template<class FF>
-class FFFactory : public FFFactoryBase {
- boost::shared_ptr<FeatureFunction> Create(const std::string& param) const {
- return boost::shared_ptr<FeatureFunction>(new FF(param));
+struct FFFactory : public FactoryBase<FeatureFunction> {
+ FP Create(std::string param) const {
+ return FP(new FF(param));
}
- // called with false,false just gives feature name
virtual std::string usage(bool params,bool verbose) const {
return FF::usage(params,verbose);
}
+};
+
+// same as above, but we didn't want to require a typedef e.g. Parent in FF class, and template typedef isn't available
+template<class FF>
+struct FsaFactory : public FactoryBase<FsaFeatureFunction> {
+ FP Create(std::string param) const {
+ return FP(new FF(param));
+ }
+ virtual std::string usage(bool params,bool verbose) const {
+ return FF::usage(params,verbose);
+ }
+};
+
+struct UntypedFactoryRegistry {
+ std::string usage(std::string const& ffname,bool params=true,bool verbose=true) const;
+ void DisplayList() const;
+ void Register(const std::string& ffname, UntypedFactory* factory);
+ void Register(UntypedFactory* factory);
+ void clear();
+ static bool parse_debug(std::string & param_in_out); // returns true iff param starts w/ debug (and remove that prefix from param)
+ protected:
+ typedef boost::shared_ptr<UntypedFactory> FactoryP;
+ typedef std::map<std::string, FactoryP > Factmap;
+ Factmap reg_;
+ friend int main(int argc, char** argv);
+ friend class UntypedFactory;
};
+
+
+template <class Feat>
+struct FactoryRegistry : public UntypedFactoryRegistry {
+ typedef Feat F;
+ typedef boost::shared_ptr<F> FP;
+ typedef FactoryBase<F> FB;
+
+ FP Create(const std::string& ffname, std::string param) const {
+ using namespace std;
+ Factmap::const_iterator it = reg_.find(ffname);
+ if (it == reg_.end())
+ throw std::runtime_error("I don't know how to create feature "+ffname);
+ bool debug=parse_debug(param);
+ if (debug)
+ cerr<<"debug enabled for "<<ffname<< " - remaining options: '"<<param<<"'\n";
+ FP res = dynamic_cast<FB const&>(*it->second).Create(param);
+ res->name_ = ffname;
+ res->debug_ = debug;
+ return res;
+ }
+};
+
+typedef FactoryRegistry<FeatureFunction> FFRegistry;
+typedef FactoryRegistry<FsaFeatureFunction> FsaFFRegistry;
+
+extern FsaFFRegistry fsa_ff_registry;
+inline FsaFFRegistry & global_fsa_ff_registry() { return fsa_ff_registry; }
+extern FFRegistry ff_registry;
+inline FFRegistry & global_ff_registry() { return ff_registry; }
+/*
+extern boost::shared_ptr<FsaFFRegistry> global_fsa_ff_registry;
+extern boost::shared_ptr<FFRegistry> global_ff_registry;
+*/
#endif