Subversion Repositories gelsvn

Rev

Rev 561 | Rev 601 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 561 Rev 563
Line 40... Line 40...
40
    void executef(const char* format, ...);
40
    void executef(const char* format, ...);
41
 
41
 
42
    typedef int cmd_token;
42
    typedef int cmd_token;
43
 
43
 
44
    //0-ary
44
    //0-ary
45
    inline cmd_token reg_cmd(const std::string& name,
45
    inline cmd_token reg_cmd0(const std::string& name,
46
                             const std::function<void ()>& f,
46
                              const std::function<void ()>& f,
47
                             const std::string& help);
47
                              const std::string& help);
48
 
48
 
49
    //1-ary
49
    //1-ary
50
    template <typename A0>
50
    template <typename A0>
51
    cmd_token reg_cmd(const std::string& name,
51
    cmd_token reg_cmd1(const std::string& name,
52
                      const std::function<void (const A0&)>& f,
52
                       const std::function<void (const A0&)>& f,
53
                      const std::string& help);
53
                       const std::string& help);
54
 
54
 
55
    //2-ary
55
    //2-ary
56
    template <typename A0, typename A1>
56
    template <typename A0, typename A1>
57
    cmd_token reg_cmd(const std::string& name,
57
    cmd_token reg_cmd2(const std::string& name,
58
                      const std::function<void (const A0&,const A1&)>& f,
58
                       const std::function<void (const A0&,const A1&)>& f,
59
                      const std::string& help);
59
                       const std::string& help);
60
 
60
 
61
    //3-ary
61
    //3-ary
62
    template <typename A0, typename A1, typename A2>
62
    template <typename A0, typename A1, typename A2>
63
    cmd_token reg_cmd(const std::string& name,
63
    cmd_token reg_cmd3(const std::string& name,
64
                      const std::function<void (const A0&,
64
                       const std::function<void (const A0&,
65
                                                const A1&,const A2&)>& f,
65
                                                 const A1&,const A2&)>& f,
66
                      const std::string& help);
66
                       const std::string& help);
-
 
67
 
-
 
68
    //N-ary
-
 
69
    inline cmd_token reg_cmdN(const std::string& name,
-
 
70
                              const std::function<
-
 
71
                                void (const std::vector<std::string>&)>& f,
-
 
72
                              const std::string& help);
67
 
73
 
68
    //remove
74
    //remove
69
    inline void unreg_cmd(cmd_token);
75
    inline void unreg_cmd(cmd_token);
70
 
76
 
71
    //get name/help of registrered command
77
    //get name/help of registrered command
Line 211... Line 217...
211
 
217
 
212
    private:
218
    private:
213
        function_type m_callback;
219
        function_type m_callback;
214
    };
220
    };
215
 
221
 
-
 
222
    //no need to be a template
-
 
223
    class commandN : public command_base
-
 
224
    {
-
 
225
    public:
-
 
226
        typedef std::function<
-
 
227
            void (const std::vector<std::string>&)> function_type;
-
 
228
 
-
 
229
        inline commandN(Console& c,
-
 
230
                        const std::string& h, const function_type& f)
-
 
231
            : command_base(c,h), m_callback(f) {}
-
 
232
 
-
 
233
        inline void execute(const std::vector<std::string>& args) const;
-
 
234
        inline size_t arity() const { return any_arity; }
-
 
235
 
-
 
236
    private:
-
 
237
        function_type m_callback;
-
 
238
    };
-
 
239
 
216
    //multi, so we can have multiple cmds with same name (but different arity)
240
    //multi, so we can have multiple cmds with same name (but different arity)
217
    typedef std::multimap<std::string,
241
    typedef std::multimap<std::string,
218
                          std::unique_ptr<command_base> > command_map_t;
242
                          std::unique_ptr<command_base> > command_map_t;
219
 
243
 
220
    cmd_token add_command(const std::string& name,
244
    cmd_token add_command(const std::string& name,
Line 233... Line 257...
233
 
257
 
234
    void load_history();
258
    void load_history();
235
    void save_history() const;
259
    void save_history() const;
236
    void clear_history();
260
    void clear_history();
237
 
261
 
-
 
262
    enum { any_arity = 0xFFFF };
-
 
263
 
238
    //draw commands
264
    //draw commands
239
    void draw_text(int x, int y,
265
    void draw_text(int x, int y,
240
                   float r, float g, float b,
266
                   float r, float g, float b,
241
                   const char* buffer);
267
                   const char* buffer);
242
 
268
 
Line 261... Line 287...
261
    static const unsigned char g_png_data[];
287
    static const unsigned char g_png_data[];
262
    static const size_t g_png_size;
288
    static const size_t g_png_size;
263
};
289
};
264
 
290
 
265
//0-ary
291
//0-ary
266
Console::cmd_token Console::reg_cmd(const std::string& name,
292
Console::cmd_token Console::reg_cmd0(const std::string& name,
267
                                    const std::function<void ()>& f,
293
                                     const std::function<void ()>& f,
268
                                    const std::string& help)
294
                                     const std::string& help)
269
{
295
{
270
    typedef command0 command_type;
296
    typedef command0 command_type;
271
    return add_command(name,
297
    return add_command(name,
272
        std::unique_ptr<command_type>(
298
        std::unique_ptr<command_type>(
273
            new command_type(std::ref(*this), help, f)));
299
            new command_type(std::ref(*this), help, f)));
Line 279... Line 305...
279
    m_callback();
305
    m_callback();
280
}
306
}
281
 
307
 
282
//1-ary
308
//1-ary
283
template <typename A0>
309
template <typename A0>
284
Console::cmd_token Console::reg_cmd(const std::string& name,
310
Console::cmd_token Console::reg_cmd1(const std::string& name,
285
                                    const std::function<void (const A0&)>& f,
311
                                     const std::function<void (const A0&)>& f,
286
                                    const std::string& help)
312
                                     const std::string& help)
287
{
313
{
288
    typedef command1<A0> command_type;
314
    typedef command1<A0> command_type;
289
    return add_command(name,
315
    return add_command(name,
290
        std::unique_ptr<command_type>(
316
        std::unique_ptr<command_type>(
291
            new command_type(std::ref(*this), help, f)));
317
            new command_type(std::ref(*this), help, f)));
Line 298... Line 324...
298
    m_callback(lexical_cast<A0>(args[0]));
324
    m_callback(lexical_cast<A0>(args[0]));
299
}
325
}
300
 
326
 
301
//2-ary
327
//2-ary
302
template <typename A0, typename A1>
328
template <typename A0, typename A1>
303
Console::cmd_token Console::reg_cmd(const std::string& name,
329
Console::cmd_token Console::reg_cmd2(const std::string& name,
304
                                    const std::function<void (const A0&,
330
                                     const std::function<void (const A0&,
305
                                      const A1&)>& f,
331
                                       const A1&)>& f,
306
                                    const std::string& help)
332
                                     const std::string& help)
307
{
333
{
308
    typedef command2<A0,A1> command_type;
334
    typedef command2<A0,A1> command_type;
309
    return add_command(name,
335
    return add_command(name,
310
        std::unique_ptr<command_type>(
336
        std::unique_ptr<command_type>(
311
            new command_type(std::ref(*this), help, f)));
337
            new command_type(std::ref(*this), help, f)));
Line 320... Line 346...
320
               lexical_cast<A1>(args[1]));
346
               lexical_cast<A1>(args[1]));
321
}
347
}
322
 
348
 
323
//3-ary
349
//3-ary
324
template <typename A0, typename A1, typename A2>
350
template <typename A0, typename A1, typename A2>
325
Console::cmd_token Console::reg_cmd(const std::string& name,
351
Console::cmd_token Console::reg_cmd3(const std::string& name,
326
                                    const std::function<void (const A0&,
352
                                     const std::function<void (const A0&,
327
                                      const A1&,const A2&)>& f,
353
                                       const A1&,const A2&)>& f,
328
                                    const std::string& help)
354
                                     const std::string& help)
329
{
355
{
330
    typedef command3<A0,A1,A2> command_type;
356
    typedef command3<A0,A1,A2> command_type;
331
    return add_command(name,
357
    return add_command(name,
332
        std::unique_ptr<command_type>(
358
        std::unique_ptr<command_type>(
333
            new command_type(std::ref(*this), help, f)));
359
            new command_type(std::ref(*this), help, f)));
Line 341... Line 367...
341
    m_callback(lexical_cast<A0>(args[0]),
367
    m_callback(lexical_cast<A0>(args[0]),
342
               lexical_cast<A1>(args[1]),
368
               lexical_cast<A1>(args[1]),
343
               lexical_cast<A2>(args[2]));
369
               lexical_cast<A2>(args[2]));
344
}
370
}
345
 
371
 
-
 
372
//N-ary
-
 
373
Console::cmd_token Console::reg_cmdN(const std::string& name,
-
 
374
    const std::function<void (const std::vector<std::string>&)>& f,
-
 
375
    const std::string& help)
-
 
376
{
-
 
377
    typedef commandN command_type;
-
 
378
    return add_command(name,
-
 
379
        std::unique_ptr<command_type>(
-
 
380
            new command_type(std::ref(*this), help, f)));
-
 
381
}
-
 
382
 
-
 
383
void Console::commandN::execute(const std::vector<std::string>& args) const
-
 
384
{
-
 
385
    m_callback(args);
-
 
386
}
-
 
387
 
346
void Console::unreg_cmd(cmd_token id)
388
void Console::unreg_cmd(cmd_token id)
347
{
389
{
348
    remove_command(id);
390
    remove_command(id);
349
}
391
}
350
 
392
 
Line 358... Line 400...
358
        const std::function<void ()>& function,
400
        const std::function<void ()>& function,
359
        const std::string& help)
401
        const std::string& help)
360
    {
402
    {
361
        assert(!m_console);
403
        assert(!m_console);
362
        m_console = &cs;
404
        m_console = &cs;
363
        m_id = m_console->reg_cmd(name, function, help);
405
        m_id = m_console->reg_cmd0(name, function, help);
364
    }
406
    }
365
 
407
 
366
    template <typename A0>
408
    template <typename A0>
367
    void reg(Console& cs,
409
    void reg(Console& cs,
368
        const std::string& name,
410
        const std::string& name,
369
        const std::function<void (const A0&)>& function,
411
        const std::function<void (const A0&)>& function,
370
        const std::string& help)
412
        const std::string& help)
371
    {
413
    {
372
        assert(!m_console);
414
        assert(!m_console);
373
        m_console = &cs;
415
        m_console = &cs;
374
        m_id = m_console->reg_cmd<A0>(name, function, help);
416
        m_id = m_console->reg_cmd1<A0>(name, function, help);
375
    }
417
    }
376
 
418
 
377
    template <typename A0, typename A1>
419
    template <typename A0, typename A1>
378
    void reg(Console& cs,
420
    void reg(Console& cs,
379
        const std::string& name,
421
        const std::string& name,
380
        const std::function<void (const A0&, const A1&)>& function,
422
        const std::function<void (const A0&, const A1&)>& function,
381
        const std::string& help)
423
        const std::string& help)
382
    {
424
    {
383
        assert(!m_console);
425
        assert(!m_console);
384
        m_console = &cs;
426
        m_console = &cs;
385
        m_id = m_console->reg_cmd<A0,A1>(name, function, help);
427
        m_id = m_console->reg_cmd2<A0,A1>(name, function, help);
386
    }
428
    }
387
 
429
 
388
    template <typename A0, typename A1, typename A2>
430
    template <typename A0, typename A1, typename A2>
389
    void reg(Console& cs,
431
    void reg(Console& cs,
390
        const std::string& name,
432
        const std::string& name,
Line 392... Line 434...
392
        const A1&, const A2&)>& function,
434
        const A1&, const A2&)>& function,
393
        const std::string& help)
435
        const std::string& help)
394
    {
436
    {
395
        assert(!m_console);
437
        assert(!m_console);
396
        m_console = &cs;
438
        m_console = &cs;
397
        m_id = m_console->reg_cmd<A0,A1,A2>(name, function, help);
439
        m_id = m_console->reg_cmd3<A0,A1,A2>(name, function, help);
398
    }
440
    }
399
 
441
 
400
    inline ~command()
442
    inline ~command()
401
    {
443
    {
402
        if (m_console)
444
        if (m_console)