Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | List of all members
reranker::Factory< T > Class Template Reference

Factory for dynamically created instance of the specified type. More...

#include <factory.H>

Inheritance diagram for reranker::Factory< T >:
reranker::FactoryBase

Public Member Functions

 Factory ()
 Constructs a new factory. More...
 
virtual void Clear ()
 Clears this factory of all (possibly static) data. More...
 
shared_ptr< T > CreateOrDie (StreamTokenizer &st, Environment *env=NULL)
 Dynamically creates an object, whose type and initialization are contained in a specification string, the tokens of which are given by the specified StreamTokenizer. More...
 
shared_ptr< T > CreateOrDie (const string &spec, const string err_msg, Environment *env=NULL)
 
virtual const string BaseName () const
 Returns the name of the base type of objects constructed by this factory. More...
 
virtual void CollectRegistered (unordered_set< string > &registered) const
 Collects the names of types registered with this factory. More...
 
virtual VarMapBaseCreateVarMap (Environment *env) const
 
virtual VarMapBaseCreateVectorVarMap (Environment *env) const
 
- Public Member Functions inherited from reranker::FactoryBase
virtual ~FactoryBase ()
 

Static Public Member Functions

static bool IsRegistered (const string &type)
 Returns whether the specified type has been registered with this factory (where registration happens typically via the REGISTER_NAMED macro). More...
 
static const Constructor< T > * Register (const string &type, const Constructor< T > *p)
 The method used by the REGISTER_NAMED macro to ensure that subclasses add themselves to the factory. More...
 
static void ClearStatic ()
 Clears all static data associated with this class. More...
 

Detailed Description

template<typename T>
class reranker::Factory< T >

Factory for dynamically created instance of the specified type.

Template Parameters
Tthe type of objects created by this factory, required to have the two methods defined in the FactoryConstructible class

Definition at line 396 of file factory.H.

Constructor & Destructor Documentation

template<typename T>
reranker::Factory< T >::Factory ( )
inline

Constructs a new factory.

Definition at line 399 of file factory.H.

Member Function Documentation

template<typename T>
virtual const string reranker::Factory< T >::BaseName ( ) const
inlinevirtual

Returns the name of the base type of objects constructed by this factory.

Implements reranker::FactoryBase.

Definition at line 716 of file factory.H.

template<typename T>
virtual void reranker::Factory< T >::Clear ( )
inlinevirtual

Clears this factory of all (possibly static) data.

Note that invoking this method will prevent the factory from functioning! It should only be invoked when the factory is no longer needed by the current process.

Implements reranker::FactoryBase.

Definition at line 406 of file factory.H.

template<typename T>
static void reranker::Factory< T >::ClearStatic ( )
inlinestatic

Clears all static data associated with this class.

Note that invoking this method will prevent the factory from functioning! It should only be invoked when the factory is no longer needed by the current process.

Definition at line 773 of file factory.H.

template<typename T>
virtual void reranker::Factory< T >::CollectRegistered ( unordered_set< string > &  registered) const
inlinevirtual

Collects the names of types registered with this factory.

Parameters
[out]registeredregistered a set to be modified by this method so that it contains the names of concrete types registered with this factory

Implements reranker::FactoryBase.

Definition at line 730 of file factory.H.

template<typename T>
shared_ptr<T> reranker::Factory< T >::CreateOrDie ( StreamTokenizer st,
Environment env = NULL 
)
inline

Dynamically creates an object, whose type and initialization are contained in a specification string, the tokens of which are given by the specified StreamTokenizer.

A specification string has the form

Typename(member1(init1), member2(init2), ...)

where the type of a member can be

  • a primitive (a string, double, int or bool),
  • a Factory-constructible type,
  • a vector of primtives or
  • a vector of types constructible by the same Factory.

In the case of members that are vectors, the init string can be a comma-separated list of of initializers for its elements. For example, the class ExampleFeatureExtractor class has three members that are registered to be initialized by Factory<FeatureExtractor> (via the ExampleFeatureExtractor::RegisterInitializers method):

  • a member named arg of type string,
  • a member named strvec of type vector<string> and
  • a member named b of type bool.

None of these is a “required” member, meaning they act like optional arguments to a constructor. The following are all legal specification strings for constructing instances of ExampleFeatureExtractor:

ExampleFeatureExtractor(arg("foo"), strvec({"foo", "bar", "baz"}), b(true))
ExampleFeatureExtractor(strvec({"foo", "bar", "baz",}))
ExampleFeatureExtractor(b(false))
ExampleFeatureExtractor()

Crucially, note how a vector can have an optional comma at the end of its list (the second example), and how a boolean may be initialized either with one of the two reserved words true or false, as in C and C++. Finally, unlike parameter lists to C++ constructors, since our members are always named, the grammar allows them to appear in any order, making the following two specification strings equivalent:

ExampleFeatureExtractor(arg("foo"), strvec({"foo", "bar", "baz"}))
ExampleFeatureExtractor(strvec({"foo", "bar", "baz"}), arg("foo"))

More formally, the specification string must conform to the following grammar:

<spec> ::= <type> '(' <member_init_list> ')'
<type> ::= name of type constructible by a Factory
<member_init_list> ::= <member_init> [ ',' <member_init> ]* [',']
<member_init> ::= <primitive_init> | <factory_init> | <primitive_vector_init> | <factory_vector_init>
<primitive_init> ::= <member_name> '(' <literal> ')'
<member_name> ::= the name of the member to be initialized, as specified by <type>’s RegisterInitializers method
<literal> ::= <string_literal> | <double_literal> | <int_literal> | <bool_literal>
<string_literal> ::= a C++ string literal (a string of characters surrounded by double quotes); double quotes and backslashes may be escaped inside a string literal with a backslash; other escape sequences, such as \t for the tab character, are currently not recognized
<double_literal> ::= a string that can be parsed by atof
<int_literal> ::= a string that can be parsed by atoi
<bool_literal> ::= true | false
<primitive_vector_init> ::= <member_name> '(' '{' <literal_list> '}' ')'
<literal_list> ::= <string_literal> [ ',' <string_literal> ]* [','] |
<double_literal> [ ',' <double_literal> ]* [','] |
<int_literal> [ ',' <int_literal> ]* [','] |
<bool_literal> [ ',' <bool_literal> ]* [',']
<factory_init> ::= <member_name> '(' <spec> ')'
<factory_vector_init> ::= <member_name> '(' '{' <spec_list> '}' ')'
<spec_list> ::= <spec> [ ',' <spec> ]* [',']
where every <spec> has a <type> constructible by the same Factory (i.e., all <type>’s have a common abstract base class)
Parameters
stthe stream tokenizer providing tokens according to the grammar shown above
envthe Environment in this method was called, or NULL if there is no calling environment

Definition at line 562 of file factory.H.

template<typename T>
shared_ptr<T> reranker::Factory< T >::CreateOrDie ( const string &  spec,
const string  err_msg,
Environment env = NULL 
)
inline

Definition at line 708 of file factory.H.

template<typename T>
virtual VarMapBase* reranker::Factory< T >::CreateVarMap ( Environment env) const
inlinevirtual

Implements reranker::FactoryBase.

Definition at line 741 of file factory.H.

template<typename T>
virtual VarMapBase* reranker::Factory< T >::CreateVectorVarMap ( Environment env) const
inlinevirtual

Implements reranker::FactoryBase.

Definition at line 746 of file factory.H.

template<typename T>
static bool reranker::Factory< T >::IsRegistered ( const string &  type)
inlinestatic

Returns whether the specified type has been registered with this factory (where registration happens typically via the REGISTER_NAMED macro).

Parameters
typethe type to be tested
Returns
whether the specified type has been registered with this factory

Definition at line 725 of file factory.H.

template<typename T>
static const Constructor<T>* reranker::Factory< T >::Register ( const string &  type,
const Constructor< T > *  p 
)
inlinestatic

The method used by the REGISTER_NAMED macro to ensure that subclasses add themselves to the factory.

Parameters
typethe type to be registered
pthe constructor for the specified type

Definition at line 757 of file factory.H.


The documentation for this class was generated from the following file: