Click here to Skip to main content
15,891,513 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.1K   464   15  
Establishing a strong binding between enumerations and arrays
// This file is part of EnumeratorLib
// 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 enm {

template<typename enum_array, int index> struct _at_rt;

template<
    typename enum_array, int index>
struct _at_rt {
    typedef typename copy_const<typename enum_array, typename enum_array::value_type>::type value_type;
    typedef typename copy_const<typename enum_array, typename enum_array::parent_array>::type parent_array;
    
    inline static value_type& value(enum_array& arr, typename enum_array::enum_type enum_value_test) {
        return enum_value_test == enum_array::enum_value ?
            arr._value
                :
            _at_rt<parent_array, parent_array::size-1>::value(arr, enum_value_test);  
    }
};

template<
    typename enum_array>
struct _at_rt <
    enum_array,
    0
> {
    typedef typename copy_const<typename enum_array, typename enum_array::value_type>::type value_type;
    
    inline static value_type& value(enum_array& arr, typename enum_array::enum_type enum_value_test) {
        if (enum_value_test != enum_array::enum_value) {
            throw std::out_of_range("_at");
        }
        return arr._value;
    }
};

}

}

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