Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / ATL

CM_ConfigBuilder 1.2g: Visual Studio 6/Visual Studio 2005/Visual Studio 2008 Code Generator for Application Settings Graphic Management

Rate me:
Please Sign up or sign in to vote.
4.94/5 (126 votes)
12 Feb 2008CPOL17 min read 696.3K   9.8K   262  
CM_ConfigBuilder generates and compiles the required files to manage your application's settings/preferences and to store/retrieve them in XML format.
// CCppClassArrayGenerator.cpp
//
#include "stdafx.h"
#include "CppClassArrayGenerator.h"
#include "XmlProjectSettings.h"
#include "XmlClassInfo.h"
#include "XmlMemberInfo.h"

CCppClassArrayGenerator::CCppClassArrayGenerator()
{}

CCppClassArrayGenerator::~CCppClassArrayGenerator()
{}

void CCppClassArrayGenerator::Generate()
{
	// generate header file
	//
	GenerateHeader();
	
	// generate implementation file
	//
	GenerateImplementation();
}
	
void CCppClassArrayGenerator::GenerateHeader()
{
	string fileName;
	string pathName;

	Head_generateHeader();
	Head_generateIfDefBegin();
	Head_generateIncludeSection();
	Head_generateForwardDecl();
	Head_generateClassBegin();
	Head_generateConstructor();
	Head_generateDestructor();
	Head_generateMethods();
	Head_generateSerialization();
	Head_generateMembers();
	Head_generateClassEnd();
	Head_generateIfDefEnd();

	pathName = BuildGeneratedSourcePath() + "include\\";
	fileName = GetClassName() + GetCardinalityPostfix(classInfo_) + ".h";
	SaveFile(pathName + fileName, headCode_);

	hGeneratedFiles_[fileName] = pathName + fileName; 
}

void CCppClassArrayGenerator::Head_generateHeader()
{
	headCode_ += "// " + GetClassName() + GetCardinalityPostfix(classInfo_) + ".h" + xCrLf;
	headCode_ += "// " + xCrLf; 
	headCode_ += GetGenerationTimeStamp();
}

void CCppClassArrayGenerator::Head_generateIfDefBegin()
{
	headCode_ += "#if !defined(" + DefineOf(GetClassName()) + GetCardinalityPostfix(classInfo_) + ")" + xCrLf;
	headCode_ += "#define " + DefineOf(GetClassName()) + GetCardinalityPostfix(classInfo_) + xCrLf;
	headCode_ += xCrLf;
}

void CCppClassArrayGenerator::Head_generateIncludeSection()
{
	headCode_ += "#include <string>" + xCrLf;
	headCode_ += "#include <vector>" + xCrLf;
	headCode_ += "#include \"XmlSerializable.h\"" + xCrLf;
	
	// include contained class header
	//
	headCode_ += "#include \"" + GetClassName(classInfo_->GetName()) + ".h\"" + xCrLf;
	headCode_ += xCrLf;

	headCode_ += xCrLf;
	headCode_ += "using namespace std;" + xCrLf;
	headCode_ += xCrLf;
}

void CCppClassArrayGenerator::Head_generateForwardDecl()
{
}

void CCppClassArrayGenerator::Head_generateClassBegin()
{
	headCode_ += "class AFX_EXT_CLASS " + GetClassName() + GetCardinalityPostfix(classInfo_) + ": public CXmlSerializable" + xCrLf;
	headCode_ += "{" + xCrLf;
}

void CCppClassArrayGenerator::Head_generateConstructor()
{
	headCode_ += "public:" + xCrLf;
	headCode_ += xTab + GetClassName() + GetCardinalityPostfix(classInfo_) + "();" + xCrLf;
	headCode_ += xTab + GetClassName() + GetCardinalityPostfix(classInfo_) + "(const " + GetClassName() + GetCardinalityPostfix(classInfo_) + "& copy);" + xCrLf;
}

void CCppClassArrayGenerator::Head_generateDestructor()
{
	headCode_ += xTab + "virtual ~" + GetClassName() + GetCardinalityPostfix(classInfo_) + "();" + xCrLf + xCrLf;
}

void CCppClassArrayGenerator::Head_generateMethods()
{
	headCode_ += xTab + "// utils" + xCrLf;
	headCode_ += xTab + "//" + xCrLf;
	headCode_ += "public:" + xCrLf;
	headCode_ += xTab + "void operator=(const " + GetClassName() + GetCardinalityPostfix(classInfo_) + "& copy);" + xCrLf + xCrLf;
	headCode_ += xTab + "void SetDefaults();"  + xCrLf;
	headCode_ += xTab + "void Clear();"  + xCrLf;
	headCode_ += xTab + "virtual string GetXmlNodeName(){return \"" + classInfo_->GetName() + GetCardinalityPostfix(classInfo_) + "\";}" + xCrLf;
	headCode_ += xCrLf;
	headCode_ += xTab + "// access methods" + xCrLf;
	headCode_ += xTab + "//" + xCrLf;
	headCode_ += xTab + "int GetSize() const;" + xCrLf;
	headCode_ += xTab + "const " + GetClassName() + "& operator[] (int index) const;" + xCrLf;
	headCode_ += xTab + GetClassName() + "& operator[] (int index);" + xCrLf;
	headCode_ += xTab + "void Insert(int index, const " + GetClassName() + "& " + GetCamelCase(classInfo_->GetName()) + ");" + xCrLf;
	headCode_ += xTab + "void Delete(int index);" + xCrLf;
	headCode_ += xCrLf;

	headCode_ += "protected:" + xCrLf;
	headCode_ += xTab + "void Copy(const " + GetClassName() + GetCardinalityPostfix(classInfo_) + "& copy);"  + xCrLf;
	headCode_ += xCrLf;
}

void CCppClassArrayGenerator::Head_generateSerialization()
{
	headCode_ += xTab + "// serialization" + xCrLf;
	headCode_ += xTab + "//" + xCrLf;
	headCode_ += xTab + "public:" + xCrLf;
	headCode_ += xTab + "virtual bool BuildFromXml(CXMLDOMNode& node);" + xCrLf;
	headCode_ += xTab + "virtual bool AppendToDOMDocument(CXMLDOMDocument2& xmlDoc, CXMLDOMElement& parentElement, bool parentIsValid = true);" + xCrLf;
	headCode_ += xCrLf;
}

void CCppClassArrayGenerator::Head_generateMembers()
{
	headCode_ += xTab + "// inner members" + xCrLf;
	headCode_ += xTab + "//" + xCrLf;
	headCode_ += "protected:" + xCrLf;

	headCode_ += xTab + "vector<" + GetClassName() + "> " + GetCamelCase(classInfo_->GetName()+ GetCardinalityPostfix(classInfo_))  + "_;";
}

void CCppClassArrayGenerator::Head_generateClassEnd()
{
	headCode_ += xCrLf;
	headCode_ += "};" + xCrLf;
	headCode_ += xCrLf;
}

void CCppClassArrayGenerator::Head_generateIfDefEnd()
{
	headCode_ += "#endif //!defined(" + DefineOf(GetClassName() + GetCardinalityPostfix(classInfo_)) + ")" + xCrLf + xCrLf;
}

void CCppClassArrayGenerator::GenerateImplementation()
{
	string fileName;
	string pathName;

	Impl_generateHeader();
	Impl_generateIncludeSection();
	
	Impl_generateConstructor();
	Impl_generateDestructor();
	Impl_generateMethods();
	Impl_generateSerialization();

	pathName = BuildGeneratedSourcePath() + "src\\";
	fileName =  GetClassName() + GetCardinalityPostfix(classInfo_) +  ".cpp";
	SaveFile(pathName + fileName, implCode_);

	cppGeneratedFiles_[fileName] = pathName + fileName; 
}

