Well don't that own? Sorted all the #include's.
[UnsignedByte.git] / src / Generator / ClassSourceGenerator.cpp
blob01582c4ea93921de5c0829b4d3fe6663375287cd
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "Assert.h"
22 #include "ClassSourceGenerator.h"
23 #include "Global.h"
24 #include "Tables.h"
25 #include "TableDef.h"
26 #include "FieldDef.h"
27 #include "KeyDef.h"
28 #include "StringUtilities.h"
30 using std::endl;
32 ClassSourceGenerator::ClassSourceGenerator(TableDefPtr table, std::ofstream* file) :
33 m_tabs("\t"),
34 m_table(table),
35 m_file(file)
37 Assert(table);
39 m_name = m_table->getName();
42 ClassSourceGenerator::~ClassSourceGenerator()
47 void ClassSourceGenerator::GenerateClass()
49 Assert(m_table->primarykeysize() > 0);
51 Assert(m_file);
53 (*m_file) << "void " << m_name << "::Initialize()" << endl;
54 (*m_file) << "{" << endl;
55 for(FieldDefVector::const_iterator it = m_table->begin(); it != m_table->end(); it++)
57 FieldDefPtr field = *it;
59 (*m_file) << m_tabs << String::Get()->toupper((*it)->getName()) << " = ";
61 // C++ -does- have reflection, it does now at least.
62 if(field->isKey())
64 KeyDef* keydef = (KeyDef*)field.get();
65 // KeyImpl(TableImplPtr <these ones>, TableImplPtr <these ones>, ...);
66 (*m_file) << "KeyImplPtr(new KeyImpl";
67 (*m_file) << "(TableImpls::Get()->" << String::Get()->toupper(m_table->getName());
68 (*m_file) << ", TableImpls::Get()->" << String::Get()->toupper(keydef->getForeignTable()->getName());
70 else
72 // FieldImpl(TableImplPtr <this one>, ...);
73 (*m_file) << "FieldImplPtr(new FieldImpl";
74 (*m_file) << "(TableImpls::Get()->" << String::Get()->toupper(m_table->getName());
77 // ...Impl(TableImplPtr table, ..., cstring <this one>, ...);
78 (*m_file) << ", \"" << (*it)->getName() << "\"";
80 if(field->isKey())
82 if(field->isPrimaryKey())
84 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool <primary>, ...);
85 (*m_file) << ", true";
87 if(m_table->primarykeysize() == 1)
89 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
90 (*m_file) << ", true";
92 else
94 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
95 (*m_file) << ", false";
98 else
100 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool <primary>, ...);
101 (*m_file) << ", false";
103 if(field->isLookup())
105 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
106 (*m_file) << ", true";
108 else
110 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
111 (*m_file) << ", false";
115 else
117 if(field->isText())
119 // FieldImpl(TableImplPtr table, cstring name, bool <text>, ...);
120 (*m_file) << ", true";
122 else
124 // FieldImpl(TableImplPtr table, cstring name, bool <text>, ...);
125 (*m_file) << ", false";
128 std::string defaultvalue = field->getDefaultValue();
129 if(defaultvalue.size() > 0)
131 // FieldImpl(TableImplPtr table, cstring name, bool text, const std::string& <defaultvalue>);
132 (*m_file) << ", std::string(\"" << defaultvalue << "\")";
134 else
136 if(field->isLookup())
138 // FieldImpl(TableImplPtr table, cstring name, bool text, bool <lookup>);
139 (*m_file) << ", true";
141 else
143 // FieldImpl(TableImplPtr table, cstring name, bool text, bool <lookup>);
144 (*m_file) << ", false";
149 (*m_file) << "));";
150 (*m_file) << endl;
152 (*m_file) << endl;
154 for(FieldDefVector::const_iterator it = m_table->begin(); it != m_table->end(); it++)
156 FieldDefPtr field = *it;
157 Assert(field);
159 (*m_file) << m_tabs << "m_fields.push_back(" << String::Get()->toupper((*it)->getName()) << ");" << endl;
162 (*m_file) << endl;
163 (*m_file) << m_tabs << "updateFieldCount();" << endl;
165 (*m_file) << "}" << endl;
166 (*m_file) << endl;