Du bist nicht angemeldet.

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

11

17.07.2007, 21:48

es sind halt nicht alle so perfekt wie du vllt. schön das man sich hier als anfänger auch noch mal vertuen kann...

so, meine detaillierte fragestellung:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
    class Quit : public Command
    {
        private:
            Quit();
        public:
            ~Quit();
            static void create();
            void doCommand();
    };


das ist eine meiner von Command abgeleiteten Klassen. Ich nehme sie jetzt mal als beispiel um zu erklären was ich möchte. Weiterhin soll es Commands geben, deren create Funktion parameter entgegen nimmt, um das Command zu konkretisieren.
Die Implementierung dürfte hier egal sein.
in meinem Command Manager habe ich ein enum mit allen vorhanden events:

C-/C++-Quelltext

1
    enum CommandName{Quit};


jetzt hat mein CommandManager eine funktion createCommand(string name, std::deque<void*> parameter);

und um diese funktion geht es mir jetzt.
natürlich wäre es möglich einfach ein switch(Name) zumachen, aber ich glaube das ist einfach zu aufwendig wenn man später viele Commands hat. Also hatte ich die idee mit der std::map.
Sie soll so funktionieren das alle bekannten Commands in ihr verzeichnet sind. Die Commandnamen sind der Key über den man auf einen Funktionspointer der mir ein entsprechendes Command zurückliefert zugreift. also hätte ich in der createCommand nur noch ein

C-/C++-Quelltext

1
KnownCommands[Name](paramemeter 1, parameter 2, usw)

stehen. Jetzt ist mein problem das ich ja für die map einen typ angeben muss. Der erste typ ist klar mein enum. aber der zweite typ ist mein problem. Bei der Definition eines Funktionspointers muss ja die anzahl und typ der argumente angeben werden, aber die können ja unterschiedlich sein. Was muss ich also für die definition meiner map für einen typ verwenden?

ich hoffe das war dir detailiert genug, sonst frag bitte nach.



[/cpp]

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

12

17.07.2007, 22:33

Dreh mal nichso am Rad. Es is einfach nich grad toll wenn man sich ne halbe Stunde Gedanken über ein Problem macht und dann trotzdem wieder alles umgeworfen wird. Immerhin ist das Ganze hier ja freiwillig...

Zu deinem Problem: Du kannst das lösen indem du eine Map mit Funktionszeiger auf void (*)() hast und die Funktionen castest. Allerdings brauchst du dann eine Art RTTI um den Pointer zurückcasten zu können.

Zumindest fällt mir Momentan nichts besseres ein:

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
#include <iostream>
#include <map>

struct foo
{
    typedef int ( *funptr )( int );

    static int test( int x )
    {
        return x * 10;
    }
};

struct bar
{
    typedef void ( *funptr )();

    static void test()
    {
        std::cout << "foo\n";
    }
};

int main()
{
    std::map< int, void (*)() > map;

    map[ 0 ] = reinterpret_cast< void (*)() >( foo::test );
    map[ 1 ] = bar::test;

    std::cout << reinterpret_cast< foo::funptr >( map[ 0 ] )( 10 );
    map[ 1 ]();

    std::cin.get();
}
@D13_Dreinig

13

17.07.2007, 22:44

sry, hab grade überreagiert :(

ich hab eine frage zu deiner lösung: wird hierbei das problem mit den mehrern argumenten oder das problem mit dem unbekannten rückgabe typ umgangen? so wie ich das verstehe wird dabei das problem mit den verschiedenen rückgabe typen gelöst oder?

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

14

17.07.2007, 22:49

Naja, beides wird "umgangen". Ist aber nicht gerade die schönste Art mit dem casten.
@D13_Dreinig

15

17.07.2007, 22:59

ich werde mir das morgen nochmal in ruhe anschauen.
danke für die hilfe :)

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

16

17.07.2007, 23:43

So, meine letzte Idee zu dem Thema. Ohne casten hab ichs aber leider nichmehr hinbekommen...

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//////////////////////////////////////////////////////////////////////////

//

// fun_impl

//

//////////////////////////////////////////////////////////////////////////

struct base_fun_impl
{
    typedef NullType parm1;
    typedef NullType parm2;
    typedef NullType parm3;

