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/>.
26 using namespace fail::failit
;
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();
42 vector
< string
>::const_reverse_iterator it
;
43 for( it
= NSNames
.rbegin(); it
!= NSNames
.rend(); ++it
)
47 if( ( it
- NSNames
.rbegin() + vector
< string
>::size_type( 1 ) ) < NSNames
.size() )
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();
72 vector
< string
>::const_reverse_iterator it
;
73 for( it
= NSNames
.rbegin(); it
!= NSNames
.rend(); ++it
)
74 defname
<< '_' << *it
;
77 "// Automatically generated by FailIT (Fail IDL Translator).\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
);
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() )
104 "\tstruct fail_tag {};\n";
106 for( unsigned int i
= 0; i
< NSNames
.size(); ++i
)
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";
131 "\ttemplate<> struct module_traits< ";
133 stringstream FullNSName
;
134 for( it
= NSNames
.rbegin(); it
!= NSNames
.rend(); ++it
)
138 if( ( it
- NSNames
.rbegin() + vector
< string
>::size_type( 1 ) ) < NSNames
.size() )
143 digest
.write( FullNSName
.str().c_str(), FullNSName
.str().size() );
145 file
<< FullNSName
.str() << "::fail_tag >\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"
153 for( it
= NSNames
.rbegin(); it
!= NSNames
.rend(); ++it
)
154 file
<< "\t\t\tVisitor_.QualifiedNameComponent( \"" << *it
<< "\" );\n";
158 "\t\ttemplate< class V > static void VisitClasses( V& Visitor_ )\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";