2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
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.
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
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/>.
24 class WriteNumericSink;
26 template <char Sep, bool SendEmpty, typename Sink>
29 template <char Sep = ',', bool SendEmpty = false>
33 class Numeric::WriteNumericSink
35 LocalUser* const user;
38 WriteNumericSink(LocalUser* u)
43 void operator()(Numeric& numeric) const
45 user->WriteNumeric(numeric);
49 template <char Sep, bool SendEmpty, typename Sink>
50 class Numeric::GenericBuilder
54 const std::string::size_type max;
56 bool HasRoom(const std::string::size_type additional) const
58 return (numeric.GetParams().back().size() + additional <= max);
62 GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
65 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 9)
68 numeric.push(std::string());
71 Numeric& GetNumeric() { return numeric; }
73 void Add(const std::string& entry)
75 if (!HasRoom(entry.size()))
77 numeric.GetParams().back().append(entry).push_back(Sep);
80 void Add(const std::string& entry1, const std::string& entry2)
82 if (!HasRoom(entry1.size() + entry2.size()))
84 numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
89 std::string& data = numeric.GetParams().back();
97 data.erase(data.size()-1);
104 bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
107 template <char Sep, bool SendEmpty>
108 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
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())