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

📄 umlentityclasstemplate.cpp

📁 uml编辑器很牛
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

CString CUMLEntityClassTemplate::GetOperationList( int format ) const
/* ============================================================
	Function :		CUMLEntityClassTemplae::GetOperationList
	Description :	Returns a string with the operations of 
					this object in the format "format".
	Access :		Protected

	Return :		CString		-	Result
	Parameters :	int format	-	Format to use.
					
	Usage :			Call as a part of an export, for example.
					"format" can be one of the following:
						"EXPORT_CPP" Export to cpp-files
						"EXPORT_H" Export to header files
						"EXPORT_HTML" Export to HTML-files

   ============================================================*/
{
	CString result;
	CString title = GetTitle();

	if( format == EXPORT_H )
	{
		int oldaccess = ACCESS_TYPE_PUBLIC;
		int max = GetOperations();
		for( int pass = 0; pass < 2 ; pass++ )
		{
			// First pass, we will do ctors and dtors,
			// second, the rest
			if( pass == 0 )
				result += _T( "// Construction/destruction\r\n" );
			else
				result += _T( "\r\n// Operations\r\n" );

			for( int t = 0; t < max ; t++ )
			{
				COperation* op = GetOperation( t );
				if( ( pass == 0 && ( op->name == title || op->name == _T( "~" ) + title ) ) || ( pass != 0 && ( op->name != title && op->name != _T( "~" ) + title ) ) )
				{
					if( op->access != oldaccess )
					{
						switch( op->access )
						{
							case ACCESS_TYPE_PRIVATE:
								result += _T( "\r\nprivate:\r\n" );
							break;
							case ACCESS_TYPE_PROTECTED:
								result += _T( "\r\nprotected:\r\n" );
							break;
							case ACCESS_TYPE_PUBLIC:
								result += _T( "\r\npublic:\r\n" );
							break;
						}
					}

					if( pass == 0 )
						result += op->GetString( STRING_FORMAT_H_CTOR_TEMPLATE ) + _T( "\r\n" );
					else
						result += op->GetString( STRING_FORMAT_H_TEMPLATE ) + _T( "\r\n" );

					oldaccess = op->access;
				}

			}
		}
	}
	else
		result = CUMLEntityClass::GetOperationList( format );

	return result;
}

#define STATE_STARTING	0
#define STATE_READING	1

