Added GPLv3 headers all over the place.
[fail.git] / src / core / failit / module.cpp
blobffc5be430d0a9383253fb9191a36fa5c762d27dd
1 /*
2 Fail game engine
3 Copyright 2007 Antoine Chavasse <a.chavasse@gmail.com>
5 This file is part of Fail.
7 Fail is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3
9 as published by the Free Software Foundation.
11 Fail is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sstream>
20 #include <fstream>
21 #include <vector>
22 #include "module.h"
23 #include "crc32.h"
25 using namespace fail;
26 using namespace fail::failit;
27 using namespace std;
29 namespace
31 void GenerateModuleHeaderName( std::ostream& Out_, const idlast::Namespace* pNamespace_ )
33 vector< string > NSNames;
34 const idlast::Namespace* pCurrentNS = pNamespace_;
35 while( pCurrentNS->getParent() )
37 NSNames.push_back( pCurrentNS->getName() );
38 pCurrentNS = pCurrentNS->getParent();
41 Out_ << "module_";
42 vector< string >::const_reverse_iterator it;
43 for( it = NSNames.rbegin(); it != NSNames.rend(); ++it )
45 Out_ << *it;
47 if( ( it - NSNames.rbegin() + vector< string >::size_type( 1 ) ) < NSNames.size() )
48 Out_ << '-';
51 Out_ << ".h";
55 void fail::failit::GenerateModuleFiles( const idlast::Namespace* pNamespace_ )
57 // Build an array of namespace names, from the root namespace to the one we're interested in.
59 std::stringstream filename;
60 GenerateModuleHeaderName( filename, pNamespace_ );
61 ofstream file( filename.str().c_str() );
63 vector< string > NSNames;
64 const idlast::Namespace* pCurrentNS = pNamespace_;
65 while( pCurrentNS->getParent() )
67 NSNames.push_back( pCurrentNS->getName() );
68 pCurrentNS = pCurrentNS->getParent();
71 stringstream defname;
72 vector< string >::const_reverse_iterator it;
73 for( it = NSNames.rbegin(); it != NSNames.rend(); ++it )
74 defname << '_' << *it;
76 file << "//\n"
77 "// Automatically generated by FailIT (Fail IDL Translator).\n"
78 "//\n"
79 "#ifndef MODULE" << defname.str() << "_H_\n"
80 "#define MODULE" << defname.str() << "_H_\n\n"
81 "#include \"core/core.h\"\n";
83 const idlast::NamespaceDependencySet& deps = pNamespace_->getDependencies();
84 idlast::NamespaceDependencySet::const_iterator depit;
85 for( depit = deps.begin(); depit != deps.end(); ++depit )
87 file << "#include \"";
88 GenerateModuleHeaderName( file, *depit );
89 file << "\"\n";
92 file << '\n';
94 for( it = NSNames.rbegin(); it != NSNames.rend(); ++it )
96 file << "namespace " << *it;
98 if( ( it - NSNames.rbegin() + vector< string >::size_type( 1 ) ) < NSNames.size() )
99 file << " { ";
102 file << "\n"
103 "{\n"
104 "\tstruct fail_tag {};\n";
106 for( unsigned int i = 0; i < NSNames.size(); ++i )
107 file << '}';
109 file << "\n\n";
111 const idlast::Dictionary< idlast::Class >& Classes = pNamespace_->getClasses();
112 idlast::Dictionary< idlast::Class >::const_iterator clit;
114 for( clit = Classes.begin(); clit != Classes.end(); ++clit )
116 file << "#include \"" << clit->second->getName() << ".h\"\n";
117 file << "#include \"traits_" << clit->second->getName() << ".h\"\n";
120 const idlast::Dictionary< idlast::Class >& Structs = pNamespace_->getStructs();
121 idlast::Dictionary< idlast::Class >::const_iterator stit;
122 for( stit = Structs.begin(); stit != Structs.end(); ++stit )
124 file << "#include \"" << stit->second->getName() << ".h\"\n";
125 file << "#include \"traits_" << stit->second->getName() << ".h\"\n";
128 file << "\n"
129 "namespace fail\n"
130 "{\n"
131 "\ttemplate<> struct module_traits< ";
133 stringstream FullNSName;
134 for( it = NSNames.rbegin(); it != NSNames.rend(); ++it )
136 FullNSName << *it;
138 if( ( it - NSNames.rbegin() + vector< string >::size_type( 1 ) ) < NSNames.size() )
139 FullNSName << "::";
142 crc32 digest;
143 digest.write( FullNSName.str().c_str(), FullNSName.str().size() );
145 file << FullNSName.str() << "::fail_tag >\n"
146 "\t{\n"
147 "\t\tstatic uint32_t Digest() { return 0x" << std::hex << digest.result() << "; }\n"
148 "\t\tstatic const char* Name() { return \"" << NSNames[0] << "\"; }\n"
149 "\t\tstatic const char* FullName() { return \"" << FullNSName.str() << "\"; }\n\n"
150 "\t\ttemplate< class V > static void VisitQualifiedName( V& Visitor_ )\n"
151 "\t\t{\n";
153 for( it = NSNames.rbegin(); it != NSNames.rend(); ++it )
154 file << "\t\t\tVisitor_.QualifiedNameComponent( \"" << *it << "\" );\n";
156 file << "\t\t}\n"
157 "\n"
158 "\t\ttemplate< class V > static void VisitClasses( V& Visitor_ )\n"
159 "\t\t{\n";
161 for( clit = Classes.begin(); clit != Classes.end(); ++clit )
163 file << "\t\t\tVisitor_.template Class< "
164 << FullNSName.str() << "::" << clit->second->getName() << " >();\n";
167 for( stit = Structs.begin(); stit != Structs.end(); ++stit )
169 file << "\t\t\tVisitor_.template Struct< "
170 << FullNSName.str() << "::" << stit->second->getName() << " >();\n";
173 file << "\t\t}\n"
174 "\t};\n"
175 "}\n"
176 "\n"
177 "#endif\n";