Click here to Skip to main content
15,898,134 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.3K   464   15  
Establishing a strong binding between enumerations and arrays
#pragma once

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

inline void cout_title(const std::string& title) {
    std::cout << std::endl;
    std::cout << title << std::endl;
    std::cout << std::setfill('-') << std::setw(title.size()) << "" << std::endl;
}

// Testwise run-time iteration through enumerated array
template<typename enum_array>
void iterator_test(const std::string& title, const enum_array& arr) 
{
    cout_title(title);
    
    std::cout << std::setfill(' ');
    
    std::cout << "enum_array size: " << enum_array::size << std::endl;
    
    for (typename enum_array::index_list::const_iterator it = enum_array::index_list::begin(); 
        it != enum_array::index_list::end(); 
        it++) 
    {
        std::ostringstream s;
        s << "Element " << enum_array::index_list::index_of(*it) << ":";
        std::cout << std::setw(12) << s.str() << arr[*it] << " " << std::endl;
    }
}

template<typename t>
void cout_desc(const char* description, t info) {
    std::cout /* << std::setw(20) */ << description << ": " << info << std::endl;
}

inline void size_test(int real_size, int exp_size, const char* description) {
    std::cout << std::left << std::setw(6);
    real_size == exp_size ? std::cout << "OK!" : std::cout << "FAIL!";
    cout_desc(description, real_size);
}

template<typename t>
void value_test(t real_value,t exp_value, const char* description) {
    std::cout << std::left << std::setw(6);
    real_value == exp_value ? std::cout << "OK!" : std::cout << "FAIL!";
    cout_desc(description, real_value);
}


inline void cout_list(const char* title, const char* content) {
    cout_title(title);
    std::cout << content << std::endl;
}


#define TEST_SIZE(x, e) size_test(sizeof(x) / sizeof((x)[0]), (e), #x)
#define TEST_VALUE(x, e) value_test((x), e, #x)
#define COUT_LIST(x) cout_list(#x, x)
#define COUT_SIZE(x) cout_desc("Size of \"" #x "\"", (sizeof(x) / sizeof((x)[0])))
#define COUT_VALUE(x) cout_desc("Value of \"" #x "\"", (x))

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