Boost logo

Boost Users :

From: Howard Gardner (hgardner_at_[hidden])
Date: 2006-04-24 08:26:54


/*
This code detects the presence of particular syntax in a type.
The relevant part is the template class syntax_member_insert:
it will ramify into an unbounded set of similar classes, each of
which is checking for some particular interesting bit of syntax.

I am aware of parts of boost that grapple with this issue very
directly:

   boost::concept_check
     The concepts to be checked are close kin to the syntax I'm looking
     for.

   boost::concept_traits
     Again, the concepts to be checked are close kin to the syntax I'm
     looking for.

And I'm aware of parts of boost that touch on this issue indirectly:

   boost::function
     Contains a really nice mechanism for expressing function signatures.

     In point of fact, one effort that I made to generalize the
     implementation of syntax_member_insert yielded a solution with
     template classes like:

     template< typename x, typename xRet, typename xDummy = dummy, ... >
       struct member_function;

     template< typename x, typename xRet >
       member_function< x, xRet >;

     template< typename x, typename xRet, typename xP0 >
       member_function< x, xRet, xP0 >;

     These look suspiciously like the boost::functionX< > templates.

   boost::type_traits
     The is_* classes are used in one of the ways that I would want to
     use supports_syntax.

        boost::mpl
                Represents a *lot* of other uses for supports_syntax.

It seems to me that it would be good to express this unbounded set of
interesting syntax checks so that all of those facilities can use it.
Does such a mechanism already exist? Is it feasable to create it using
existing boost facilities? Is it feasable to create it at all?
*/

#include<ostream>
using namespace std;

template< typename xParm >
struct syntax_member_insert
   {
     template< typename x, x & (x::*)( xParm ) = &x::insert >
       struct match;
   };

template< typename x, typename xSyntax >
   struct supports_syntax
   {
     private:

       typedef char ret_true;
       struct ret_false{ char c[2]; };

       template< typename >
         static
          ret_false
            func( ... );

       template< typename xX >
         static
           ret_true
             func( typename xSyntax::template match< xX > * );

     public:

       static
         const bool
           value
             = ( sizeof( func< x >( 0 ) ) == sizeof( ret_true ) );
   };

struct passes
{
   passes &
     insert( char );
};

struct fails{};

int
   main()
   {
     cout
       << supports_syntax< passes, syntax_member_insert< char > >::value
       << endl;

     cout
       << supports_syntax< fails, syntax_member_insert< char > >::value
       << endl;
        }


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net