CT++ user defined functions API
At the given moment all user functions realize by means of the inheritance from the abstract class udf_fn (User Defined FuNction) from the namespace template_parser_ns.
For the transmission of data in class is provided the overloaded method param. Method assume upon the entrance one, two or three parameters of the type std::string, or one parameter of the type std::vector<std::string>.
Class must inform the causing process about a quantity of the necessary variables adopted. For this serves method accept_params(). Method must return one of four values: ONE_PARAM (one parameter), TWO_PARAMS (two parameters), THREE_PARAMS (three parameters) or ANY_PARAMS (vector with the unlimited quantity of parameters).
The interface of class is given below:
namespace template_parser_ns
{
//
// Parameters, transferred to the function
//
typedef std::vector<std::string> udf_fn_param;
//
// User defined function
//
class udf_fn
{
public:
// A quantity of the parameters
enum e_accept_params {ONE_PARAM, TWO_PARAMS, THREE_PARAMS, ANY_PARAMS};
// Give parameters
// Unlimited quantity of parameters
virtual void param(udf_fn_param * pParam);
// One parameter
virtual void param(const std::string &sParam);
// Two parameters
virtual void param(const std::string &sParam1, const std::string &sParam2);
// Three parameters
virtual void param(const std::string &sParam1, const std::string &sParam2, const std::string &sParam3);
// Number of the accepted parameters
virtual e_accept_params accept_params() = 0;
// Function handler
virtual void handler() = 0;
// Result
virtual std::string & result() = 0;
// Destructor
virtual ~udf_fn() { ;; }
};
} // namespace template_parser_ns
For the connection of new user function it is necessary to introduce the created class into the factory of objects.
There are two methods of using the factory of the objects: creation by new and the use of current from the object of template engine.
For creating the new factory of objects is used class udf_fn_factory from namespace template_parser_ns:
// Create the factory of the objects
template_parser_ns::udf_fn_factory * pFactory = new udf_fn_factory();
// Create the object of user function
template_parser_ns::udf_fn * pFunction = new user_function();
// Create function in the factory of the objects
pFactory -> install_udf_fn("FOOBAR", pFunction);
// Create the object of template engine
template_text * pTemplate = new template_text(pFactory);
In the case of using the current factory of objects the code will be approximately following:
// Create the factory of the objects
template_parser_ns::udf_fn_factory * pFactory = pTemplate -> get_fn_factory();
// Create the object of user function
template_parser_ns::udf_fn * pFunction = new user_function();
// Create function in the factory of the objects
pFactory -> install_udf_fn("FOOBAR", pFunction);
Copyright © 2003 - 2005 CTPP Dev. Team | http://reki.ru/products/ctpp