
On Apr 8, 2005 9:49 PM, Hartmut Kaiser <HartmutKaiser@t-online.de> wrote:
Matt Hurd wrote:
Do you think Wave could be used for adding some syntatic sugar that would allow easy compile time programming of strings?
What concretely do you have in mind?
I've been toying with some thoughts of writing some compile time parsing functionality. To do this I need compile time strings which aren't in the standard. I think the closest thing is probably using a mpl::vector_c< char... The definitions of such are a bit unwieldy to say the least. E.g. typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello; typedef mpl::vector_c< char, 'w', 'o', 'r', 'l', 'd' > cts_world; I could localise such things in a resource file and have a little program transform it into an appropriate hpp or perhaps use a little preprocessor extension to do the same if it was straightforward. For example, a quick hack of a compile time string doo-dad: #include <iostream> #include <string> #include <boost/mpl/vector_c.hpp> #include <boost/mpl/size.hpp> #include <boost/mpl/front.hpp> #include <boost/mpl/copy.hpp> #include <boost/mpl/back_inserter.hpp> #include <boost/mpl/pop_front.hpp> namespace mpl = boost::mpl; typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello; typedef mpl::vector_c< char, ' ', 'w', 'o', 'r', 'l', 'd' > cts_world; template < class CTS > struct cts_to_rts { char value[ mpl::size< CTS >::value ] ; cts_to_rts() { write_string< mpl::size< CTS >::value, CTS >::apply( value ); } std::string string_copy() { return std::string( value, mpl::size< CTS >::value ); } private: template< int N, class CTS > struct write_string { static void apply( char * p ) { *p = mpl::front< CTS >::type::value; write_string<N-1, typename mpl::pop_front< CTS >::type
::apply( ++p ); } };
template< class CTS > struct write_string< 0, typename CTS > { static void apply( char * p ) { } }; }; typedef mpl::copy < cts_world , mpl::back_inserter< cts_hello >
::type cts_hello_world;
int main() { cts_to_rts< cts_hello_world > rts_hello_world; std::cout << rts_hello_world.string_copy() << "\n"; system("pause"); return 0; }
I'm not quite sure if it is sufficient to have Wave around to solve the problem you've mentioned. Probably you'll need some kind of C++ semantic analysis for that (i.e. a parser for C++ or a significant subset).
was thinking perhaps a __cts"hello" could be translated to typedef mpl::vector_c< char, 'h', 'e', 'l', 'l', 'o' > cts_hello by wave or something more appropriate. Simple enough to do my own preprocessor pass for this as it is a fairly straight forward manipulation. Just wondering out loud as I can imagine all sorts of nice regex, parsing and state machine things I'd like to be able to do at compile time. Any better thoughts on pseudo string manipulation at compile time? matt. matthurd@acm.org