    virtual ~base_fun_impl() = 0;

    template< typename R >
    R call()
    {
        return ( *reinterpret_cast< fun_impl< R, Sequence<> >* >( this ) )();
    }

    template< typename R, typename P1 >
    R call( P1 p1 )
    {
        return ( *reinterpret_cast< fun_impl< R, Sequence< P1 > >* >( this ) )( p1 );
    }

    template< typename R, typename P1, typename P2 >
    R call( P1 p1, P2 p2 )
    {
        return ( *reinterpret_cast< fun_impl< R, Sequence< P1, P2 > >* >( this ) )( p1, p2 );
    }

    template< typename R, typename P1, typename P2, typename P3 >
    R call( P1 p1, P2 p2, P3 p3 )
    {
        return ( *reinterpret_cast< fun_impl< R, Sequence< P1, P2, P3 > >* >( this ) )( p1, p2, p3 );
    }
};

base_fun_impl::~base_fun_impl()
{}

template< typename R, typename Parms >
struct fun_impl;

template< typename R >
struct fun_impl< R, Sequence<> > : public base_fun_impl
{
    typedef R result_type;
    virtual R operator()() const = 0;
};

template< typename R, typename P1 >
struct fun_impl< R, Sequence< P1 > > : public base_fun_impl
{
    typedef R result_type;
    typedef P1 parm1;

    virtual R operator()( P1 p1 ) const = 0;
};

template< typename R, typename P1, typename P2 >
struct fun_impl< R, Sequence< P1, P2 > > : public base_fun_impl
{
    typedef R result_type;
    typedef P1 parm1;
    typedef P2 parm2;

    virtual R operator()( P1 p1, P2 p2 ) const = 0;
};

template< typename R, typename P1, typename P2, typename P3 >
struct fun_impl< R, Sequence< P1, P2, P3 > > : public base_fun_impl
{
    typedef R result_type;
    typedef P1 parm1;
    typedef P2 parm2;
    typedef P3 parm3;

    virtual R operator()( P1 p1, P2 p2, P3 p3 ) const = 0;
};


//////////////////////////////////////////////////////////////////////////

//

// function_holder

//

//////////////////////////////////////////////////////////////////////////


template< typename R, typename Parms, typename FunPtr >
struct fun_holder
    : public fun_impl< R, Parms >
{
    typedef typename fun_impl< R, Parms > impl;
    typedef typename impl::result_type result_type;
    typedef typename impl::parm1 parm1;
    typedef typename impl::parm2 parm2;
    typedef typename impl::parm3 parm3;

    fun_holder( const FunPtr& ptr )
        : fun( ptr )
    {}

    result_type operator()() const
    {
        return fun();
    }

    result_type operator()( parm1 p1 ) const
    {
        return fun( p1 );
    }

    result_type operator()( parm1 p1, parm2 p2 ) const
    {
        return fun( p1, p2 );
    }

    result_type operator()( parm1 p1, parm2 p2, parm3 p3 ) const
    {
        return fun( p1, p2, p3 );
    }

private:
    FunPtr fun;
};

namespace function
{
    template< typename R, typename Parms, typename FunPtr >
    base_fun_impl* create( const FunPtr& ptr )
    {
        return new fun_holder< R, Parms, FunPtr >( ptr );
    }
}

//////////////////////////////////////////////////////////////////////////

//

// command_list

//

//////////////////////////////////////////////////////////////////////////


class command_list
{
    typedef boost::shared_ptr< base_fun_impl > ptr_type;
    typedef std::map< std::string, ptr_type > list_type;

public:
    template< typename R, typename Parms, typename FunPtr >
    bool register_( const std::string& name, FunPtr fun )
    {
        return list.insert( 
            list_type::value_type( name, ptr_type( new fun_holder< R, Parms, FunPtr >( fun ) ) ) ).second;
    }

    bool unregister_( const std::string& name )
    {
        return list.erase( name ) != 0;
    }

    template< typename R >
    R call( const std::string& name )
    {
        return find( name )->call< R >();
    }

    template< typename R, typename P1 >
    R call( const std::string& name, P1 p1 )
    {
        return find( name )->call< R >( p1 );
    }

    template< typename R, typename P1, typename P2 >
    R call( const std::string& name, P1 p1, P2 p2 )
    {
        return find( name )->call< R >( p1, p2 );
    }

    template< typename R, typename P1, typename P2, typename P3 >
    R call( const std::string& name, P1 p1, P2 p2, P3 p3 )
    {
        return find( name )->call< R >( p1, p2, p3 );
    }

private:
    ptr_type& find( const std::string& name )
    {
        list_type::iterator it = list.find( name );

        if ( it == list.end() )
        {
            throw std::logic_error( "unregistered type" );
        }

        return it->second;
    }
private:
    list_type list;
};

struct foo
{
    static void bar( int i )
    {
        std::cout << "passed: " << i << std::endl;
    }

    static int add( int a, int b )
    {
        return a + b;
    }
};

int main()
{
    command_list list;

    list.register_< void, Sequence< int > >( "bar", &foo::bar );
    list.register_< int, Sequence< int, int > >( "add", &foo::add );

    try
    {
        list.call< void >( "bar", 10 );
        std::cout << list.call< int >( "add", 10, 20 );
    }
    catch ( std::logic_error& e )
    {
        std::cout << "Error: " << e.what() << std::endl;
    }

    std::cin.get();
}
@D13_Dreinig

grek40

Alter Hase

Beiträge: 1 491

Wohnort: Dresden

  • Private Nachricht senden

17

18.07.2007, 01:14

Respect :shock:
Ich hab zwar grad kein entsprechendes Problem parat, aber bei Gelegenheit zieh ich mir den Code auch ma rein... da haste sicher einige graue Zellen rein investiert ;)

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

18

18.07.2007, 11:39

Mir is noch was leichteres eingefallen. Ist aber mit Sicherheit noch aufwertbar. :)

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
102
103
104
#include <iostream>
#include <map>

struct simple_functor
{
private:
    struct base_holder
    {
        virtual ~base_holder() {}
        virtual base_holder* clone() const = 0;
    };

    template< typename FunPtr >
    struct holder : public base_holder
    {
        holder( FunPtr ptr )
            : fun( ptr )
        {}

        operator FunPtr() const
        {
            return fun;
        }

        base_holder* clone() const
        {
            return new holder< FunPtr >( fun );
        }

    private:
        FunPtr fun;
    };

public:
    simple_functor()
        : content( 0 )
    {}

    simple_functor( const simple_functor& rhs )
        : content( rhs.content->clone() )
    {}

    template< typename FunPtr >
    simple_functor( FunPtr fun )
        : content( new holder< FunPtr >( fun ) )
    {}

    ~simple_functor()
    {
        delete content;
    }

    template< typename R >
    R call() const
    {
        return ( *safe_cast< R (*)() >( content ) )();  
    }

    template< typename R, typename P1 >
    R call( P1 p1 ) const
    {
        return ( *safe_cast< R (*)( P1 ) >( content ) )( p1 );
    }

    template< typename R, typename P1, typename P2 >
    R call( P1 p1, P2 p2 ) const
    {
        return ( *safe_cast< R (*)( P1, P2 ) >( content ) )( p1, p2 );
    }

private:
    template< typename FunPtr >
    holder< FunPtr >* safe_cast( base_holder* ptr ) const
    {
        return ( ptr ? static_cast< holder< FunPtr >* >( ptr ) : 0 );
    }

private:
    base_holder* content;
};

struct bar
{
    static void test()
    {
        std::cout << "Hallo Welt" << std::endl;
    }

    static int blubb( int x )
    {
        return x * 10;
    }
};

int main()
{
    std::map< int, simple_functor > map;

    map.insert( std::map< int, simple_functor >::value_type( 0, simple_functor( &bar::test ) ) );
    map.insert( std::map< int, simple_functor >::value_type( 1, simple_functor( &bar::blubb ) ) );

    map[ 0 ].call< void >();
    std::cout << map[ 1 ].call< int >( 20 );
}
@D13_Dreinig

19

18.07.2007, 20:02

das sieht super aus :)

und das unterstützt jetzt funktionen mit bis zu 2 argumenten, wenn ihc mehr haben will muss ich einfach noch ein paar templates hinzufügen richtig?

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

20

18.07.2007, 20:05

Richtig!
@D13_Dreinig

Werbeanzeige