I'm trying to write a generic container class that I can use as a library. I want to use it to instantiate each object in a collection exactly once. So I thought to pass in both the pointer to the new instance and an accomodating enum class to be able to make sure all objects are unique and accounted for.
After repeating myself a few times in functions like these ...
enum class HBeams
{
HEB100,
enumSize
};
void HBeam::CreatePredefinedHBeam(std::map<HBeams, HBeam *> & _library, HBeams _key, ushort _width, ushort _height)
{
if(_library.count(_key) == 0)
{
_library.insert(std::pair<HBeams, HBeam *>(_key, new HBeam{ _width, _height } ));
if(_library.count(_key) == 1)
{
Logger::Log(Status::OK, "Created predefined HBeam with ID ", static_cast<int>(_key));
return;
}
}
Logger::Log(Status::Warning, "Failed to create predefined HBeam with ID ", static_cast<int>(_key));
return;
}
... I felt I should create a generic container class for this. However,
// Library.h
template <class T, enum class E>
class Library
{
std::map<E, T*> m_entries;
public:
void Insert(T* _entry, E _enum);
};
// Library.cpp
template<class T, enum class E>
void Library<T, E>::Insert(T* _entry, E _enum)
{
...
}
... first tells me I must use 'enum' not 'enum class', then that my template argument for non-type template parameter should be an expression. It seems I can overcome this by defining all enum classes in the header of the Library class like this:
enum class HBeams
{
HEB100,
enumSize
};
template <class T>
class HBeamLibrary
{
std::map<HBeams, T*> m_entries;
public:
void Insert(T* _entry, HBeams _key);
};
... but then I still have to manually create a class for each implementation. I there a way to make the "enum class template" work?