Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.5M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
#ifndef _MTCOMPLUGINADAPTER_INCLUDED
#define _MTCOMPLUGINADAPTER_INCLUDED

#include "MTParser.h"
#include <atlbase.h>
#import "../MTPluginIDL/MTPluginIDL.tlb" no_namespace 

// @class Plugin loader
// Load a COM plugin: constants, operators and functions.
class MTCOMPluginLoader
{
public:

	// Load this plugin
	// @param plugin: plugin object (class)
	// @param pMathExpr: all items contained in the plugin will be defined
	// in this math expression object.	
	static bool loadPlugin(CLSID plugin, MTParser *pMathExpr) throw(MTDefException);	
};

// @class Adapter to allow COM plugin functions to be handled
// like native c++ functions.
// All calls will be delegated to the plugin
class MTCOMFuncAdapter : public MTFunctionI
{
public:

	void init(int id, CComPtr<IMTPlugin> &pPlugin);
	~MTCOMFuncAdapter();

	//******************************
	// MTFunctionI implementation

	virtual MTSTRING getSymbol();
	virtual MTSTRING getHelpString();
	virtual MTSTRING getDescription();
	virtual int getNbArgs();
	virtual bool isConstant();
	virtual MTDOUBLE evaluate(unsigned int nbArgs, const MTDOUBLE *pArg);
	
	virtual MTFunctionI* spawn();
	
private:

	int m_id;
	CComPtr<IMTPlugin> m_pPlugin;
};

// @class Adapter to allow COM plugin operators to be handled
// like native c++ operators.
// All calls will be delegated to the plugin
class MTCOMOpAdapter : public MTOperatorI
{
public:
	
	void init(int id, CComPtr<IMTPlugin> &plugin);
	~MTCOMOpAdapter();

	//******************************
	// MTOperatorI implementation

	virtual EMathOpPrecedence getPrecedence();
	virtual MTSTRING getSymbol();
	virtual MTSTRING getHelpString();
	virtual MTSTRING getDescription();
	virtual MTDOUBLE evaluate(unsigned int nbArgs, const MTDOUBLE *pArg);

	virtual MTOperatorI* spawn();

private:

	int m_id;
	CComPtr<IMTPlugin> m_pPlugin;
};


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions