summaryrefslogtreecommitdiff
path: root/decoder/ff_factory.cc
blob: 88991fbf5b2a7b8d6f04221348c998c4c5df64fc (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
#include "ff_factory.h"

#include "ff.h"
#include "stringlib.h"

using boost::shared_ptr;
using namespace std;

FFFactoryBase::~FFFactoryBase() {}

void FFRegistry::DisplayList() const {
  for (map<string, shared_ptr<FFFactoryBase> >::const_iterator it = reg_.begin();
       it != reg_.end(); ++it) {
    cerr << "  " << it->first << endl;
  }
}

string FFRegistry::usage(string const& ffname,bool params,bool verbose) const {
  map<string, shared_ptr<FFFactoryBase> >::const_iterator it = reg_.find(ffname);
  return it == reg_.end()
    ? "Unknown feature " + ffname
    : it->second->usage(params,verbose);
}

namespace {
std::string const& debug_pre="debug";
}

shared_ptr<FeatureFunction> FFRegistry::Create(const string& ffname, const string& param) const {
  map<string, shared_ptr<FFFactoryBase> >::const_iterator it = reg_.find(ffname);
  shared_ptr<FeatureFunction> res;
  if (it == reg_.end()) {
    cerr << "I don't know how to create feature " << ffname << endl;
  } else {
    int pl=debug_pre.size();
    bool space=false;
    std::string p=param;
    bool debug=match_begin(p,debug_pre)&&
      (p.size()==pl || (space=(p[pl]==' ')));
    if (debug) {
      p.erase(0,debug_pre.size()+space);
      cerr<<"debug enabled for "<<ffname<< " - remaining options: '"<<p<<"'\n";
    }
    res = it->second->Create(p);
    res->name=ffname;
    res->debug_=debug;
  }
  return res;
}

void FFRegistry::Register(const string& ffname, FFFactoryBase* factory) {
  if (reg_.find(ffname) != reg_.end()) {
    cerr << "Duplicate registration of FeatureFunction with name " << ffname << "!\n";
    abort();
  }
  reg_[ffname].reset(factory);
}


void FFRegistry::Register(FFFactoryBase* factory)
{
  Register(factory->usage(false,false),factory);
}