summaryrefslogtreecommitdiff
path: root/decoder/ff_factory.h
blob: 102e709c3f5c6ee724816c2dc083dd3ff897b8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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
 * all be mashed into a single ffunc, which can just emit all the fid scores at
 * once.  that's fine.
 */


#include <iostream>
#include <string>
#include <map>
#include <stdexcept>

#include <boost/shared_ptr.hpp>

class FeatureFunction;

class FsaFeatureFunction;


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>
struct FFFactory : public FactoryBase<FeatureFunction> {
  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);
  }
};


// 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;
  bool have(std::string const& ffname);
  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; }

void ff_usage(std::string const& name,std::ostream &out=std::cout);

/*
extern boost::shared_ptr<FsaFFRegistry> global_fsa_ff_registry;
extern boost::shared_ptr<FFRegistry> global_ff_registry;
*/
#endif