]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/numericbuilder.h
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[user/henk/code/inspircd.git] / include / numericbuilder.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 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 namespace Numeric
23 {
24         class WriteNumericSink;
25
26         template <char Sep, bool SendEmpty, typename Sink>
27         class GenericBuilder;
28
29         template <char Sep = ',', bool SendEmpty = false>
30         class Builder;
31 }
32
33 class Numeric::WriteNumericSink
34 {
35         LocalUser* const user;
36
37  public:
38         WriteNumericSink(LocalUser* u)
39                 : user(u)
40         {
41         }
42
43         void operator()(Numeric& numeric) const
44         {
45                 user->WriteNumeric(numeric);
46         }
47 };
48
49 template <char Sep, bool SendEmpty, typename Sink>
50 class Numeric::GenericBuilder
51 {
52         Sink sink;
53         Numeric numeric;
54         const std::string::size_type max;
55
56         bool HasRoom(const std::string::size_type additional) const
57         {
58                 return (numeric.GetParams().back().size() + additional <= max);
59         }
60
61  public:
62         GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
63                 : sink(s)
64                 , numeric(num)
65                 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 9)
66         {
67                 if (addparam)
68                         numeric.push(std::string());
69         }
70
71         Numeric& GetNumeric() { return numeric; }
72
73         void Add(const std::string& entry)
74         {
75                 if (!HasRoom(entry.size()))
76                         Flush();
77                 numeric.GetParams().back().append(entry).push_back(Sep);
78         }
79
80         void Add(const std::string& entry1, const std::string& entry2)
81         {
82                 if (!HasRoom(entry1.size() + entry2.size()))
83                         Flush();
84                 numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
85         }
86
87         void Flush()
88         {
89                 std::string& data = numeric.GetParams().back();
90                 if (IsEmpty())
91                 {
92                         if (!SendEmpty)
93                                 return;
94                 }
95                 else
96                 {
97                         data.erase(data.size()-1);
98                 }
99
100                 sink(numeric);
101                 data.clear();
102         }
103
104         bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
105 };
106
107 template <char Sep, bool SendEmpty>
108 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
109 {
110  public:
111         Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
112                 : ::Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())
113         {
114         }
115 };