Added macro that creates enums with string conversion function automatically.

This commit is contained in:
Éder Ferreira Zulian
2015-05-08 16:00:50 +02:00
parent 6ee652bdca
commit 48154a356d
3 changed files with 43 additions and 3 deletions

View File

@@ -13,9 +13,10 @@
#include <string>
#include <ostream>
#include <tlm.h>
#include <boost/preprocessor.hpp>
#include <iomanip>
#include "dramExtension.h"
#include "third_party/tinyxml2.h"
#include <iomanip>
//TODO : move to timing specific header
sc_time getDistance(sc_time a, sc_time b);
@@ -87,5 +88,43 @@ double queryDoubleParameter(tinyxml2::XMLElement* node, std::string name);
void setUpDummy(tlm::tlm_generic_payload& payload, Bank& bank);
//
// BOOST_PP_STRINGIZE(text)
// text will be converted to a string literal.
//
// BOOST_PP_SEQ_ENUM(enumerators)
// This macro expands to a comma-separated list of the elements in
// enumerators.
//
// BOOST_PP_SEQ_FOR_EACH(macro, data, seq)
// This macro is a repetition construct. If seq is (a)(b)(c), it expands to
// the sequence: macro(r, data, a) macro(r, data, b) macro(r, data, c)
//
// Note: this implementation requires that the enumerators map to unique
// values.
//
// See also:
// http://www.boost.org/doc/libs/1_38_0/libs/preprocessor/doc/
// http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string
//
#define X_DEFINE_ENUM_CLASS_WITH_STRING_CONVERSIONS_TOSTRING_CASE(r, data, elem) \
case data::elem : return BOOST_PP_STRINGIZE(elem);
#define DEFINE_ENUM_CLASS_WITH_STRING_CONVERSIONS(enumName, enumerators) \
enum class enumName { \
BOOST_PP_SEQ_ENUM(enumerators) \
}; \
\
inline const char* ToString(enumName v) \
{ \
switch (v) { \
BOOST_PP_SEQ_FOR_EACH( \
X_DEFINE_ENUM_CLASS_WITH_STRING_CONVERSIONS_TOSTRING_CASE, \
enumName, \
enumerators \
) \
default: return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
} \
}
#endif /* UTILS_COMMON_H_ */

View File

@@ -11,9 +11,10 @@
#include <systemc.h>
#include <string>
#include "MemSpec.h"
#include "../../../common/Utils.h"
enum class EPowerDownMode{NoPowerDown, Staggered, TimeoutPDN, TimeoutSREF};
enum class StorageMode{NoStorage, Store, ErrorModel};
DEFINE_ENUM_CLASS_WITH_STRING_CONVERSIONS(StorageMode, (NoStorage) (Store) (ErrorModel));
struct Configuration
{

View File

@@ -61,7 +61,7 @@ struct Dram: sc_module
IFPOW( MemorySpecification memSpec(MemSpecParser::getMemSpecFromXML(Configuration::getInstance().memspecUri)) );
IFPOW( DRAMPower = new libDRAMPower( memSpec, 0 ) );
//cout << "StorageMode = " << StorMode << endl;
cout << "StorageMode = " << ToString(StorMode) << endl;
if(StorMode == StorageMode::ErrorModel)
{