Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
inline static base* instance(base* ptr = NULL) { static unsigned short count = 0; std::assert(count++ != 0); static base* _inst = ptr; if (_inst == NULL) throw std::runtime_error("no instance"); return _inst; } |
C-/C++-Quelltext |
|
1 |
base* a = derived::create(); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
#include <iostream> #include <map> #include <cassert> struct uncopyable { protected: uncopyable() {} ~uncopyable() {} private: uncopyable( const uncopyable& ); uncopyable& operator=( const uncopyable& ); }; struct base : protected uncopyable { typedef void ( base::*memfun_ptr )( const void* ); typedef std::map< std::size_t, memfun_ptr > list_type; typedef list_type::iterator iterator_type; static base* create() { static int instances = 0; assert( !( instances++ ) && "zu viele instanzen" ); static base inst; return &inst; } void register_msg( std::size_t id, memfun_ptr handler ) { list[ id ] = handler; } void call_msg( std::size_t id, const void* data ) { iterator_type it( list.find( id ) ); if ( it != list.end() ) ( this->*( it->second ) )( data ); } virtual void test( const void* data ) { std::cout << "calling base::test()\n"; } protected: base() { register_msg( 0, &base::test ); } private: std::map< std::size_t, memfun_ptr > list; }; struct derived : base { static derived* create() { static int instances = 0; assert( !( instances++ ) && "zu viele instanzen" ); static derived inst; return &inst; } void test( const void* data ) { std::cout << "calling derived::test()\n"; } void foo( const void* data ) { std::cout << "calling derived::foo()\n"; } protected: derived() { register_msg( 0, static_cast< base::memfun_ptr >( &derived::test ) ); register_msg( 1, static_cast< base::memfun_ptr >( &derived::foo ) ); } }; int main() { base* a = derived::create(); // create derived object base* b = base::create(); // create base object //derived* c = derived::create(); // runtime assertion //base* d = base::create(); // runtime assertion a->call_msg( 0, 0 ); // calling derived::test a->call_msg( 1, 0 ); // calling derived::foo b->call_msg( 0, 0 ); // calling base::test } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
struct base : protected uncopyable { static base* instance() { static base* inst = 0; if ( inst == 0 ) inst = base::create(); return inst; } protected: static base* create() { static int instances = 0; assert( !( instances++ ) && "zu viele instanzen" ); static base inst; return &inst; } private: std::map< std::size_t, memfun_ptr > list; }; struct derived : base { static base* instance() { static base* inst = 0; if ( inst == 0 ) inst = derived::create(); return inst; } protected: static derived* create() { static int instances = 0; assert( !( instances++ ) && "zu viele instanzen" ); static derived inst; return &inst; } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
int main() { base::instance()->call_msg( 0, 0 ); derived::instance()->call_msg( 1, 0 ); derived::instance()->call_msg( 0, 0 ); } |
Werbeanzeige