Last active
April 12, 2017 16:27
-
-
Save fl4shk/fbb64e546cd6d67186d1c5b6c3f469fe to your computer and use it in GitHub Desktop.
C++ templates can be used in a nice way for factory stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cout; | |
using std::cin; | |
using std::cerr; | |
using std::endl; | |
// Pass the name of some other macro to list_of_sprite_classes() | |
#define list_of_sprite_classes(other_macro) \ | |
other_macro(sprite_base) \ | |
other_macro(sprite_a) \ | |
other_macro(sprite_b) \ | |
other_macro(sprite_c) \ | |
other_macro(sprite_d) | |
//enum sprite_type | |
//{ | |
// st_sprite_base, | |
// st_sprite_a, | |
// st_sprite_b, | |
// st_sprite_c, | |
// st_sprite_d, | |
// | |
// lim_st, | |
//}; | |
// These two enums are the same | |
enum sprite_type | |
{ | |
#define gen_enum(spr_class) \ | |
st_##spr_class, | |
list_of_sprite_classes(gen_enum) | |
#undef gen_enum | |
lim_st, | |
}; | |
class sprite_base | |
{ | |
public: // functions | |
virtual ~sprite_base() | |
{ | |
} | |
virtual void update() | |
{ | |
cout << "sprite_base!\n"; | |
} | |
}; | |
class sprite_a : public sprite_base | |
{ | |
public: // functions | |
virtual void update() | |
{ | |
cout << "sprite_a!\n"; | |
} | |
}; | |
class sprite_b : public sprite_base | |
{ | |
public: // functions | |
virtual void update() | |
{ | |
cout << "sprite_b!\n"; | |
} | |
}; | |
class sprite_c : public sprite_base | |
{ | |
public: // functions | |
virtual void update() | |
{ | |
cout << "sprite_c!\n"; | |
} | |
}; | |
class sprite_d : public sprite_base | |
{ | |
public: // functions | |
virtual void update() | |
{ | |
cout << "sprite_d!\n"; | |
} | |
}; | |
template<typename spr_class> | |
sprite_base* gen_sprite() | |
{ | |
return new spr_class(); | |
} | |
typedef sprite_base* (*spr_generator)(); | |
//const spr_generator test_spr_gen_arr[] | |
// = { &(gen_sprite<sprite_base>), | |
// &(gen_sprite<sprite_a>), | |
// &(gen_sprite<sprite_b>), | |
// &(gen_sprite<sprite_c>), | |
// &(gen_sprite<sprite_d>) }; | |
#define gen_func_ptr(spr_class) \ | |
&(gen_sprite<spr_class>), | |
const spr_generator test_spr_gen_arr[lim_st] | |
= { | |
list_of_sprite_classes(gen_func_ptr) | |
}; | |
#undef gen_func_ptr | |
int main( int argc, char** argv ) | |
{ | |
sprite_base * base, * a, * b, * c, * d; | |
base = test_spr_gen_arr[st_sprite_base](); | |
a = test_spr_gen_arr[st_sprite_a](); | |
b = test_spr_gen_arr[st_sprite_b](); | |
c = test_spr_gen_arr[st_sprite_c](); | |
d = test_spr_gen_arr[st_sprite_d](); | |
base->update(); | |
a->update(); | |
b->update(); | |
c->update(); | |
d->update(); | |
cout << endl; | |
base->update(); | |
a->update(); | |
b->update(); | |
c->update(); | |
d->update(); | |
delete base; | |
delete a; | |
delete b; | |
delete c; | |
delete d; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment