1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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 ***************************************************************************/
24 #include "ClassHeaderGenerator.h"
25 #include "ClassSourceGenerator.h"
26 #include "Generator.h"
28 #include "StringUtilities.h"
35 Generator::Generator(const std::string
& name
) :
37 m_fieldname("FieldImpls"),
38 m_tablename("TableImpls"),
41 std::string headername
;
42 headername
.append(m_fieldname
);
43 headername
.append(".h");
44 m_headerfile
.open(headername
.c_str());
45 cout
<< "> " << headername
<< endl
;
47 std::string sourcename
;
48 sourcename
.append(m_fieldname
);
49 sourcename
.append(".cpp");
50 m_sourcefile
.open(sourcename
.c_str());
51 cout
<< "> " << sourcename
<< endl
;
53 std::string tiheadername
;
54 tiheadername
.append(m_tablename
);
55 tiheadername
.append(".h");
56 m_tiheaderfile
.open(tiheadername
.c_str());
57 cout
<< "> " << tiheadername
<< endl
;
59 std::string tisourcename
;
60 tisourcename
.append(m_tablename
);
61 tisourcename
.append(".cpp");
62 m_tisourcefile
.open(tisourcename
.c_str());
63 cout
<< "> " << tisourcename
<< endl
;
66 Generator::~Generator()
68 Assert(m_headerfile
.is_open());
69 Assert(m_sourcefile
.is_open());
70 Assert(m_tiheaderfile
.is_open());
71 Assert(m_tisourcefile
.is_open());
75 m_tiheaderfile
.close();
76 m_tisourcefile
.close();
79 void Generator::GenerateDAL(TableDefVector::const_iterator begin
, TableDefVector::const_iterator end
)
89 void Generator::AppendLicense(std::ofstream
& file
)
93 file
<< "/***************************************************************************" << endl
;
94 file
<< " * Copyright (C) 2008 by Sverre Rabbelier *" << endl
;
95 file
<< " * sverre@rabbelier.nl *" << endl
;
96 file
<< " * *" << endl
;
97 file
<< " * This program is free software; you can redistribute it and/or modify *" << endl
;
98 file
<< " * it under the terms of the GNU General Public License as published by *" << endl
;
99 file
<< " * the Free Software Foundation; either version 3 of the License, or *" << endl
;
100 file
<< " * (at your option) any later version. *" << endl
;
101 file
<< " * *" << endl
;
102 file
<< " * This program is distributed in the hope that it will be useful, *" << endl
;
103 file
<< " * but WITHOUT ANY WARRANTY; without even the implied warranty of *" << endl
;
104 file
<< " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *" << endl
;
105 file
<< " * GNU General Public License for more details. *" << endl
;
106 file
<< " * *" << endl
;
107 file
<< " * You should have received a copy of the GNU General Public License *" << endl
;
108 file
<< " * along with this program; if not, write to the *" << endl
;
109 file
<< " * Free Software Foundation, Inc., *" << endl
;
110 file
<< " * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *" << endl
;
111 file
<< " ***************************************************************************/" << endl
;
117 void Generator::AppendGeneratorNotice(std::ofstream
& file
)
121 file
<< "/* NOTE: This file was generated automatically. Do not edit. */" << endl
;
125 void Generator::AppendHeaderIncludes()
127 Assert(m_headerfile
);
129 m_headerfile
<< "#pragma once" << endl
;
130 m_headerfile
<< endl
;
131 m_headerfile
<< "/**" << endl
;
132 m_headerfile
<< " * @file " << m_fieldname
<< ".h" << endl
;
133 m_headerfile
<< " * This file contains the generated FieldImpl classes." << endl
;
134 m_headerfile
<< " *" << endl
;
135 m_headerfile
<< " * @see FieldImpl" << endl
;
136 m_headerfile
<< " */" << endl
;
137 m_headerfile
<< endl
;
139 m_headerfile
<< "#include \"Types.h\"" << endl
;
140 m_headerfile
<< "#include \"TableImpl.h\"" << endl
;
141 m_headerfile
<< endl
;
143 m_headerfile
<< "namespace " << m_name
<< endl
;
144 m_headerfile
<< "{" << endl
;
149 void Generator::AppendHeaderClass(TableDefPtr table
)
153 ClassHeaderGenerator
gen(table
, &m_headerfile
, m_name
);
159 void Generator::AppendHeaderFooter()
161 Assert(m_headerfile
);
163 m_headerfile
<< "} // end of namespace" << endl
;
164 m_headerfile
<< endl
;
169 void Generator::CreateHeader()
171 AppendLicense(m_headerfile
);
172 AppendGeneratorNotice(m_headerfile
);
173 AppendHeaderIncludes();
175 for(TableDefVector::const_iterator it
= m_begin
; it
!= m_end
; it
++)
176 AppendHeaderClass(*it
);
178 AppendHeaderFooter();
183 void Generator::AppendSourceIncludes()
185 Assert(m_sourcefile
);
187 m_sourcefile
<< "#include \"FieldImpls.h\"" << endl
;
188 m_sourcefile
<< "#include \"TableImpl.h\"" << endl
;
189 m_sourcefile
<< "#include \"TableImpls.h\"" << endl
;
190 m_sourcefile
<< "#include \"Tables.h\"" << endl
;
191 m_sourcefile
<< "#include \"FieldImpl.h\"" << endl
;
192 m_sourcefile
<< "#include \"KeyImpl.h\"" << endl
;
193 m_sourcefile
<< endl
;
195 m_sourcefile
<< "using namespace " << m_name
<< ";" << endl
;
196 m_sourcefile
<< endl
;
201 void Generator::AppendSourceClass(TableDefPtr table
)
205 ClassSourceGenerator
gen(table
, &m_sourcefile
);
211 void Generator::AppendSourceFooter()
213 Assert(m_sourcefile
);
215 m_headerfile
<< endl
;
220 void Generator::CreateSource()
222 AppendLicense(m_sourcefile
);
223 AppendGeneratorNotice(m_sourcefile
);
224 AppendSourceIncludes();
226 for(TableDefVector::const_iterator it
= m_begin
; it
!= m_end
; it
++)
227 AppendSourceClass(*it
);
229 AppendSourceFooter();
234 void Generator::CreateTI()
236 AppendLicense(m_tiheaderfile
);
237 AppendGeneratorNotice(m_tiheaderfile
);
238 AppendHeaderTableImpls();
240 AppendLicense(m_tisourcefile
);
241 AppendGeneratorNotice(m_tisourcefile
);
242 AppendSourceTableImpls();
245 void Generator::AppendHeaderTableImpls()
247 Assert(m_tiheaderfile
);
249 m_tiheaderfile
<< "#pragma once" << endl
;
250 m_tiheaderfile
<< endl
;
252 m_tiheaderfile
<< "/**" << endl
;
253 m_tiheaderfile
<< " * @file " << m_tablename
<< ".h" << endl
;
254 m_tiheaderfile
<< " * This file contains the generated " << m_name
<< "::TableImpls class." << endl
;
255 m_tiheaderfile
<< " *" << endl
;
256 m_tiheaderfile
<< " * @see " << m_name
<< "::TableImpls" << endl
;
257 m_tiheaderfile
<< " */" << endl
;
258 m_tiheaderfile
<< endl
;
260 m_tiheaderfile
<< "#include \"Types.h\"" << endl
;
261 m_tiheaderfile
<< "#include \"FieldImpls.h\"" << endl
;
262 m_tiheaderfile
<< endl
;
264 m_tiheaderfile
<< "namespace " << m_name
<< endl
;
265 m_tiheaderfile
<< "{" << endl
;
267 m_tiheaderfile
<< m_tabs
<< "/**" << endl
;
268 m_tiheaderfile
<< m_tabs
<< " * This is a generated class that contains all TableImpls." << endl
;
269 m_tiheaderfile
<< m_tabs
<< " *" << endl
;
270 m_tiheaderfile
<< m_tabs
<< " * Before using any tables, <code>Initialize</code> should be called." << endl
;
271 m_tiheaderfile
<< m_tabs
<< " *" << endl
;
272 m_tiheaderfile
<< m_tabs
<< " * @see Initialize" << endl
;
273 m_tiheaderfile
<< m_tabs
<< " */" << endl
;
274 m_tiheaderfile
<< m_tabs
<< "class TableImpls : public Singleton<db::TableImpls>" << endl
;
275 m_tiheaderfile
<< m_tabs
<< "{" << endl
;
276 m_tiheaderfile
<< m_tabs
<< "public:" << endl
;
278 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** Initialize all tables. */" << endl
;
279 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "void Initialize();" << endl
;
280 m_tiheaderfile
<< endl
;
282 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** Returns an iterator to the beginning of m_tables. */" << endl
;
283 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImplVector::const_iterator begin() const { return m_tables.begin(); }" << endl
;
284 m_tiheaderfile
<< endl
;
286 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** Returns an iterator to the end of m_tables. */" << endl
;
287 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImplVector::const_iterator end() const { return m_tables.end(); }" << endl
;
288 m_tiheaderfile
<< endl
;
290 m_tiheaderfile
<< endl
;
291 for(TableDefVector::const_iterator it
= m_begin
; it
!= m_end
; it
++)
293 TableDefPtr table
= *it
;
294 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "SmartPtr<" << table
->getName() << "> " << String::Get()->toupper(table
->getName()) << ";";
295 m_tiheaderfile
<< " /**< TableImpl for the " << table
->getName() << " class. */" << endl
;
297 m_tiheaderfile
<< endl
;
299 m_tiheaderfile
<< m_tabs
<< "private:" << endl
;
300 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** This is a singleton class, use <code>Get</code> to get an instance. */" << endl
;
301 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImpls();" << endl
;
302 m_tiheaderfile
<< endl
;
304 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** This is a singleton class, use <code>Free</code> to free the instance. */" << endl
;
305 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "~TableImpls() { }" << endl
;
306 m_tiheaderfile
<< endl
;
308 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** Hide the copy constructor. */" << endl
;
309 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImpls(const TableImpls& rhs);" << endl
;
310 m_tiheaderfile
<< endl
;
312 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "/** Hide the assignment operator. */" << endl
;
313 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImpls operator=(const TableImpls& rhs);" << endl
;
314 m_tiheaderfile
<< endl
;
316 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "friend class Singleton<TableImpls>;" << endl
;
317 m_tiheaderfile
<< endl
;
318 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "TableImplVector m_tables; /**< All generated tables. */" << endl
;
319 m_tiheaderfile
<< m_tabs
<< m_tabs
<< "bool m_initialized; /**< Whether this manager is initialized yet. */" << endl
;
320 m_tiheaderfile
<< m_tabs
<< "};" << endl
;
321 m_tiheaderfile
<< "} // end of namespace" << endl
;
322 m_tiheaderfile
<< endl
;
325 void Generator::AppendSourceTableImpls()
327 Assert(m_tisourcefile
);
329 //m_tisourcefile << "#ifdef _WIN32" << endl;
330 //m_tisourcefile << "#pragma warning (disable:4244)" << endl;
331 //m_tisourcefile << "#pragma warning (disable:4267)" << endl;
332 //m_tisourcefile << "#endif" << endl;
333 //m_tisourcefile << endl;
335 m_tisourcefile
<< "#include \"TableImpl.h\"" << endl
;
336 m_tisourcefile
<< "#include \"TableImpls.h\"" << endl
;
337 m_tisourcefile
<< "#include \"Tables.h\"" << endl
;
338 m_tisourcefile
<< "#include \"FieldImpl.h\"" << endl
;
339 m_tisourcefile
<< "#include \"FieldImpls.h\"" << endl
;
340 m_tisourcefile
<< "#include \"KeyImpl.h\"" << endl
;
341 m_tisourcefile
<< endl
;
343 m_tisourcefile
<< "using namespace " << m_name
<< ";" << endl
;
344 m_tisourcefile
<< endl
;
346 m_tisourcefile
<< "TableImpls::TableImpls() :" << endl
;
347 for(TableDefVector::const_iterator it
= m_begin
; it
!= m_end
; it
++)
349 TableDefPtr table
= *it
;
353 m_tisourcefile
<< "," << endl
;
355 m_tisourcefile
<< String::Get()->toupper(table
->getName()) << "( SmartPtr<db::" << table
->getName() <<">(new db::" << table
->getName() << "()) )";
358 m_tisourcefile
<< endl
;
359 m_tisourcefile
<< "{" << endl
;
360 for(TableDefVector::const_iterator it
= m_begin
; it
!= m_end
; it
++)
362 TableDefPtr table
= *it
;
365 m_tisourcefile
<< m_tabs
<< "m_tables.push_back(" << String::Get()->toupper(table
->getName()) << ");" << endl
;
367 m_tisourcefile
<< "}" << endl
;
368 m_tisourcefile
<< endl
;
370 m_tisourcefile
<< "void TableImpls::Initialize()" << endl
;
371 m_tisourcefile
<< "{" << endl
;
372 m_tisourcefile
<< m_tabs
<< "Assert(!m_initialized);" << endl
;
373 m_tisourcefile
<< endl
;
375 m_tisourcefile
<< m_tabs
<< "m_initialized = true;" << endl
;
376 m_tisourcefile
<< endl
;
378 m_tisourcefile
<< m_tabs
<< "for(TableImplVector::const_iterator it = m_tables.begin(); it != m_tables.end(); it++)" << endl
;
379 m_tisourcefile
<< m_tabs
<< "{" << endl
;
380 m_tisourcefile
<< m_tabs
<< m_tabs
<< "TableImplPtr table = *it;" << endl
;
381 m_tisourcefile
<< m_tabs
<< m_tabs
<< "Assert(table);" << endl
;
382 m_tisourcefile
<< endl
;
383 m_tisourcefile
<< m_tabs
<< m_tabs
<< "table->Initialize();" << endl
;
384 m_tisourcefile
<< m_tabs
<< "}" << endl
;
385 m_tisourcefile
<< endl
;
387 m_tisourcefile
<< "}" << endl
;
388 m_tisourcefile
<< endl
;