InFact
Interpreter and factory for easily creating C++ objects at run-time
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Static Public Member Functions | List of all members
infact::Factory< T > Class Template Reference

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

#include <factory.h>

Inheritance diagram for infact::Factory< T >:
infact::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=nullptr)
 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=nullptr)
 
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 infact::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 infact::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 593 of file factory.h.

Constructor & Destructor Documentation

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

Constructs a new factory.

Definition at line 596 of file factory.h.

Member Function Documentation

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

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

Implements infact::FactoryBase.

Definition at line 932 of file factory.h.

template<typename T>
virtual void infact::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 infact::FactoryBase.

Definition at line 603 of file factory.h.

template<typename T>
static void infact::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 997 of file factory.h.

template<typename T>
virtual void infact::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 infact::FactoryBase.

Definition at line 946 of file factory.h.

template<typename T>
shared_ptr<T> infact::Factory< T >::CreateOrDie ( StreamTokenizer st,
Environment env = nullptr 
)
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 Cow has two members that are registered to be initialized by Factory<Cow> (via the Cow::RegisterInitializers method):

  • a member named name of type string,
  • a member named age of type int

Only the first of these is a “required” member, meaning the age member acts like an optional argument to a constructor. The following are all legal specification strings for constructing instances of Cow:

Cow(name("foo"), age(3))
Cow(age(3), name("foo"))
Cow(name("foo"))

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 be a <spec_or_null> conforming to the following grammar:

<spec_or_null> ::= <spec> | 'NULL' | 'nullptr'
<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> ')' | '=' <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> '}'
<literal_list> ::= <string_literal> [ ',' <string_literal> ]* [','] |
<double_literal> [ ',' <double_literal> ]* [','] |
<int_literal> [ ',' <int_literal> ]* [','] |
<bool_literal> [ ',' <bool_literal> ]* [',']
<factory_init> ::= <member_name> [ '(' <spec_or_null> ')' | '=' <spec_or_null> ]
<factory_vector_init> ::= <member_name> [ '(' '{' <spec_list> '}' ')' | '=' '{' <spec_list> '}'
<spec_list> ::= <spec_or_null> [ ',' <spec_or_null> ]* [',']
where every <spec_or_null> has a <type> constructible by the same Factory (i.e., all <type>’s have a common abstract base class), or is either 'NULL' or 'nullptr'
Parameters
stthe stream tokenizer providing tokens according to the grammar shown above
envthe Environment in this method was called, or nullptr if there is no calling environment

Definition at line 766 of file factory.h.

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

Definition at line 924 of file factory.h.

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

Implements infact::FactoryBase.

Definition at line 957 of file factory.h.

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

Implements infact::FactoryBase.

Definition at line 962 of file factory.h.

template<typename T>
static bool infact::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 941 of file factory.h.

template<typename T>
static const Constructor<T>* infact::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 974 of file factory.h.


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