void CCppClassArrayGenerator::Impl_generateHeader()
{
	implCode_ += "// " + GetClassName() + GetCardinalityPostfix(classInfo_) + ".cpp" + xCrLf;
	implCode_ += "// " + xCrLf; 
	implCode_ += GetGenerationTimeStamp();
}

void CCppClassArrayGenerator::Impl_generateIncludeSection()
{
	implCode_ += "#include \"stdafx.h\"" + xCrLf;
	implCode_ += "#include \"" + GetClassName() + GetCardinalityPostfix(classInfo_) + ".h\"" + xCrLf;

	ClassMemberMap::iterator it;
	ClassMemberMap* members;
	CXmlBaseElement* member;
	string name;

	members = classInfo_->GetMembers();
	for(it = members->begin(); it != members->end(); it++) {
		member = it->second;
		if (member->GetDataType() == enDataType_Class) {
			CXmlClassInfo* classInfo;

			classInfo = dynamic_cast<CXmlClassInfo*>(member);
			if (classInfo->GetDefinitionInheritance())
				classInfo = dynamic_cast<CXmlClassInfo*>(rootClassInfo_->GetMemberById(classInfo->GetInherithClassId()));

			name = classInfo->GetName();
			implCode_ += "#include \"" + GetClassName(name) + ".h\"" + xCrLf;
		}
	}

	implCode_ +=  xCrLf;
}

void CCppClassArrayGenerator::Impl_generateConstructor()
{
	string p;

	p = GetCardinalityPostfix(classInfo_);

	// default constructor
	//
	implCode_ += GetClassName() + p + "::" + GetClassName() + p + "()" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "SetDefaults();" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// copy constructor
	//
	implCode_ += GetClassName() + p + "::";
	implCode_ += GetClassName() + p;
	implCode_ += "(const " + GetClassName()  + p + "& ";
	implCode_ += GetCamelCase(classInfo_->GetName())  + p + ")" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "Copy(" + GetCamelCase(classInfo_->GetName()) + p + ");" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;
}

void CCppClassArrayGenerator::Impl_generateDestructor()
{
	string p;

	p = GetCardinalityPostfix(classInfo_);

	implCode_ += GetClassName() + p + "::~" + GetClassName() + p + "()" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "Clear();" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;
}

void CCppClassArrayGenerator::Impl_generateMethods()
{
	string p;

	p = GetCardinalityPostfix(classInfo_);
	// operator =
	//
	implCode_ += "void " +  GetClassName() + p + "::operator=(const " + GetClassName() + p + "& " + GetCamelCase(classInfo_->GetName()) + p + ")" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "Copy(" + GetCamelCase(classInfo_->GetName()) + p + ");" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// copy
	//
	string vectName;

	vectName = GetCamelCase(classInfo_->GetName()) + p;
	implCode_ += "void " + GetClassName() + p + "::Copy(const " + GetClassName() + p + "& " + GetCamelCase(classInfo_->GetName()) + p + ")"  + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "Clear();" + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + "for (int i = 0; i < " + vectName + ".GetSize(); i++)" + xCrLf;
	implCode_ += xTab + xTab + vectName + "_.push_back(" + vectName + "[i]);" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// SetDefaults
	//
	implCode_ += "void " + GetClassName() + p + "::SetDefaults()"  + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "Clear();" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// SetDefaults
	//
	implCode_ += "void " + GetClassName() + p +"::Clear()"  + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + vectName +"_.clear();" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// GetSize
	//
	implCode_ += "int " + GetClassName() + p +"::GetSize() const"  + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "return " + vectName + "_.size();" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// operator []
	//
	implCode_ += "const " + GetClassName() + "& " + GetClassName() + p +"::operator[] (int index) const" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "return " + vectName + "_[index];" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// operator []
	//
	implCode_ += GetClassName() + "& " + GetClassName() + p +"::operator[] (int index)" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "return " + vectName + "_[index];" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// Insert
	//
	implCode_ += "void " + GetClassName() + p +"::Insert(int index, const " + GetClassName() + "& " + GetCamelCase(classInfo_->GetName()) + ")" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + vectName + "_.insert(" + vectName + "_.begin() + index, " + GetCamelCase(classInfo_->GetName()) + ");" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	// Delete
	//
	implCode_ += "void " + GetClassName() + p +":: Delete(int index)" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + vectName + "_.erase(" + vectName + "_.begin() + index);" + xCrLf;
	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;
}

