How to organize these things ?

Currently I have such modules:
"types_enum.h"
"constructors_enum.h"
"common.h"
"common.cpp"
...

Take a fast look at my "types_enum.h":
1
2
3
4
5
6
7
8
9
10
enum __component_types {
///>
    COMPONENT_TYPE_BACKGROUND,
    COMPONENT_TYPE_TILEBOX,
    COMPONENT_TYPE_POINT,
    COMPONENT_TYPE_SPRITE,
    COMPONENT_TYPE_CURSOR,
///<
COMPONENT_TYPES_COUNT
};


And here is my "constructors_enum.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "common.h"

///>
#include "background.h"
#include "tilebox.h"
#include "point.h"
#include "sprite.h"
#include "cursor.h"
///<

TComponent *(* const __component_constructors_array[COMPONENT_TYPES_COUNT])(TFrame * frame) = {
///>
    &__component_constructor<TBackground>,
    &__component_constructor<TTilebox>,
    &__component_constructor<TPoint>,
    &__component_constructor<TSprite>,
    &__component_constructor<TCursor>
///<
};


What is here?
I need to enumerate each class with some index and each constructor of it.

IN A RESULT:
I have to have 2 files: one for TYPES and one for CONSTRUCTORS.
Why?
Because COMMON.H uses TYPES but does not use CONSTRUCTORS (but common.cpp uses constructors),
but CONSTRUCTORS use COMMON.H.
So I can't put TYPES and CONSTRUCTORS in 1 file, because they would require each other.

But I would very like to have it all in 1.
Is there way to do it?
Last edited on
Well, it seems like I found a solution myself.
Instead of initializing constructors array like that, I create 1 instance of "initializer class" in each cpp file, that sets CONSTRUCTOR[INDEX] when created, and then self-destroys. (I don't need to set constructor pointers by myself anymore) This array isn't const, and it all takes a little run-time, but it's not principal.
Last edited on
Who are interested might wanna look at my solution:

common.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...

///Component type initializer

class __component_constructors {
    friend class __component_type_initializer;
    friend class TFrame; //as components are created on frames

    static TComponent *(* array[COMPONENT_TYPES_COUNT])(TFrame *);
};

class __component_type_initializer {
    public:
        __component_type_initializer(int type, TComponent *(* constructor)(TFrame *));
};

template<typename t> TComponent * __component_constructor(TFrame * frame) {
    return new t(frame);
}

#define init_component_type(type, className) \
__component_type_initializer className##_initalizer_instance(type, &__component_constructor<className>) 


common.cpp:
1
2
3
4
5
6
7
8
9
10
...

///Component type initializer

TComponent *(* __component_constructors::array[COMPONENT_TYPES_COUNT])(TFrame *);

__component_type_initializer::__component_type_initializer(int type, TComponent *(* constructor)(TFrame *)) {
    __component_constructors::array[type] = constructor;
    delete this;
}


For example, background.cpp:
1
2
3
4
5
...

init_component_type(COMPONENT_TYPE_BACKGROUND, TBackground);

...


So you have to write just line of code.
Last edited on
Topic archived. No new replies allowed.