Click here to Skip to main content
15,884,176 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.4M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
	
// MTFirstPlugin.h : Declaration of the CMTFirstPlugin

#ifndef __MTFIRSTPLUGIN_H_
#define __MTFIRSTPLUGIN_H_

#include "resource.h"       // main symbols
#import "..\MTPluginIDL\MTPluginIDL.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
#include <vector>

/////////////////////////////////////////////////////////////////////////////
// CMTFirstPlugin
class ATL_NO_VTABLE CMTFirstPlugin : 
	public CComObjectRootEx<CComMultiThreadModel>,
	public CComCoClass<CMTFirstPlugin, &CLSID_MTFirstPlugin>,
	public ISupportErrorInfo,
	public IDispatchImpl<IMTFirstPlugin, &IID_IMTFirstPlugin, &LIBID_MTPLUGINDEMOLib>,
	public IDispatchImpl<IMTPlugin, &IID_IMTPlugin, &LIBID_MTPLUGINIDLLib>
{
public:
	CMTFirstPlugin()
	{
		m_pUnkMarshaler = NULL;
		initPlugin();
	}

	~CMTFirstPlugin();

DECLARE_REGISTRY_RESOURCEID(IDR_MTFIRSTPLUGIN)
DECLARE_GET_CONTROLLING_UNKNOWN()

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CMTFirstPlugin)
	COM_INTERFACE_ENTRY(IMTFirstPlugin)
//DEL 	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.p)
	COM_INTERFACE_ENTRY2(IDispatch, IMTFirstPlugin)
	COM_INTERFACE_ENTRY(IMTPlugin)
END_COM_MAP()

	HRESULT FinalConstruct()
	{
		return CoCreateFreeThreadedMarshaler(
			GetControllingUnknown(), &m_pUnkMarshaler.p);
	}

	void FinalRelease()
	{
		m_pUnkMarshaler.Release();
	}

	CComPtr<IUnknown> m_pUnkMarshaler;

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IMTFirstPlugin
public:
// IMTPlugin
	STDMETHOD(getNbConsts)(INT * pNbConsts);	
	STDMETHOD(getConst)(INT id, BSTR * name, DOUBLE * pVal);	
	STDMETHOD(getNbFuncs)(INT * pNbFuncs);	
	STDMETHOD(getFuncSymbol)(INT id, BSTR * symbol);
	STDMETHOD(getFuncHelpString)(INT id, BSTR * helpString);
	STDMETHOD(getFuncDescription)(INT id, BSTR * description);
	STDMETHOD(isFuncConstant)(INT id, INT * pConstant);
	STDMETHOD(getFuncNbArgs)(INT id, INT * pNbArgs);
	STDMETHOD(evaluateFunc)(INT id, INT nbArgs, DOUBLE * pArg, DOUBLE * pResult);	
	STDMETHOD(getNbOps)(INT * pNbOps);	
	STDMETHOD(getOpPrecedence)(INT id, INT * pPrecedence);
	STDMETHOD(isOpConstant)(INT id, INT * pConstant);	
	STDMETHOD(getOpSymbol)(INT id, BSTR * symbol);
	STDMETHOD(getOpHelpString)(INT id, BSTR * helpString);
	STDMETHOD(getOpDescription)(INT id, BSTR * description);
	STDMETHOD(isOneArgOp)(INT id, INT * pOneArg);
	STDMETHOD(evaluateOp)(INT id, INT nbArgs, DOUBLE * pArg, DOUBLE * pResult);
	
private:

	// create function and operator objects
	void initPlugin();

	// add a constant
	void addConst(LPCTSTR name, double value);

private:

	struct SConstant
	{
		CComBSTR name;
		double value;
	};

	std::vector<class IMathFunction*> m_funcs;
	std::vector<class IMathOperator*> m_ops;
	std::vector<SConstant> m_consts;

};

#endif //__MTFIRSTPLUGIN_H_

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