I am having an error with accessing static members of the class CErrortracker(example code below). What is being tried here is to have a common Error module which has an id, error level and the error source. The error source basically will have some common error sources and some component specific sources. In the below example enum "ErrorSourceBase_t" has common error sources and "componentErrorSrc_t" has component specific error sources. These two enums are combined using "InheritEnum" class. The code as below: ErrorModuleUtility.h
#pragma once
#include <array>
#include <assert.h>
using namespace std;
/* -------------------------------- */
typedef std::array<unsigned char, 32> characterArray_t;
typedef enum ErrorLevel_t
{
ERRLVL_NONE = 0,
ERRLVL_INFO,
ERRLVL_WARNING,
ERRLVL_NOT_CRITICAL,
ERRLVL_CRITICAL,
ERRLVL_NUM_LEVELS,
ERRLVL_MAX = 2147483647
}ErrorLevel_t;
typedef enum ErrorSourceBase_t
{
ERRSRC_NONE = 0,
ERRSRC_ONE,
ERRSRC_TWO,
ERRSRC_THREE,
ERRSRC_MAX = 2147483647
}ErrorSourceBase_t;
template <typename EnumT, typename BaseEnumT>
class InheritEnum
{
public:
InheritEnum() {}
InheritEnum(EnumT e)
: enum_(e)
{}
InheritEnum(BaseEnumT e)
: baseEnum_(e)
{}
explicit InheritEnum(int val)
: enum_(static_cast<EnumT>(val))
{}
operator EnumT() const { return enum_; }
private:
// Note - the value is declared as a union mainly for as a debugging aid. If
// the union is undesired and you have other methods of debugging, change it
// to either of EnumT and do a cast for the constructor that accepts BaseEnumT.
union
{
EnumT enum_;
BaseEnumT baseEnum_;
};
};
ErrorModule.h
#pragma once
#include "ErrorModuleUtility.h"
template<typename ErrorSourceComp_t>
class CErrortracker
{
public:
typedef InheritEnum<ErrorSourceComp_t, ErrorSourceBase_t> ErrorSource_t;
// have to use typename instead of ErrorSource_t, since non floating point data type cannot be a template parameter
template<int _code, ErrorLevel_t _level, const ErrorSource_t& _source>
class BaseError
{
public:
static const int id = _code;
static const ErrorLevel_t lvl = _level;
static const ErrorSource_t src = _source;
};
static CErrortracker & getInstance()
{
static CErrortracker<ErrorSourceComp_t> _instance;
//static CErrortracker _instance;
return _instance;
}
template<typename T>
inline void setError(int sourceLine, characterArray_t sourceFile, int optionalData = 0)
{
int errorid = T::id; // Error is observed here
}
private:
// singleton class
CErrortracker()
: m_errorCount(0)
, m_highesterrorLevel(ERRLVL_NONE)
{
}
// disable copy constructor and assignement operator
CErrortracker(const CErrortracker&) = delete;
CErrortracker& operator= (const CErrortracker&) = delete;
int m_errorCount;
ErrorLevel_t m_highesterrorLevel;
};
static const char* getPointerToLastChar(const char file[])
{
// find the terminating \0 in the character sequence
while (*file != '\0')
{
file++;
}
file--; // last character is just before \0....
return (file);
}
static const characterArray_t getFileNameFromFullPath(const char file[])
{
const char *tmpFile = getPointerToLastChar(file);
characterArray_t fileName;
// find position of fileNmae within file[]
while ((*tmpFile != '\\') && (*tmpFile != '/') && (tmpFile >= file))
{
tmpFile--;
}
tmpFile++;
// copy fileName into output structure
bool isFull = false;
int itr(0);
while ((*tmpFile != '\0') && !isFull)
{
fileName[itr] = (static_cast<unsigned char>(*tmpFile));
isFull = itr == 31;
tmpFile++;
itr++;
}
fileName[itr] = (static_cast<unsigned char>('\0')); // terminate according to usual convention
return fileName;
}
static characterArray_t getFileID(const char file[])
{
characterArray_t fileName;
if (nullptr != file)
{
fileName = getFileNameFromFullPath(file);
}
return fileName;
}
#define ERROR_MAGIC_MACRO(code, level, src, name) \
typedef CErrortracker::BaseError<(code), (level), (src)> (name);
#define FILE_ID getFileID(__FILE__)
ComponentErrors.h
#pragma once
#include "ErrorModule.h"
//define the error codes here
typedef enum componentErrorSrc_t
{
ERRSRC_DATA1,
ERRSRC_DATA2
}componentErrorSrc_t;
#define COMPONENT_ERROR_LIST \
ERROR_MAGIC_MACRO( 0, ERRLVL_NONE, ERRSRC_NONE, VODCA_ERROR_NO_ERROR) \
ERROR_MAGIC_MACRO( 1, ERRLVL_WARNING, ERRSRC_ONE, VODCA_ERROR_UNKNOWN) \
ERROR_MAGIC_MACRO( 30, ERRLVL_CRITICAL, ERRSRC_TWO, VODCA_ERROR_INVALID_IMG_AREA) \
ERROR_MAGIC_MACRO( 40, ERRLVL_CRITICAL, ERRSRC_DATA1, VODCA_ERROR_DATA1)
COMPONENT_ERROR_LIST
And finally the component(main) file, Component.cpp
#include "ComponentErrors.h"
int main()
{
//CErrortracker< componentErrorSrc_t>& instance = CErrortracker<componentErrorSrc_t>::getInstance();
CErrortracker< componentErrorSrc_t>& instance = CErrortracker< componentErrorSrc_t>::getInstance();
instance.setError<VODCA_ERROR_INVALID_IMG_AREA>(__LINE__, FILE_ID);
return EXIT_SUCCESS;
}
The observed error is Error C1001 An internal error has occurred in the compiler.
Note: If the Inheritance is removed, i do not get any issue with the compilation.
Please let me know what am i doing wrong here ?
Aucun commentaire:
Enregistrer un commentaire