Click here to Skip to main content
15,896,501 members
Articles / Programming Languages / VC++

Static Value Lists

Rate me:
Please Sign up or sign in to vote.
4.60/5 (7 votes)
25 Aug 2011CPOL18 min read 23.6K   246   17  
An introduction to advanced template metaprogramming using an explanatory project
// This file is part of the lobster library
// Copyright: Andreas Raczek
// This file is published under the The Code Project Open License (CPOL) 
// See the file "CPOL.html" for the full license governing this code. 
#pragma once

namespace lobster {

namespace static_list {

template<typename list, bool compact> struct compact_helper;

// compact_helper<..., false> does not combine this list item with the parent list item
template<
    typename list
> struct compact_helper<
    list,
    false
> {
    typedef list type;
};


// compact_helper<..., false> does not combine this list item with the parent list item
template<
    typename grandparent,
    typename grandparent::value_type parent_value_first,
    typename grandparent::value_type parent_value_last,
    typename grandparent::value_type value_first,
    typename grandparent::value_type value_last
> struct compact_helper<
    list_item<
        list_item<
            grandparent, 
            parent_value_first, 
            parent_value_last 
        >,
        value_first,
        value_last
    >,
    true
> {
    typedef list_item<grandparent, parent_value_first, value_last> type;
};


template<typename list> struct compact;


template<
    typename grandparent,
    typename grandparent::value_type parent_value_first,
    typename grandparent::value_type parent_value_last,
    typename grandparent::value_type value_first,
    typename grandparent::value_type value_last
> struct compact<
    list_item<
        list_item<
            grandparent, 
            parent_value_first, 
            parent_value_last 
        >,
        value_first,
        value_last
    >
> {
    typedef typename grandparent::value_type value_type;
    typedef list_item<grandparent, parent_value_first, parent_value_last> parent;
    
    typedef typename compact<parent>::type compacted_parent;
    
    // The compacted parent may have new first and last elements, see how it fits:
    const static bool fits_ascending = 
        ((int) (compacted_parent::value_first) <= (int) (compacted_parent::value_last)) && 
        ((int) (value_first) <= (int) (value_last)) && 
        ((int) (value_first) == ((int) (compacted_parent::value_last) + 1));
        
    const static bool fits_descending = 
        ((int) (compacted_parent::value_first) >= (int) (compacted_parent::value_last)) && 
        ((int) (value_first) >= (int) (value_last)) && 
        ((int) (value_first) == ((int) (compacted_parent::value_last) - 1));
        
    // For both fitting types, the entries are combined
    typedef typename compact_helper<
        list_item<compacted_parent, value_first, value_last>,
        fits_ascending || fits_descending
    >::type type;
};


template<
    typename value_type,
    value_type value_first,
    value_type value_last
> struct compact<
    list_item<
        list_tail<
            value_type
        >,
        value_first,
        value_last
    >
> {
    typedef list_item<list_tail<value_type>, value_first, value_last> type;    
};


}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions