]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dynref.h
Seperate ModeReference into ChanModeReference and UserModeReference
[user/henk/code/inspircd.git] / include / dynref.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #pragma once
22
23 #include "base.h"
24
25 class CoreExport dynamic_reference_base : public interfacebase
26 {
27  private:
28         std::string name;
29         void resolve();
30  protected:
31         ServiceProvider* value;
32  public:
33         ModuleRef creator;
34         dynamic_reference_base(Module* Creator, const std::string& Name);
35         ~dynamic_reference_base();
36         inline const std::string& GetProvider() { return name; }
37         void SetProvider(const std::string& newname);
38         void check();
39         operator bool() { return (value != NULL); }
40         static void reset_all();
41 };
42
43 inline void dynamic_reference_base::check()
44 {
45         if (!value)
46                 throw ModuleException("Dynamic reference to '" + name + "' failed to resolve");
47 }
48
49 template<typename T>
50 class dynamic_reference : public dynamic_reference_base
51 {
52  public:
53         dynamic_reference(Module* Creator, const std::string& Name)
54                 : dynamic_reference_base(Creator, Name) {}
55
56         inline T* operator->()
57         {
58                 check();
59                 return static_cast<T*>(value);
60         }
61
62         T* operator*()
63         {
64                 return operator->();
65         }
66 };
67
68 template<typename T>
69 class dynamic_reference_nocheck : public dynamic_reference_base
70 {
71  public:
72         dynamic_reference_nocheck(Module* Creator, const std::string& Name)
73                 : dynamic_reference_base(Creator, Name) {}
74
75         T* operator->()
76         {
77                 return static_cast<T*>(value);
78         }
79
80         T* operator*()
81         {
82                 return operator->();
83         }
84 };
85
86 class ModeHandler;
87 class ChanModeReference : public dynamic_reference_nocheck<ModeHandler>
88 {
89  public:
90         ChanModeReference(Module* mod, const std::string& modename)
91                 : dynamic_reference_nocheck<ModeHandler>(mod, "mode/" + modename) {}
92 };
93
94 class UserModeReference : public dynamic_reference_nocheck<ModeHandler>
95 {
96  public:
97         UserModeReference(Module* mod, const std::string& modename)
98                 : dynamic_reference_nocheck<ModeHandler>(mod, "umode/" + modename) {}
99 };