MPL Map of types to integral values
Hi, I'm looking for a complete compile-time constant map container that uses types as keys and integral constants as values. For example: typedef map< pair<float, 90>, pair<unsigned int, 3> > mapType; unsigned int value = get_value<mapType, float>::value; The above code is PSEUDO CODE only. It is supposed to give you an idea of what I'm looking for. I tried using boost::fusion::map, however the fusion::pair object expects a TYPE as the second template argument, so I can't specify integral values. Could someone provide a code example that achieves the above? Note that I should be able to do the following as well: typedef map< pair<float, 90>, pair<unsigned int, 3> > mapType; class foo { // This would result in 'value' being the integral number 90. static const unsigned int value = get_value<mapType, float>::value; }
So far, this is all I've been able to come up with: #include <boost/mpl/map.hpp> #include <boost/mpl/at.hpp> namespace detail { template< unsigned int t_value > struct integral { static const unsigned int value = t_value; }; typedef boost::mpl::map< boost::mpl::pair<float, integral<GL_FLOAT>
, boost::mpl::pair<double, integral<GL_DOUBLE>
> glTypeMap; }; template< typename t_type > struct Vertex { static const unsigned int type = boost::mpl::at<detail::glTypeMap, t_type>::type::value; t_type x, y, z; }; On Nov 16, 2007 5:48 PM, Robert Dailey <rcdailey@gmail.com> wrote:
Hi,
I'm looking for a complete compile-time constant map container that uses types as keys and integral constants as values. For example:
typedef map< pair<float, 90>, pair<unsigned int, 3> > mapType; unsigned int value = get_value<mapType, float>::value;
The above code is PSEUDO CODE only. It is supposed to give you an idea of what I'm looking for. I tried using boost::fusion::map, however the fusion::pair object expects a TYPE as the second template argument, so I can't specify integral values. Could someone provide a code example that achieves the above? Note that I should be able to do the following as well:
typedef map< pair<float, 90>, pair<unsigned int, 3> > mapType;
class foo { // This would result in 'value' being the integral number 90. static const unsigned int value = get_value<mapType, float>::value; }
--- Robert Dailey wrote:
So far, this is all I've been able to come up with:
#include <boost/mpl/map.hpp> #include <boost/mpl/at.hpp>
namespace detail { template< unsigned int t_value > struct integral { static const unsigned int value = t_value; };
typedef boost::mpl::map< boost::mpl::pair<float, integral<GL_FLOAT>
,
boost::mpl::pair<double, integral<GL_DOUBLE>
> glTypeMap; };
template< typename t_type > struct Vertex { static const unsigned int type = boost::mpl::at<detail::glTypeMap, t_type>::type::value; t_type x, y, z; };
Try this: #include <boost/mpl/integral_c.hpp> #include <boost/mpl/map.hpp> #include <boost/mpl/at.hpp> namespace detail { using namespace boost; using namespace mpl; typedef map< pair< float , integral_c<unsigned int,GL_FLOAT> > , pair< double , integral_c<unsigned int,GL_DOUBLE> > > glTypeMap; template <typename t_type> struct Vertex { static const unsigned int type = at<glTypeMap,t_type>::type::value; t_type x, y, z; }; } // namespace detail HTH, Cromwell D. Enage ____________________________________________________________________________________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/
That's basically what I did in the previous post, the only differences are: 1) You're using boosts version of integral (which I didn't know about), however I could make my integral struct a wrapper for integral_c (since integral_c specifies one additional template parameter that will always be the same in this case). 2) You're specifying "using namespace" inside of namespace scope, which extends out into global namespace. 'using' does not respect namespace scope. This conflicts with a lot of objects I have defined outside of the namespace. Thanks for your help. On Nov 16, 2007 8:22 PM, Cromwell Enage < sponage@yahoo.com> wrote:
--- Robert Dailey wrote:
So far, this is all I've been able to come up with:
#include <boost/mpl/map.hpp> #include <boost/mpl/at.hpp>
namespace detail { template< unsigned int t_value > struct integral { static const unsigned int value = t_value; };
typedef boost::mpl::map< boost::mpl::pair<float, integral<GL_FLOAT>
,
boost::mpl::pair<double, integral<GL_DOUBLE>
> glTypeMap; };
template< typename t_type > struct Vertex { static const unsigned int type = boost::mpl::at<detail::glTypeMap, t_type>::type::value; t_type x, y, z; };
Try this:
#include <boost/mpl/integral_c.hpp> #include <boost/mpl/map.hpp> #include <boost/mpl/at.hpp>
namespace detail { using namespace boost; using namespace mpl;
typedef map< pair< float , integral_c<unsigned int,GL_FLOAT> > , pair< double , integral_c<unsigned int,GL_DOUBLE> >
glTypeMap;
template <typename t_type> struct Vertex { static const unsigned int type = at<glTypeMap,t_type>::type::value; t_type x, y, z; }; } // namespace detail
HTH, Cromwell D. Enage
____________________________________________________________________________________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
--- Robert Dailey wrote:
That's basically what I did in the previous post, the only differences are:
1) You're using boosts version of integral (which I didn't know about), however I could make my integral struct a wrapper for integral_c (since integral_c specifies one additional template parameter that will always be the same in this case).
I'm guessing that you're looking for a map_c that kind of wraps around a map almost the same way a vector_c is wrapped around a vector. Sorry, no such beast exists yet. You'll have to roll your own, either just the way you did or with my suggestions.
2) You're specifying "using namespace" inside of namespace scope, which extends out into global namespace. 'using' does not respect namespace scope. This conflicts with a lot of objects I have defined outside of the namespace.
We all know that "using namespace" declarations outside class scope are a bad idea, especially in header files. (Not sure if they work in class scope but outside function scope, haven't tried that yet.) They were placed there to keep the e-mail concise and readable--eyes start to glaze after reading too many boost::mpl:: prefixes and so on.
Thanks for your help.
No problem. Cromwell D. Enage ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs
participants (2)
-
Cromwell Enage
-
Robert Dailey