What boost things you mean? The Preprocessor Macros?
They generate 8 versions of the same function starting with 1 argument (only the key for the map) and going up to 8 additional template and method parameters. If you run the compiler just in preprocessing mode you will basically get this (but each method will be on a single line, so i had to format it a little):
Code: Select all
T* GenerateObject(K key ) const
{
T* retval = 0; typename std::map<K,G>::const_iterator it = m_generators.find(key);
if (it != m_generators.end())
retval = it->second();
return retval;
}
template < class A1 >
T* GenerateObject(K key , A1 arg1) const
{
T* retval = 0;
typename std::map<K,G>::const_iterator it = m_generators.find(key);
if (it != m_generators.end())
retval = it->second( arg1);
return retval;
}
template < class A1 , class A2 >
T* GenerateObject(K key , A1 arg1 , A2 arg2) const
{
T* retval = 0;
typename std::map<K,G>::const_iterator it = m_generators.find(key);
if (it != m_generators.end())
retval = it->second( arg1 , arg2);
return retval;
}
template < class A1 , class A2 , class A3 >
T* GenerateObject(K key , A1 arg1 , A2 arg2 , A3 arg3) const
{
T* retval = 0;
typename std::map<K,G>::const_iterator it = m_generators.find(key);
if (it != m_generators.end())
retval = it->second( arg1 , arg2 , arg3);
return retval;
}
template < class A1 , class A2 , class A3 , class A4 >
T* GenerateObject(K key , A1 arg1 , A2 arg2 , A3 arg3 , A4 arg4) const
{
T* retval = 0;
typename std::map<K,G>::const_iterator it = m_generators.find(key);
if (it != m_generators.end())
retval = it->second( arg1 , arg2 , arg3 , arg4);
return retval; }
//and so on until GENERATE_MAX_PARAMS is reached
If you really want to understand how it works look at the Boost Preprocessor library. This stuff is called Preprocessor Metaprogramming because it generates source-code using the Preprocessor. So these Macros are "Meta-Functions". It is also possible to do metaprogramming with templates. This is really powerfull stuff, but at first sight maybe a little hard to understand. I had to read a book about it to fully understand how it works. But it is REALLY worth it! (here's the page of the book:
http://boost-consulting.com/mplbook/. Basically you can use the Compilers Prerprocessor or its Template-Facility to do calculations on Types. You can run the Compiler like a Touring-Machine (although a not very fast one). I actually wrote a little program once that made the compiler simulate a one-dimensional mechanical system, just using templates! As the compiler was finished with compiling, the simulation results could be read out from an array inside the executable
![Wink ;-)](./images/smilies/icon_wink.gif)
. So the only thing the App did at runtime, was just read out the result-Array. This worked on gcc3.5 but unfortunately they changed something in gcc4 and now it does not work anymore
![Sad :-(](./images/smilies/icon_sad.gif)
. Maybe it is just because my gcc4 was altered by Apple. It would be cool if someone could check, whether it works with his compiler, or tell me the errors he gets. The source can be obtained here:
http://tempire.selfip.org/index.php/COTOS