BOOL CUMLEntityClassTemplate::ImportH( const CString& filename )
/* ============================================================
	Function :		CUMLEntityClassTemplate::ImportH
	Description :	Imports information from a c++ h-file to 
					this object.
	Access :		Protected

	Return :		BOOL					-	"TRUE" if the header 
												could be parsed
	Parameters :	const CString& filename	-	Import from this
												file.
					
	Usage :			Call to import data for this object from a 
					c++ header-file.

   ============================================================*/
{

	ClearAttributes();
	ClearOperations();
	ClearProperties();

	CTextFile		file( _T( "" ), _T( "\n" ) );;
	CString			localfilename( filename );
	CString			str;
	if( file.ReadTextFile( localfilename, str ) )
	{
		int comment = str.Find( _T( "/*" ) );
		while( comment != -1 )
		{
			int endcomment = str.Find( _T( "*/" ) );
			if( endcomment != -1 )
				str = str.Left( comment ) + str.Right( str.GetLength() - ( endcomment + 2 ) );
			else
			{
				CString err;
				err.LoadString( IDS_UML_MALFORMED_COMMENT );
				GetUMLContainer()->SetErrorMessage( err );
				return FALSE;
			}

			comment = str.Find( _T( "/*" ) );
		}

		CTokenizer tok( str, _T( "\n" ) );
		CStringArray classContents;
		int max = tok.GetSize();
		int state = STATE_STARTING;
		int rb = 0;
		for( int t = 0; t < max ; t++ )
		{
			CString data;
			tok.GetAt( t, data );
			data.TrimLeft();
			int found = data.Find( _T( "//" ) );
			if( found != -1 )
				data = data.Left( found );
			data.TrimRight();
			if( data.GetLength() )
			{
				switch( state )
				{
					case STATE_STARTING:
						if( data.Left( 8 ) == _T( "template" ) )
						{
							if( data.Find( _TCHAR( '{' ) ) != -1 )
								rb++;
							state = STATE_READING;
							classContents.Add( data );
						}
						break;
					case STATE_READING:
						if( data.Find( _TCHAR( '{' ) ) != -1 )
							rb++;
						if( data.Find( _TCHAR( '}' ) ) != -1 )
							rb--;

						if( rb && data.GetLength() && data[0] != _TCHAR( '#' ) )
							classContents.Add( data );

						break;
				}
			}
		}

		// Now, we assemble the cleaned contents into one single string,
		// to be able to clear out inline functions.
		max = classContents.GetSize();
		str = _T( "" );
		for( t = 0 ; t < max ; t++ )
			str += classContents[ t ] + _T("\n" );
		int leftbracket = str.Find( _TCHAR( '{' ) );
		// We must remove the first bracket found
		if( leftbracket != -1 )
			str = str.Left( leftbracket ) + str.Right( str.GetLength() - ( leftbracket + 1 ) );

		leftbracket = str.Find( _TCHAR( '{' ) );
		while( leftbracket!= -1 )
		{
			int rightbracket = str.Find( _T( "};" ) );
			if( rightbracket != -1 )
			{
				CString str1 = str.Left( leftbracket );
				CString str2 = str.Right( str.GetLength() - ( rightbracket + 1 ) );
				str = str1 + str2;
			}
			else
			{
				CString err;
				err.LoadString( IDS_UML_MISSING_BRACKET );
				GetUMLContainer()->SetErrorMessage( err );
				return FALSE;
			}

			leftbracket = str.Find( _TCHAR( '{' ) );
		}

		// To make sure declarations are ended with a ";"
		str.Replace( _T( "\n;" ), _T( ";" ) );

		// Now, we tokenize again
		CString className;
		CString baseClass;
		CString parameterType;

		tok.Init( str, _T( "\n" ) );
		max = tok.GetSize();
		classContents.RemoveAll();
		for( t = 0 ; t < max ; t++ )
		{
			CString data;
			tok.GetAt( t, data );
			data.TrimRight();

			if( data.GetLength() )
			{
				data.Replace( _TCHAR( '\t' ), _TCHAR( ' ' ) );
				data.Replace( _T( " *" ), _T( "*" ) );
				data.Replace( _T( " &" ), _T( "&" ) );
				data.Replace( _T( " (" ), _T( "(" ) );
				data.Replace( _T( "( " ), _T( "(" ) );
				data.Replace( _T( " )" ), _T( ")" ) );
				data.Replace( _T( " ;" ), _T( ";" ) );
				data.Replace( _T( " [" ), _T( "[" ) );
				data.Replace( _T( " ]" ), _T( "]" ) );
				data.Replace( _T( " =" ), _T( "=" ) );
				data.Replace( _T( "= " ), _T( "=" ) );
				data.Replace( _T( "< " ), _T( "<" ) );
				data.Replace( _T( " >" ), _T( ">" ) );

				data.Replace( _T( "*" ), _T( "* " ) );
				data.Replace( _T( "&" ), _T( "& " ) );

				data.Replace( _T( "  " ), _T( " " ) );
				data.Replace( _T( "  " ), _T( " " ) );

				if( t == 0 )
				{
					// The first line should contain the class name
					int colon = data.Find( _TCHAR( ':' ) );
					if( colon != -1 )
					{
						baseClass = data.Right( data.GetLength() - ( colon + 1 ) );
						className = data.Left( colon );
						className = className.Right( className.GetLength() - 5 ); // remove 'class'
						className.TrimLeft();
						className.TrimRight();
						baseClass.TrimLeft();
						baseClass.TrimRight();
					}
					else
					{
						int space = data.ReverseFind( _TCHAR( ' ' ) );
						if( space != -1 )
						{
							className = data.Right( data.GetLength() - ( space + 1 ) );
							parameterType = data.Left( space );
							parameterType = parameterType.Right( parameterType.GetLength() - 9 );
							space = parameterType.Find( _TCHAR( '>' ) );
							if( space != -1 )
								parameterType = parameterType.Left( space );
						}
					}
				}
				else
				{
					if( data[ data.GetLength() - 1 ] == _TCHAR( ';' ) || data[ data.GetLength() - 1 ] == _TCHAR( ':' ) )
						// We want to keep only access markers and declarations
						classContents.Add( data );
		
				}
			}

		}

		if( className.IsEmpty() )
			return FALSE;

		// Now, we are left with a list of the declarations and access markers from the header file. Now, we will start to create attributes and operations from those. We will recognize an operation as it will have a paranthesis somewhere in the string.
		max = classContents.GetSize();
		int access = ACCESS_TYPE_PRIVATE;
		for( t = 0 ; t < max ; t++ )
		{
			CString data = classContents[ t ];
			if( data[ data.GetLength() - 1 ] == _TCHAR( ':' ) )
			{
				// An access marker
				if( data == _T( "private:" ) )
					access = ACCESS_TYPE_PRIVATE;
				else if( data == _T( "protected:" ) )
					access = ACCESS_TYPE_PROTECTED;
				else if( data == _T( "public:" ) )
					access = ACCESS_TYPE_PUBLIC;
			}
			else if( data.Find( _TCHAR( '(' ) ) != -1 )
			{
				// A member function
				COperation* op = new COperation;
				op->access = access;

				// Get the main type of the function
				CString afxmsgmarker( _T( "afx_msg " ) );
				int afxmsgmarkerfound = data.Find( afxmsgmarker ) ;
				if( afxmsgmarkerfound != -1 )
					data = data.Left( afxmsgmarkerfound ) + data.Right( data.GetLength() - ( afxmsgmarkerfound + afxmsgmarker.GetLength() ) );

				CString virtualmarker( _T( "virtual " ) );
				int virtualmarkerfound = data.Find( virtualmarker ) ;
				if( virtualmarkerfound != -1 )
				{
					data = data.Left( virtualmarkerfound ) + data.Right( data.GetLength() - ( virtualmarkerfound + virtualmarker.GetLength() ) );
					op->properties.Add( _T( "virtual" ) );
				}

				CString staticmarker( _T( "static " ) );
				int staticmarkerfound = data.Find( staticmarker ) ;
				if( staticmarkerfound != -1 )
				{
					data = data.Left( staticmarkerfound ) + data.Right( data.GetLength() - ( staticmarkerfound + staticmarker.GetLength() ) );
					op->maintype |= ENTITY_TYPE_STATIC;
				}

				// Get the return value of the function
				int space = data.Find( _TCHAR( ' ' ) );
				int paramstart = data.Find( _TCHAR( '(' ) );
				if( space != -1 && paramstart > space )
				{
					CString type = data.Left( space );
					data = data.Right( data.GetLength() - ( space + 1 ) );
					if( type == _T( "void" ) )
						type = _T( "" );

					op->type = type;
				}

				paramstart = data.Find( _TCHAR( '(' ) );
				if( paramstart != -1 )
				{
					CString name = data.Left( paramstart );
					data = data.Right( data.GetLength() - ( paramstart + 1 ) );
					op->name = name;
				}

				//////////////////////////////////////////////////////
				// Function parameters
				// 
				int paramend = data.ReverseFind( _TCHAR( ')' ) );
				if( paramend != -1 )
				{
					CString params = data.Left( paramend );
					data = data.Right( data.GetLength() - ( paramend + 1 ) );
					int paramlen = params.GetLength();
					if( paramlen )
					{
						tok.Init( params );
						int size = tok.GetSize();
						for( int i = 0 ; i < size ; i++ )
						{
							CString param;
							tok.GetAt( i, param );
							param.TrimLeft();
							param.TrimRight();
							int space = param.ReverseFind( _TCHAR( ' ' ) );
							if( space != -1 )
							{
								CString type = param.Left( space );
								CString name = param.Right( param.GetLength() - ( space + 1 ) );
								CString defval;
								int eq = name.Find( _TCHAR( '=' ) );
								if( eq != -1 )
								{
									defval = name.Right( name.GetLength() - ( eq + 1 ) );
									name = name.Left( eq );
								}

								CParameter* param = new CParameter;
								param->name = name;
								param->defaultvalue = defval;
								param->in = TRUE;

								param->out = FALSE;
								if( type.Remove( _TCHAR( '&' ) ) )
								{
									param->out = TRUE;
									param->reference = TRUE;
								}

								if( type.Remove( _TCHAR( '*' ) ) )
									param->out = TRUE;

								CString constmarker( _T( "const " ) );
								int constmarkerfound = type.Find( constmarker ) ;
								if( constmarkerfound != -1 )
								{

									type = type.Left( constmarkerfound ) + type.Right( type.GetLength() - ( constmarkerfound + constmarker.GetLength() ) );
									param->out = FALSE;

								}

								param->type = type;
								op->parameters.Add( param );

							}
						}
					}
				}

				CString constmarker( _T( "const" ) );
				int constmarkerfound = data.Find( constmarker );
				if( constmarkerfound != -1 )
				{
					data = data.Left( constmarkerfound ) + data.Right( data.GetLength() - ( constmarkerfound + constmarker.GetLength() ) );
					op->properties.Add( _T( "query" ) );
				}

				CString virtmarker( _T( "=0" ) );
				int virtmarkerfound = data.Find( virtmarker );
				if( virtmarkerfound != -1 )
				{
					data = data.Left( virtmarkerfound ) + data.Right( data.GetLength() - ( virtmarkerfound + virtmarker.GetLength() ) );
					op->maintype |= ENTITY_TYPE_ABSTRACT;
				}

				AddOperation( op );

			}
			else
			{

				// A member variable
				// Parse into type and name
				data = data.Left( data.GetLength() - 1 ); // remove semiconol
				int space = data.Find( _TCHAR( ' ' ) );
				if( space != -1 )
				{
					CAttribute* attr = new CAttribute;
					attr->maintype = ENTITY_TYPE_NONE;

				CString staticmarker( _T( "static " ) );
				int staticmarkerfound = data.Find( staticmarker ) ;
				if( staticmarkerfound != -1 )
				{
					data = data.Left( staticmarkerfound ) + data.Right( data.GetLength() - ( staticmarkerfound + staticmarker.GetLength() ) );
					attr->maintype |= ENTITY_TYPE_STATIC;
				}

					int space = data.Find( _TCHAR( ' ' ) );
					CString type = data.Left( space );
					CString name = data.Right( data.GetLength() - ( space + 1 ) );
					int bracket = name.Find( _TCHAR( '[' ) );
					if( bracket != -1 )
					{
						CString multiplicity = name.Right( name.GetLength() - ( bracket + 1 ) );
						name = name.Left( bracket );
						if( multiplicity.GetLength() )
							multiplicity = multiplicity.Left( multiplicity.GetLength() - 1 ); // Removing closing bracket
						attr->multiplicity = multiplicity;
					}

					attr->access = access;
					attr->name = name;
					attr->type = type;

					AddAttribute( attr );
				}
			}
		}

		SetTitle( className );
		SetParameterType( parameterType );

		if( baseClass.GetLength() )
		{
			CString base;
			CTokenizer tok( baseClass );
			int max = tok.GetSize();
			for( int t = 0 ; t < max ; t++ )
			{
				CString cls;
				tok.GetAt( t, cls );
				cls.TrimLeft();
				int find = cls.Find( _TCHAR( ' ' ) );
				if( find != -1 )
					cls = cls.Right( cls.GetLength() - ( find + 1 ) );
				cls.TrimLeft();
				cls.TrimRight();
				base += cls + _T( " " );
			}

			base.TrimRight();
			GetProperties()->Add( _T( "baseClass" ), base );

		}

		CalcRestraints();
		return TRUE;
	}

	return FALSE;
}

⌨️ 快捷键说明

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