void CCppClassArrayGenerator::Impl_generateSerialization()
{
	ClassMemberMap::iterator it;
	string p;
	string arrayName;
	
	p = GetCardinalityPostfix(classInfo_);
	arrayName = GetCamelCase(classInfo_->GetName()) + p;

	//////////////////////////////////////////////////////////////////////
	// BuildFromXml
	//
	implCode_ += "bool " + GetClassName() + p + "::BuildFromXml(CXMLDOMNode& node)" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "USES_CONVERSION;" + xCrLf + xCrLf;

	implCode_ += xTab + "CXMLDOMNode childNode;" + xCrLf;
    implCode_ += xTab + "CXMLDOMNodeList children;" + xCrLf;
	implCode_ += xTab + "string nodeName;" + xCrLf;
	implCode_ += xTab + GetClassName() + " newElement;" + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + "SetDefaults();" + xCrLf;
	implCode_ += xCrLf;

	implCode_ += xTab + "children = node.GetChildNodes();" + xCrLf;
	
	implCode_ += xTab + "for (long i = 0; i < children.GetLength(); i++) {" + xCrLf;
	implCode_ += xTab + xTab + "childNode = children.GetItem(i);" + xCrLf;
	implCode_ += xTab + xTab + "nodeName = childNode.GetNodeName();" + xCrLf;
	implCode_ += xTab + xTab + "if (nodeName == newElement.GetXmlNodeName()) {" + xCrLf;
	implCode_ += xTab + xTab + xTab + "newElement.BuildFromXml(childNode);" + xCrLf;
	implCode_ += xTab + xTab + xTab + arrayName + "_.push_back(newElement);" + xCrLf;
	implCode_ += xTab + xTab + "}" + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + "}" + xCrLf + xCrLf;

	implCode_ += xTab + "return true;" + xCrLf;

	implCode_ += "}" + xCrLf;
	implCode_ += xCrLf;

	//////////////////////////////////////////////////////////////////////
	// AppendToDOMDocument
	//
	implCode_ += "bool " + GetClassName() + p + "::AppendToDOMDocument(CXMLDOMDocument2& xmlDoc, CXMLDOMElement& parentElement, bool parentIsValid)" + xCrLf;
	implCode_ += "{" + xCrLf;
	implCode_ += xTab + "CXMLDOMElement newElement;" + xCrLf;
    implCode_ += xTab + "CXMLDOMElement rectElement;" + xCrLf;
	implCode_ += xTab + "CXMLDOMElement member;" + xCrLf + xCrLf;
   
    implCode_ += xTab + "newElement = xmlDoc.CreateElement(GetXmlNodeName().c_str());" + xCrLf;
		
	implCode_ += xTab + "for (int i = 0; i < " + arrayName + "_.size(); i++) {" + xCrLf;
	implCode_ += xTab + xTab + arrayName + "_[i].AppendToDOMDocument(xmlDoc, newElement);" + xCrLf;
	implCode_ += xTab + "}" + xCrLf;
	implCode_ += xCrLf;
	implCode_ += xTab + "if (!parentIsValid)" + xCrLf;
    implCode_ += xTab + xTab + "xmlDoc.AppendChild(newElement);" + xCrLf;
	implCode_ += xTab + "else" + xCrLf;
    implCode_ += xTab + xTab + "parentElement.AppendChild(newElement);" + xCrLf + xCrLf;
	implCode_ += xTab + "return true;" + xCrLf;
	implCode_ += "}" + xCrLf + xCrLf;

}

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
Italy Italy
For all Stefano's latest code, binaries and tutorials visit www.codemachines.com

Comments and Discussions