2013-10-26 09:41:45 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-10-26 09:41:45 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "classcompiler.hpp"
|
2013-10-26 09:41:45 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
2013-12-13 12:54:14 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ClassCompiler::ClassCompiler(const std::string& path, std::istream *input)
|
|
|
|
: m_Path(path), m_Input(input)
|
|
|
|
{
|
|
|
|
InitializeScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassCompiler::~ClassCompiler(void)
|
|
|
|
{
|
|
|
|
DestroyScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ClassCompiler::GetPath(void) const
|
|
|
|
{
|
|
|
|
return m_Path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ClassCompiler::GetScanner(void)
|
|
|
|
{
|
|
|
|
return m_Scanner;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ClassCompiler::ReadInput(char *buffer, size_t max_size)
|
|
|
|
{
|
|
|
|
m_Input->read(buffer, max_size);
|
|
|
|
return static_cast<size_t>(m_Input->gcount());
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleInclude(const std::string& path, const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "#include \"" << path << "\"" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleAngleInclude(const std::string& path, const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "#include <" << path << ">" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleNamespaceBegin(const std::string& name, const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "namespace " << name << std::endl
|
|
|
|
<< "{" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleNamespaceEnd(const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "}" << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleCode(const std::string& code, const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::cout << code << std::endl;
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
unsigned long ClassCompiler::SDBM(const std::string& str, size_t len = std::string::npos)
|
|
|
|
{
|
|
|
|
unsigned long hash = 0;
|
|
|
|
size_t current = 0;
|
|
|
|
|
|
|
|
std::string::const_iterator it;
|
|
|
|
|
|
|
|
for (it = str.begin(); it != str.end(); it++) {
|
|
|
|
if (current >= len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char c = *it;
|
|
|
|
|
|
|
|
hash = c + (hash << 6) + (hash << 16) - hash;
|
|
|
|
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
|
2013-10-26 09:41:45 +02:00
|
|
|
{
|
|
|
|
std::vector<Field>::const_iterator it;
|
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* forward declaration */
|
2013-10-26 09:41:45 +02:00
|
|
|
if (klass.Name.find_first_of(':') == std::string::npos)
|
|
|
|
std::cout << "class " << klass.Name << ";" << std::endl << std::endl;
|
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
/* TypeImpl */
|
2013-10-26 09:41:45 +02:00
|
|
|
std::cout << "template<>" << std::endl
|
2013-11-04 23:14:34 +01:00
|
|
|
<< "class TypeImpl<" << klass.Name << ">"
|
2014-04-05 22:17:20 +02:00
|
|
|
<< " : public Type";
|
|
|
|
|
|
|
|
if (!klass.TypeBase.empty())
|
|
|
|
std::cout << ", public " + klass.TypeBase;
|
|
|
|
|
|
|
|
std::cout << std::endl
|
|
|
|
<< " {" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "public:" << std::endl;
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
/* GetName */
|
|
|
|
std::cout << "\t" << "virtual String GetName(void) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return \"" << klass.Name << "\";" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
/* GetAttributes */
|
|
|
|
std::cout << "\t" << "virtual int GetAttributes(void) const" << std::endl
|
2013-11-08 11:17:46 +01:00
|
|
|
<< "\t" << "{" << std::endl
|
2013-12-18 10:18:57 +01:00
|
|
|
<< "\t\t" << "return " << klass.Attributes << ";" << std::endl
|
2013-11-08 11:17:46 +01:00
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
2013-11-05 08:48:45 +01:00
|
|
|
/* GetBaseType */
|
2013-11-08 11:17:46 +01:00
|
|
|
std::cout << "\t" << "virtual const Type *GetBaseType(void) const" << std::endl
|
2013-11-05 08:48:45 +01:00
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t\t" << "return ";
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
2013-11-08 21:37:21 +01:00
|
|
|
std::cout << "Type::GetByName(\"" << klass.Parent << "\")";
|
2013-11-05 08:48:45 +01:00
|
|
|
else
|
|
|
|
std::cout << "NULL";
|
|
|
|
|
|
|
|
std::cout << ";" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* GetFieldId */
|
|
|
|
std::cout << "\t" << "virtual int GetFieldId(const String& name) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return StaticGetFieldId(name);" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
|
|
|
/* StaticGetFieldId */
|
|
|
|
std::cout << "\t" << "static int StaticGetFieldId(const String& name)" << std::endl
|
2013-12-17 07:33:39 +01:00
|
|
|
<< "\t" << "{" << std::endl;
|
2013-11-04 19:12:34 +01:00
|
|
|
|
2013-12-17 07:33:39 +01:00
|
|
|
if (!klass.Fields.empty()) {
|
|
|
|
std::cout << "\t\t" << "int offset = ";
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
|
|
|
std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
|
|
|
|
else
|
|
|
|
std::cout << "0";
|
2013-11-04 19:12:34 +01:00
|
|
|
|
2013-12-17 07:33:39 +01:00
|
|
|
std::cout << ";" << std::endl << std::endl;
|
|
|
|
}
|
2013-11-04 19:12:34 +01:00
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
std::map<int, std::vector<std::pair<int, std::string> > > jumptable;
|
|
|
|
|
|
|
|
int hlen = 0, collisions = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
hlen++;
|
|
|
|
jumptable.clear();
|
|
|
|
collisions = 0;
|
|
|
|
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
int hash = static_cast<int>(SDBM(it->Name, hlen));
|
|
|
|
jumptable[hash].push_back(std::make_pair(num, it->Name));
|
|
|
|
num++;
|
|
|
|
|
|
|
|
if (jumptable[hash].size() > 1)
|
|
|
|
collisions++;
|
|
|
|
}
|
|
|
|
} while (collisions >= 5 && hlen < 8);
|
|
|
|
|
|
|
|
if (!klass.Fields.empty()) {
|
|
|
|
std::cout << "\t\tswitch (static_cast<int>(Utility::SDBM(name, " << hlen << "))) {" << std::endl;
|
|
|
|
|
|
|
|
std::map<int, std::vector<std::pair<int, std::string> > >::const_iterator itj;
|
|
|
|
|
|
|
|
for (itj = jumptable.begin(); itj != jumptable.end(); itj++) {
|
|
|
|
std::cout << "\t\t\tcase " << itj->first << ":" << std::endl;
|
|
|
|
|
|
|
|
std::vector<std::pair<int, std::string> >::const_iterator itf;
|
|
|
|
|
|
|
|
for (itf = itj->second.begin(); itf != itj->second.end(); itf++) {
|
|
|
|
std::cout << "\t\t\t\t" << "if (name == \"" << itf->second << "\")" << std::endl
|
|
|
|
<< "\t\t\t\t\t" << "return offset + " << itf->first << ";" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl
|
|
|
|
<< "\t\t\t\tbreak;" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\t\t}" << std::endl;
|
2013-11-04 19:12:34 +01:00
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << std::endl
|
|
|
|
<< "\t\t" << "return ";
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
if (!klass.Parent.empty())
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldId(name)";
|
2013-11-04 19:12:34 +01:00
|
|
|
else
|
|
|
|
std::cout << "-1";
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << ";" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* GetFieldInfo */
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "\t" << "virtual Field GetFieldInfo(int id) const" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return StaticGetFieldInfo(id);" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* StaticGetFieldInfo */
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "\t" << "static Field StaticGetFieldInfo(int id)" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t" << "{" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
if (!klass.Parent.empty())
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "\t\t" << "int real_id = id - " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount();" << std::endl
|
|
|
|
<< "\t\t" << "if (real_id < 0) { return " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldInfo(id); }" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-08 21:45:27 +01:00
|
|
|
if (klass.Fields.size() > 0) {
|
|
|
|
std::cout << "\t\t" << "switch (";
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-08 21:45:27 +01:00
|
|
|
if (!klass.Parent.empty())
|
|
|
|
std::cout << "real_id";
|
|
|
|
else
|
|
|
|
std::cout << "id";
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-08 21:45:27 +01:00
|
|
|
std::cout << ") {" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
size_t num = 0;
|
2013-11-08 21:45:27 +01:00
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
|
|
|
|
<< "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\t\t\t" << "default:" << std::endl
|
|
|
|
<< "\t\t";
|
2013-11-04 19:12:34 +01:00
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-08 21:45:27 +01:00
|
|
|
std::cout << "\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl;
|
|
|
|
|
|
|
|
if (klass.Fields.size() > 0)
|
|
|
|
std::cout << "\t\t" << "}" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* GetFieldCount */
|
|
|
|
std::cout << "\t" << "virtual int GetFieldCount(void) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return StaticGetFieldCount();" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
/* StaticGetFieldCount */
|
|
|
|
std::cout << "\t" << "static int StaticGetFieldCount(void)" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return " << klass.Fields.size();
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
if (!klass.Parent.empty())
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << " + " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << ";" << std::endl
|
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << "};" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
/* ObjectImpl */
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << "template<>" << std::endl
|
2013-11-04 23:14:34 +01:00
|
|
|
<< "class ObjectImpl<" << klass.Name << ">"
|
|
|
|
<< " : public " << (klass.Parent.empty() ? "Object" : klass.Parent) << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "{" << std::endl
|
|
|
|
<< "public:" << std::endl
|
2013-11-04 23:14:34 +01:00
|
|
|
<< "\t" << "DECLARE_PTR_TYPEDEFS(ObjectImpl<" << klass.Name << ">);" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 23:14:34 +01:00
|
|
|
/* GetReflectionType */
|
|
|
|
std::cout << "\t" << "virtual const Type *GetReflectionType(void) const" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t" << "{" << std::endl
|
2013-11-08 21:37:21 +01:00
|
|
|
<< "\t\t" << "return Type::GetByName(\"" << klass.Name << "\");" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
|
|
|
if (!klass.Fields.empty()) {
|
|
|
|
/* constructor */
|
2013-10-26 09:41:45 +02:00
|
|
|
std::cout << "public:" << std::endl
|
2013-11-04 23:14:34 +01:00
|
|
|
<< "\t" << "ObjectImpl<" << klass.Name << ">(void)" << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t" << "{" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::cout << "\t\t" << "Set" << it->GetFriendlyName() << "(" << "GetDefault" << it->GetFriendlyName() << "());" << std::endl;
|
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-11-04 19:12:34 +01:00
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
/* SetField */
|
|
|
|
std::cout << "protected:" << std::endl
|
|
|
|
<< "\t" << "virtual void SetField(int id, const Value& value)" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "\t\t" << "int real_id = id - TypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl
|
2013-11-04 19:12:34 +01:00
|
|
|
<< "\t\t" << "if (real_id < 0) { " << klass.Parent << "::SetField(id, value); return; }" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
std::cout << "\t\t" << "switch (";
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
|
|
|
std::cout << "real_id";
|
|
|
|
else
|
|
|
|
std::cout << "id";
|
|
|
|
|
|
|
|
std::cout << ") {" << std::endl;
|
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
size_t num = 0;
|
2013-10-26 09:41:45 +02:00
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
|
|
|
|
<< "\t\t\t\t" << "Set" << it->GetFriendlyName() << "(";
|
|
|
|
|
|
|
|
if (it->Attributes & FAEnum)
|
|
|
|
std::cout << "static_cast<" << it->Type << ">(static_cast<int>(";
|
|
|
|
|
|
|
|
std::cout << "value";
|
|
|
|
|
|
|
|
if (it->Attributes & FAEnum)
|
|
|
|
std::cout << "))";
|
|
|
|
|
|
|
|
std::cout << ");" << std::endl
|
|
|
|
<< "\t\t\t\t" << "break;" << std::endl;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\t\t\t" << "default:" << std::endl
|
|
|
|
<< "\t\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
|
|
|
|
<< "\t\t" << "}" << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
|
|
|
/* GetField */
|
|
|
|
std::cout << "protected:" << std::endl
|
|
|
|
<< "\t" << "virtual Value GetField(int id) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
2013-11-04 23:14:34 +01:00
|
|
|
std::cout << "\t\t" << "int real_id = id - TypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl
|
2013-10-26 09:41:45 +02:00
|
|
|
<< "\t\t" << "if (real_id < 0) { return " << klass.Parent << "::GetField(id); }" << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t\t" << "switch (";
|
|
|
|
|
|
|
|
if (!klass.Parent.empty())
|
|
|
|
std::cout << "real_id";
|
|
|
|
else
|
|
|
|
std::cout << "id";
|
|
|
|
|
|
|
|
std::cout << ") {" << std::endl;
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::cout << "\t\t\t" << "case " << num << ":" << std::endl
|
|
|
|
<< "\t\t\t\t" << "return Get" << it->GetFriendlyName() << "();" << std::endl;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\t\t\t" << "default:" << std::endl
|
|
|
|
<< "\t\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
|
|
|
|
<< "\t\t" << "}" << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
|
|
|
|
|
|
|
/* getters */
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::string prot;
|
|
|
|
|
|
|
|
if (it->Attributes & FAGetProtected)
|
|
|
|
prot = "protected";
|
|
|
|
else
|
|
|
|
prot = "public";
|
|
|
|
|
|
|
|
std::cout << prot << ":" << std::endl
|
|
|
|
<< "\t" << it->Type << " Get" << it->GetFriendlyName() << "(void) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
if (it->GetAccessor.empty())
|
|
|
|
std::cout << "\t\t" << "return m_" << it->GetFriendlyName() << ";" << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << it->GetAccessor << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setters */
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::string prot;
|
|
|
|
|
|
|
|
if (it->Attributes & FASetProtected)
|
|
|
|
prot = "protected";
|
|
|
|
else if (it->Attributes & FAConfig)
|
|
|
|
prot = "private";
|
|
|
|
else
|
|
|
|
prot = "public";
|
|
|
|
|
|
|
|
std::cout << prot << ":" << std::endl
|
2014-03-20 13:02:02 +01:00
|
|
|
<< "\t" << "void Set" << it->GetFriendlyName() << "(";
|
|
|
|
|
|
|
|
if (it->Type == "bool" || it->Type == "double" || it->Type == "int")
|
|
|
|
std::cout << it->Type;
|
|
|
|
else
|
|
|
|
std::cout << "const " << it->Type << "&";
|
|
|
|
|
|
|
|
std::cout << " value)" << std::endl
|
2013-10-26 09:41:45 +02:00
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
if (it->SetAccessor.empty())
|
|
|
|
std::cout << "\t\t" << "m_" << it->GetFriendlyName() << " = value;" << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << it->SetAccessor << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "}" << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default */
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::string prot;
|
|
|
|
|
|
|
|
std::cout << "private:" << std::endl
|
|
|
|
<< "\t" << it->Type << " GetDefault" << it->GetFriendlyName() << "(void) const" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl;
|
|
|
|
|
|
|
|
if (it->DefaultAccessor.empty())
|
2013-11-07 11:11:44 +01:00
|
|
|
std::cout << "\t\t" << "return " << it->Type << "();" << std::endl;
|
2013-10-26 09:41:45 +02:00
|
|
|
else
|
|
|
|
std::cout << it->DefaultAccessor << std::endl;
|
|
|
|
|
|
|
|
std::cout << "\t" << "}" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instance variables */
|
|
|
|
std::cout << "private:" << std::endl;
|
|
|
|
|
|
|
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
|
|
|
std::cout << "\t" << it->Type << " m_" << it->GetFriendlyName() << ";" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "};" << std::endl << std::endl;
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
/* FactoryHelper */
|
2013-12-18 10:18:57 +01:00
|
|
|
if (klass.Attributes & TAAbstract) {
|
2013-11-08 11:17:46 +01:00
|
|
|
std::cout << "template<>" << std::endl
|
|
|
|
<< "struct FactoryHelper<" << klass.Name << ">" << std::endl
|
|
|
|
<< "{" << std::endl
|
|
|
|
<< "\t" << "Type::Factory GetFactory(void)" << std::endl
|
|
|
|
<< "\t" << "{" << std::endl
|
|
|
|
<< "\t\t" << "return Type::Factory();"
|
|
|
|
<< "\t" << "}"
|
|
|
|
<< "};" << std::endl << std::endl;
|
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClassCompiler::CompileFile(const std::string& path)
|
|
|
|
{
|
|
|
|
std::ifstream stream;
|
|
|
|
stream.open(path.c_str(), std::ifstream::in);
|
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
throw std::invalid_argument("Could not open config file: " + path);
|
|
|
|
|
|
|
|
return CompileStream(path, &stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
|
|
|
|
{
|
|
|
|
stream->exceptions(std::istream::badbit);
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
std::cout << "#include \"base/object.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/type.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/debug.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/value.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/array.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/dictionary.hpp\"" << std::endl
|
|
|
|
<< "#include \"base/utility.hpp\"" << std::endl << std::endl
|
2013-10-26 09:41:45 +02:00
|
|
|
<< "#ifdef _MSC_VER" << std::endl
|
|
|
|
<< "#pragma warning( push )" << std::endl
|
|
|
|
<< "#pragma warning( disable : 4244 )" << std::endl
|
|
|
|
<< "#pragma warning( disable : 4800 )" << std::endl
|
|
|
|
<< "#endif /* _MSC_VER */" << std::endl << std::endl;
|
|
|
|
|
|
|
|
ClassCompiler ctx(path, stream);
|
|
|
|
ctx.Compile();
|
|
|
|
|
|
|
|
std::cout << "#ifdef _MSC_VER" << std::endl
|
|
|
|
<< "#pragma warning ( pop )" << std::endl
|
|
|
|
<< "#endif /* _MSC_VER */" << std::endl;
|
|
|
|
}
|