aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/CommandRegistry.hh
blob: f3b6880f4692f39a82a9d9d5f9b6279f5e726a48 (plain)
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
// CommandRegistry.hh for FbTk
// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: $

#ifndef COMMANDREGISTRY_HH
#define COMMANDREGISTRY_HH

#include <string>
#include <map>

using std::string;

namespace FbTk {
class Command;
class BoolCommand;

#define REGISTER_COMMAND_PARSER(name, parser) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, parser); \
  }

#define REGISTER_BOOLCOMMAND_PARSER(name, parser) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerBoolCommand(#name, parser); \
  }

/* Include some basic command creators */
template <typename Cmd, typename Ret>
Ret *CommandCreator(const string &name, const string &args, bool trusted) {
    return new Cmd();
}

#define REGISTER_COMMAND(name, classname) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::CommandCreator<classname, FbTk::Command>); \
  }

template <typename Cmd, typename Ret>
Ret *CommandCreatorWithArgs(const string &name, const string &args,
                            bool trusted) {
    return new Cmd(args);
}

#define REGISTER_COMMAND_WITH_ARGS(name, classname) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::CommandCreatorWithArgs<classname, FbTk::Command>); \
  }

#define REGISTER_BOOLCOMMAND_WITH_ARGS(name, classname) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerBoolCommand(#name, FbTk::CommandCreatorWithArgs<classname, FbTk::BoolCommand>); \
  }

template <typename Cmd, typename Ret>
Ret *UntrustedCommandCreator(const string &name, const string &args,
                             bool trusted) {
    if (!trusted) return 0;
    return new Cmd();
}

#define REGISTER_UNTRUSTED_COMMAND(name, classname) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::UntrustedCommandCreator<classname, FbTk::Command>); \
  }

template <typename Cmd, typename Ret>
Ret * UntrustedCommandCreatorWithArgs(const string &name, const string &args,
                                      bool trusted) {
    if (!trusted) return 0;
    return new Cmd(args);
}

#define REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(name, classname) \
  namespace { \
    static const bool p_register_##name = FbTk::CommandRegistry::instance().registerCommand(#name, FbTk::UntrustedCommandCreatorWithArgs<classname, FbTk::Command>); \
  }

/* Not built to be virtual at the moment */
class CommandRegistry {
public:
    typedef Command * CreateFunction(const string &name, const string &args, bool trusted);
    typedef BoolCommand * BoolCreateFunction(const string &name,
                                             const string &args, bool trusted);
    typedef std::map<string, CreateFunction *> CreatorMap;
    typedef std::map<string, BoolCreateFunction *> BoolCreatorMap;

    static CommandRegistry &instance();

    Command *parseLine(const string &line, bool trusted = true) const;
    BoolCommand *parseBoolLine(const string &line, bool trusted = true) const;
    Command *getCommand(const string &name, const string &args, bool trusted) const;
    BoolCommand *getBoolCommand(const string &name, const string &args,
                                bool trusted) const;

    /* Note: the ownership of this object passes to this class */
    bool registerCommand(string name, CreateFunction createFunction);
    bool registerBoolCommand(string name, BoolCreateFunction bcf);

    const CreatorMap commandMap() const { return m_commandCreators; }

private:
    CommandRegistry() {}
    ~CommandRegistry() {}

    CreatorMap m_commandCreators;
    BoolCreatorMap m_boolcommandCreators;
};

}; // end namespace FbTk

#endif // COMMANDREGISTRY_HH