InspIRCd  3.0
numericbuilder.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2015-2016 Attila Molnar <[email protected]>
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  class WriteRemoteNumericSink;
26 
27  template <char Sep, bool SendEmpty, typename Sink>
29 
30  template <char Sep = ',', bool SendEmpty = false>
31  class Builder;
32 
33  template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
35 
36  template <unsigned int NumStaticParams, bool SendEmpty = false>
37  class ParamBuilder;
38 }
39 
41 {
42  LocalUser* const user;
43 
44  public:
46  : user(u)
47  {
48  }
49 
50  void operator()(Numeric& numeric) const
51  {
52  user->WriteNumeric(numeric);
53  }
54 };
55 
57 {
58  User* const user;
59 
60  public:
62  : user(u)
63  {
64  }
65 
66  void operator()(Numeric& numeric) const
67  {
68  user->WriteRemoteNumeric(numeric);
69  }
70 };
71 
72 template <char Sep, bool SendEmpty, typename Sink>
74 {
75  Sink sink;
76  Numeric numeric;
77  const std::string::size_type max;
78 
79  bool HasRoom(const std::string::size_type additional) const
80  {
81  return (numeric.GetParams().back().size() + additional <= max);
82  }
83 
84  public:
85  GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
86  : sink(s)
87  , numeric(num)
88  , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
89  {
90  if (addparam)
91  numeric.push(std::string());
92  }
93 
94  Numeric& GetNumeric() { return numeric; }
95 
96  void Add(const std::string& entry)
97  {
98  if (!HasRoom(entry.size()))
99  Flush();
100  numeric.GetParams().back().append(entry).push_back(Sep);
101  }
102 
103  void Add(const std::string& entry1, const std::string& entry2)
104  {
105  if (!HasRoom(entry1.size() + entry2.size()))
106  Flush();
107  numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
108  }
109 
110  void Flush()
111  {
112  std::string& data = numeric.GetParams().back();
113  if (IsEmpty())
114  {
115  if (!SendEmpty)
116  return;
117  }
118  else
119  {
120  data.erase(data.size()-1);
121  }
122 
123  sink(numeric);
124  data.clear();
125  }
126 
127  bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
128 };
129 
130 template <char Sep, bool SendEmpty>
131 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
132 {
133  public:
134  Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
135  : ::Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())
136  {
137  }
138 };
139 
140 template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
142 {
143  Sink sink;
144  Numeric numeric;
145  std::string::size_type currlen;
146  std::string::size_type max;
147 
148  bool HasRoom(const std::string::size_type additional) const
149  {
150  return (currlen + additional <= max);
151  }
152 
153  public:
154  GenericParamBuilder(Sink s, unsigned int num, size_t additionalsize)
155  : sink(s)
156  , numeric(num)
157  , currlen(0)
158  , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
159  {
160  }
161 
162  void AddStatic(const std::string& entry)
163  {
164  max -= (entry.length() + 1);
165  numeric.GetParams().push_back(entry);
166  }
167 
168  void Add(const std::string& entry)
169  {
170  if (!HasRoom(entry.size()))
171  Flush();
172 
173  currlen += entry.size() + 1;
174  numeric.GetParams().push_back(entry);
175  }
176 
177  void Flush()
178  {
179  if ((!SendEmpty) && (IsEmpty()))
180  return;
181 
182  sink(numeric);
183  currlen = 0;
184  numeric.GetParams().erase(numeric.GetParams().begin() + NumStaticParams, numeric.GetParams().end());
185  }
186 
187  bool IsEmpty() const { return (numeric.GetParams().size() <= NumStaticParams); }
188 };
189 
190 template <unsigned int NumStaticParams, bool SendEmpty>
191 class Numeric::ParamBuilder : public GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>
192 {
193  public:
194  ParamBuilder(LocalUser* user, unsigned int num)
196  {
197  }
198 };
Definition: numericbuilder.h:28
Definition: numericbuilder.h:37
Definition: numeric.h:24
ServerConfig * Config
Definition: inspircd.h:284
size_t MaxLine
Definition: configreader.h:116
Definition: users.h:224
Definition: users.h:742
Definition: numericbuilder.h:34
ServerLimits Limits
Definition: configreader.h:271
Definition: numericbuilder.h:31
std::string nick
Definition: users.h:298
Definition: numericbuilder.h:40
Definition: numericbuilder.h:56
std::string ServerName
Definition: configreader.h:293