Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C++

Enumerator Lists and Enumerated Arrays

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
4 Oct 2011CPOL11 min read 59.2K   464   15  
Establishing a strong binding between enumerations and arrays
// 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, 
    typename list::value_type new_value_start,
    typename list::value_type new_value_end = new_value_start
> struct find_insert_pos;

enum search_state {
    pos_found,
    search_continue,
    search_error
};

template<
    typename list, 
    typename list::value_type new_value_start,
    typename list::value_type new_value_end,
    search_state
> struct find_insert_pos_switch;


template<
    typename list, 
    typename list::value_type new_value_start,
    typename list::value_type new_value_end
> struct find_insert_pos_switch<
    list,
    new_value_start,
    new_value_end,
    pos_found
> {
    static const int value = list::size;
};


template<
    typename parent_list,
    typename parent_list::value_type value_start,
    typename parent_list::value_type value_end,
    typename parent_list::value_type new_value_start,
    typename parent_list::value_type new_value_end
> struct find_insert_pos_switch<
    list_item<parent_list, value_start, value_end>,
    new_value_start,
    new_value_end,
    search_continue
> {
    static const int value = find_insert_pos<parent_list, new_value_start, new_value_end>::value;
};


template<
    typename parent_list,
    typename parent_list::value_type value_start,
    typename parent_list::value_type value_end,
    typename parent_list::value_type new_value_start,
    typename parent_list::value_type new_value_end
> class find_insert_pos<
    list_item<parent_list, value_start, value_end>,
    new_value_start,
    new_value_end
> {
    typedef list_item<parent_list, value_start, value_end> this_list;

    static_assert(is_ascending<this_list>::value, "find_insert_pos<...> list must be ascending");
    static_assert(new_value_end >= new_value_start, "find_insert_pos<...> value parameters must be ascending");

    const static search_state state_tag =
        (new_value_start > value_end) ? pos_found :
        (new_value_start <= value_end) && (new_value_end >= value_start) ? search_error :
        search_continue;   

    static_assert(state_tag != search_error, "find_insert_pos<...> value parameters overlap with list item");         
    
public:
    static const int value = find_insert_pos_switch<this_list, new_value_start, new_value_end, state_tag>::value;
};


template<
    typename value_type,
    value_type new_value_start,
    value_type new_value_end
> class find_insert_pos<
    list_tail<value_type>,
    new_value_start,
    new_value_end
> {
    void a1() {
        static_assert(new_value_end >= new_value_start, "find_insert_pos<...> value parameters must be ascending");
    }
        
public:
    static const int value = 0;
};


}

}

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