⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _piclsdb.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Pi2API/_PIClsDB.cpp,v $
 * $Date: 2003/06/06 05:54:10 $
 *
 Description:
	Defined CONFIG_STATICLINK to reference all functions internally 
	regardless of the libraries they are meant to be in.

\*____________________________________________________________________________*/
//$SourceTop:$

#include "PiAPI.h"
#include "PIStrStr.h"
#include "_PIBase.h"
#include "_PIDBKey.h"
#include "_PIClass.h"
#include "_PIIO.h"
#include "_PILogic.h"
#include "_PISLL.h"
#include "_PILib.h"
#include "PIConfDB.h"
#include "AutoDel.h"
#include "SLLDB.h"

/*____________________________________________________________________________*\
 *
 Description:
	This DB has two additional types.
\*____________________________________________________________________________*/
#define PIDBTYPE_LIBRARY	PIDBTYPE_BUILTINSIZE
#define PIDBTYPE_CLASS		(PIDBTYPE_LIBRARY+1)

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class _PIClassDB : public _PIBaseDB
{
public:
    /* --- typeframes for the built-in types --- */
    enum { NUM_TYPES=2 };

private:
    static TypeFrame aTypeFrames[ NUM_TYPES ];

	bool FindClassesInConfigurationData( _PIDB *pDB );
	bool LoadClassFromConfigurationData( _PIDB *pDB );
	_PIClass::PfnFunction *CreateFunctionFrame( _PIDB *pDB, PIDLL *pLib,
		const char *pClassName, const char *pFnNames[], int iNumFns );
	void *LoadSymbol( PIDLL *pLib, _PIDB *pDB, const char *pSymbol,
		const char *pClassName );

	/* --- functions to manipulate objects of db type Class and Library --- */
	static void DestructLibrary( void *pV )
		{ PIDLL_delete( (PIDLL *)pV ); };
	static void RenderLibrary( ostream &os, const void *pV, int )
		{ os << (void *)pV; };
	static void DestructClass( void *pV )
		{ PI_DELETE( (_PIClass *)pV ); };
	static void RenderClass( ostream &os, const void *pV, int )
		{ os << (void *)pV; };


public:
    _PIClassDB( _PIDB *pParent );
    virtual ~_PIClassDB()
		{
		/* --- delete the classes while the libraries are still loaded --- */
		_PIDBIterator *pIter = 0;
		AutoDelete<_PIDBIterator> tA(pIter=GetIterator(PIDBTYPE_CLASS, 0, 0) );
		for(; pIter->AtValidElement(); pIter->Next() )
			{
/*	NOTE
			pIter->RemoveCurrent(); crashes under MSVC4.2  and is not
	necessary 

*/
			};
		};

	const _PIClass *LoadClass( PIConfigDB *pDB, const char *pClassName );
	bool LoadClasses();	

	/* --- functions to create specific classes --- */
	_PIClass *CreateIOClass( _PIDB *pDB, PIDLL *pLib, const char *pName );
	_PIClass *CreateLogicClass( _PIDB *pDB, PIDLL *pLib, const char *pName );
/*	_PIClass *CreateSLLClass( _PIDB *pDB, PIDLL *pLib, const char *pName ); */
};

/*____________________________________________________________________________*\
 *
 Global declarations:
    Type frames for the built-in types.
\*____________________________________________________________________________*/
TypeFrame _PIClassDB::aTypeFrames[ _PIClassDB::NUM_TYPES ] =
{
{	"Library",
	"Dynamically loaded library",
	0,
	_PIClassDB::RenderLibrary,
	_PIClassDB::DestructLibrary
},
{	"Class",
	"Dynamically loaded class",
	0,
	_PIClassDB::RenderClass,
	_PIClassDB::DestructClass
}
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
_PIClassDB::_PIClassDB( _PIDB *pDB )
:	_PIBaseDB( pDB,
		PIDBKEY_CLASSES,
		NUM_TYPES,
		aTypeFrames )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool _PIClassDB::LoadClasses()
{
	_PIDBIterator *pIter = 0;
	AutoDelete<_PIDBIterator> tA( pIter = GetIterator( PIDBTYPE_TREE,
		PIDBKEY_CONFIGURATION, PIDBFLAG_PROPAGATEUP ) );
	if ( !pIter || !pIter->AtValidElement() )
        {
        Error( this, "Unable to load Classes. No configuration files \
have been loaded. ");
		return false;
        };
	bool bOK = true;
	for( ; bOK && pIter->AtValidElement(); pIter->Next() )
		{
		bOK = FindClassesInConfigurationData( (_PIDB *)pIter->Current( 0 ) );
		};
	return bOK;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool _PIClassDB::FindClassesInConfigurationData( _PIDB *pDB )
{
	assert( pDB );
	if ( !pDB ) { return false; };
	_PIDBIterator *pIter = 0;
	AutoDelete<_PIDBIterator> tA( pIter=pDB->GetIterator(PIDBTYPE_TREE,
		PIDBKEY_CONF_CLASS, 0 ) );
	bool bOK = true;
    for(; bOK && pIter->AtValidElement(); pIter->Next() )
        {
        _PIDB *pValue = (_PIDB *)pIter->Current( 0 );
		if ( pValue )
			{
			bOK = LoadClassFromConfigurationData( pValue );
			};
        }; 
	return bOK;
}

/*____________________________________________________________________________*\
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static _PIClassDB *Internal_LookupClassDB( _PIDB *pDB )
{
	assert( pDB );
	_PIBaseDB *pLib = _PILibrary::GetLibDB( pDB );
	assert( pLib );
	_PIClassDB *pClasses = (_PIClassDB *)pLib->Lookup( PIDBTYPE_TREE,
		PIDBKEY_CLASSES, 0 );
	if ( !pClasses )
		{
		pClasses = PI_NEW( _PIClassDB( pLib ) );
		};
	return pClasses;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Load a specific class given a pointer to a configuration DB.
\*____________________________________________________________________________*/
const _PIClass *_PIClassDB::LoadClass( PIConfigDB *pDB,
	const char *pClassName )
{
	assert( pClassName );
	if ( !pDB ) 
		{ pDB = PIConfigDB::FindConfiguration( this ); };
	if ( !pDB ) 
		{ return 0; };

	PIConfigDB *pConf = 0;
	_PIDBIterator *pIter = 0;
	AutoDelete<_PIDBIterator> tA( pIter = pDB->GetIterator(
		PIDBTYPE_TREE, PIDBKEY_CONF_CLASS, 0 ) );
	for( ; pIter && pIter->AtValidElement(); pIter->Next() )
		{
		const char *pName = PIConfigDB::LookupValue( (PIConfigDB *)
			pIter->Current( 0 ), PIDBKEY_CONF_NAME, 0, 0 );
		if ( pName && !strcmp( pName, pClassName ) )
			{
			pConf = (PIConfigDB *)pIter->Current( 0 );
			break;
			};
		};

	if ( !pConf )
		{	
		PIOStrStream os;
		os << "No definition for class '" << pClassName << "'" << ends;
		Error( pDB, os.str() );
		return 0;
		};
	if ( LoadClassFromConfigurationData( pConf ) )
		{
		return _PIClass::Lookup( this, pClassName );
		};
	return 0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
struct {
	const char *pClassType;
	_PIClass *(_PIClassDB:: *fnCreate)( _PIDB *, PIDLL *, const char * );
} aClassCreationFns[]=
{
{ PIDBVALUE_CONF_IO, &_PIClassDB::CreateIOClass },
{ PIDBVALUE_CONF_LOGIC, &_PIClassDB::CreateLogicClass },
/* { PIDBVALUE_CONF_SLL, _PIClassDB::CreateSLLClass }, */
{ 0, 0 } 
};
bool _PIClassDB::LoadClassFromConfigurationData( _PIDB *pDB )
{
	assert( pDB );
	const char *pLibrary = PIConfigDB::LookupValue( (PIConfigDB *)pDB,
		PIDBKEY_CONF_LIBRARY, 0, 0 );
	const char *pType = PIConfigDB::LookupValue( (PIConfigDB *)pDB,
		PIDBKEY_CONF_TYPE, 0, 0 );
	const char *pName = PIConfigDB::LookupValue( (PIConfigDB *)pDB,
		PIDBKEY_CONF_NAME, 0, 0 );
	if ( !pLibrary || !pType || !pName )
		{
		Error( pDB, "One of 'Name', 'Type' or 'Library' missing from class \
specification." );
		return false;
		};

	/* --- find a match in the class type --- */	
	const char *pTmp;
	_PIClass *(_PIClassDB:: *fnCreate)( _PIDB *, PIDLL *, const char * )
		= 0;
	for(int i=0; (pTmp=aClassCreationFns[i].pClassType); i++)
		{
		if ( !strcmp( pTmp, pType ) ) 
			{
			fnCreate = aClassCreationFns[i].fnCreate;
			break;
			};
		};
	if ( fnCreate==0 )
		{
		PIOStrStream os;
		os << "Unknown type '" << pType << "'" << ends;
		Error( pDB, os.str() );
		return false;
		};

	PIDLL *pLib = 0;
#if !defined(CONFIG_STATICLINK)
	/*
	** For dynamic linking lookup the library and load it etc.
	**
	** For static linking lookup function names based on internal
	** data structures
	**
	*/
	if ( !PIUtil_stricmp( pLibrary, "##Internal##" ) )
		{
		/* --- internal library --- */
		}
	else
		{
		/* --- attempt to load the library --- */
		pLib = _PILibrary::LookupOrLoad( this, 0, pLibrary );
		if ( !pLib || !PIDLL_isLoaded( pLib ) )
			{
			PIOStrStream os;
			os << "Unable to load library '" << pLibrary << "'";
			if ( pLib )
				{ os << ", error description is <"
					<< PIDLL_getErrorDescription( pLib ) 
					<< ">.";
				};
			os << ends;
			Error( pDB, os.str() );
			return false;
			};
		};
#endif
	_PIClass *pClass = (this->*fnCreate)( pDB, pLib, pName );

	return pClass!=0;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
_PIClass::PfnFunction *_PIClassDB::CreateFunctionFrame( _PIDB *pDB, PIDLL *pLib,
	const char *pClassName, const char *pFnNames[], int iNumFns )
{
	assert( pDB && pClassName && pFnNames && iNumFns );

	_PIClass::PfnFunction *pFunctionFrame = PI_NEW(
		_PIClass::PfnFunction[iNumFns] );

	bool bOK = true;
	int i;
	for(i=0; bOK && i<iNumFns; i++)
		{
		const char *pKey = pFnNames[i];
		assert( pKey );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -