]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/parammode.h
Add ParamModeBase and ParamMode, change all parameter modes to inherit from ParamMode
[user/henk/code/inspircd.git] / include / parammode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 class CoreExport ParamModeBase : public ModeHandler
23 {
24  private:
25         virtual void OnUnsetInternal(User* source, Channel* chan) = 0;
26
27  public:
28         ParamModeBase(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps)
29                 : ModeHandler(Creator, Name, modeletter, ps, MODETYPE_CHANNEL, MC_PARAM) { }
30
31         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE;
32
33         // Does nothing by default
34         void OnUnset(User* source, Channel* chan) { }
35         virtual ModeAction OnSet(User* source, Channel* chan, std::string& param) = 0;
36         virtual void GetParameter(Channel* chan, std::string& out) = 0;
37 };
38
39 /** Defines a parameter mode
40  * T = Child class
41  * ExtItemT = Type of the extension item used to store the parameter
42  *
43  * When unsetting the mode, the extension is automatically unset.
44  */
45 template <typename T, typename ExtItemT>
46 class ParamMode : public ParamModeBase
47 {
48  public:
49         ExtItemT ext;
50
51         /**
52          * @param Creator Module handling this mode
53          * @param Name The internal name of this mode
54          * @param modeletter The mode letter of this mode
55          * @param ps The parameter type of this mode, one of ParamSpec
56          */
57         ParamMode(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps = PARAM_SETONLY)
58                 : ParamModeBase(Creator, Name, modeletter, ps)
59                 , ext("parammode_" + Name, Creator)
60         {
61         }
62
63         void OnUnsetInternal(User* source, Channel* chan) CXX11_OVERRIDE
64         {
65                 T* mh = static_cast<T*>(this);
66                 mh->OnUnset(source, chan);
67                 ext.unset(chan);
68         }
69
70         void GetParameter(Channel* chan, std::string& out) CXX11_OVERRIDE
71         {
72                 T* mh = static_cast<T*>(this);
73                 mh->SerializeParam(chan, ext.get(chan), out);
74         }
75 };