#How to put class instances in enum class (c++)
1 messages · Page 1 of 1 (latest)
for context, the code that I'm trying to write:
class Difusion_matrix
{
private:
floatGrid3d _matrix;
unsigned short int _dimX;
unsigned short int _dimY;
unsigned short int _dimZ;
unsigned int _sum;
public:
Difusion_matrix(ratioGrid3d matrix)
{
_dimX = matrix.size();
_dimY = matrix[0].size();
_dimZ = matrix[0][0].size();
/* compute sum */
_sum = 0;
for (auto x : matrix)
for (auto y : x)
for (auto z : y)
_sum += z;
/* populate kernel with multipliers which summation equals 1 */
_matrix = std::vector(_dimX, std::vector(_dimY, std::vector(_dimZ, 0.f)));
for (auto x = 0; x < _dimX; x++)
for (auto y = 0; y < _dimY; y++)
for (auto z = 0; z < _dimZ; z++)
_matrix[x][y][z] = static_cast<float>(matrix[x][y][z]) / _sum;
}
std::array<int, 3> getDim() { return { _dimX, _dimY, _dimZ }; }
floatGrid3d& operator ()() { return _matrix; }
};
enum class Kernels
{
modified_floyd_steinberg_kernel = Difusion_matrix(
{{{0, 0, 7},
{3, 6, 2}},
{{3, 6, 2},
{1, 3, 1}}}),
modified_Jervis_Judice_Ninke_kernel = Difusion_matrix(
{{{0, 0, 0, 7, 5},
{3, 5, 7, 5, 3},
{1, 3, 5, 3, 1}},
{{3, 5, 7, 5, 3},
{2, 4, 4, 4, 2},
{0, 1, 2, 1, 0}},
{{1, 3, 5, 3, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0}}})
};
yeah, it might not be like Java, where you could do this
It might work with an int[][][]
and create the class at run_time or instantiate a private static const object outside and then putting a reference pointer or something
also Difusion_matrix use std::vector
which goes on the heap
you could subclass and change the new operator, but it's gonna go on the heap right?
why not make those static creators ?
wouldn't it means that the constructor only create one instance?
if you cache it yes
then it's a multiton (multiple singleton)
but in C++, it will probably leak, because when are you going to delete it?
or you use like a shared_ptr<T> ?
well you could have an enum with all 12 types and <K,V> the int[][][]
and pass that (const enum&) to a creator method
that cache it via a static std::map<K,V>
the point is that it's created on demand
unless you want to pre-allocate all of them at boot time
ideally all on the stack at compile time yea
in such case, I would create another method to pre-populate
why on the stack? 🤔
Difusion_matrix::initializeAll()
anywhere; just that it's ideal if it doesn't have to populate anything at run-time
also Difusion_matrix should be 2 f
Diffusion_matrix
it will populate stuff at run-time, because std::vector<T>
yea, I can look at replacing it with array<T, s>
otherwise you need to create a template that is acting on raw int[][][]
without doing any allocation
and without virtual either
I rather not; there is a normalisation step that I rather not do by hand:
we used that trick at my ex-workplace
well normalization is done at run-time then
otherwise you need to pre-generate the code normalized
or to normalized it with constexpr at compile-time
that would be the point if only we could have an inline or constexpr constructor
speed, and to see if I could use enum
mostly to use enum
since it makes sense, I mean, on papper
check if you can use int[][][] inside the enum
first
but normalization would be run-time overhead either once or each call
I tried puting value={{1, 2},{3 4}} kind of stuff; doesn't work even tho that should be constant
or read-write
I dunno, I remember doing weird stuff with C++ enum
but that was a long-time ago
well whatever then I guess I'll give up; meanwhile to keep prototyping I have been using a simple class