diff options
author | Attila Molnar <attilamolnar@hush.com> | 2016-03-22 16:40:05 +0100 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2016-03-22 16:40:05 +0100 |
commit | 3aa92b8d13f57b228d565df23cdad4c720d5cff0 (patch) | |
tree | ceb10ecb0d137acf991e512a8bcc312eb09828e4 | |
parent | 266eb73475303ed6d48add31eec95700923e2146 (diff) |
Add Numeric::ParamBuilder
It builds numerics where the individual components are parameters of the numeric
-rw-r--r-- | include/numericbuilder.h | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/include/numericbuilder.h b/include/numericbuilder.h index a7b83125f..cd418ea11 100644 --- a/include/numericbuilder.h +++ b/include/numericbuilder.h @@ -1,7 +1,7 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com> + * Copyright (C) 2015-2016 Attila Molnar <attilamolnar@hush.com> * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -28,6 +28,12 @@ namespace Numeric template <char Sep = ',', bool SendEmpty = false> class Builder; + + template <unsigned int NumStaticParams, bool SendEmpty, typename Sink> + class GenericParamBuilder; + + template <unsigned int NumStaticParams, bool SendEmpty = false> + class ParamBuilder; } class Numeric::WriteNumericSink @@ -113,3 +119,63 @@ class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink> { } }; + +template <unsigned int NumStaticParams, bool SendEmpty, typename Sink> +class Numeric::GenericParamBuilder +{ + Sink sink; + Numeric numeric; + std::string::size_type currlen; + std::string::size_type max; + + bool HasRoom(const std::string::size_type additional) const + { + return (currlen + additional <= max); + } + + public: + GenericParamBuilder(Sink s, unsigned int num, size_t additionalsize) + : sink(s) + , numeric(num) + , currlen(0) + , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10) + { + } + + void AddStatic(const std::string& entry) + { + max -= (entry.length() + 1); + numeric.GetParams().push_back(entry); + } + + void Add(const std::string& entry) + { + if (!HasRoom(entry.size())) + Flush(); + + currlen += entry.size() + 1; + numeric.GetParams().push_back(entry); + } + + void Flush() + { + if ((!SendEmpty) && (IsEmpty())) + return; + + sink(numeric); + currlen = 0; + numeric.GetParams().erase(numeric.GetParams().begin() + NumStaticParams, numeric.GetParams().end()); + } + + bool IsEmpty() const { return (numeric.GetParams().size() <= NumStaticParams); } +}; + +template <unsigned int NumStaticParams, bool SendEmpty> +class Numeric::ParamBuilder : public GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink> +{ + public: + ParamBuilder(LocalUser* user, unsigned int num) + : ::Numeric::GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, user->nick.size()) + { + } +}; |