diff options
Diffstat (limited to 'include/anope.h')
-rw-r--r-- | include/anope.h | 164 |
1 files changed, 124 insertions, 40 deletions
diff --git a/include/anope.h b/include/anope.h index 48a4f7674..49e7c7ddc 100644 --- a/include/anope.h +++ b/include/anope.h @@ -1,16 +1,23 @@ /* + * Anope IRC Services * - * (C) 2003-2016 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2012-2016 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ -#ifndef ANOPE_H -#define ANOPE_H +#pragma once #include <signal.h> @@ -308,33 +315,40 @@ namespace Anope inline const string operator+(const char *_str, const string &str) { string tmp(_str); tmp += str; return tmp; } inline const string operator+(const std::string &_str, const string &str) { string tmp(_str); tmp += str; return tmp; } + struct hash + { + size_t operator()(const string &s) const; + }; + + struct compare + { + bool operator()(const string &s1, const string &s2) const; + }; + struct hash_ci { - inline size_t operator()(const string &s) const - { - return TR1NS::hash<std::string>()(s.lower().str()); - } + size_t operator()(const string &s) const; }; - struct hash_cs + struct compare_ci { - inline size_t operator()(const string &s) const - { - return TR1NS::hash<std::string>()(s.str()); - } + bool operator()(const string &s1, const string &s2) const; }; - struct compare + struct hash_locale { - inline bool operator()(const string &s1, const string &s2) const - { - return s1.equals_ci(s2); - } + size_t operator()(const string &s) const; }; - template<typename T> class map : public std::map<string, T, ci::less> { }; - template<typename T> class multimap : public std::multimap<string, T, ci::less> { }; - template<typename T> class hash_map : public TR1NS::unordered_map<string, T, hash_ci, compare> { }; + struct compare_locale + { + bool operator()(const string &s1, const string &s2) const; + }; + + template<typename T> using map = std::map<string, T, ci::less>; + template<typename T> using multimap = std::multimap<string, T, ci::less>; + template<typename T> using hash_map = std::unordered_map<string, T, hash_ci, compare_ci>; + template<typename T> using locale_hash_map = std::unordered_map<string, T, hash_locale, compare_locale>; #ifndef REPRODUCIBLE_BUILD static const char *const compiled = __TIME__ " " __DATE__; @@ -366,7 +380,7 @@ namespace Anope */ extern CoreExport bool ReadOnly, NoFork, NoThird, NoExpire, ProtocolDebug; - /** The root of the services installation. Usually ~/services + /** The root of the services installation. Usually ~/anope */ extern CoreExport Anope::string ServicesDir; @@ -443,13 +457,13 @@ namespace Anope */ extern CoreExport void Unhex(const string &src, string &dest); extern CoreExport void Unhex(const string &src, char *dest, size_t sz); - + /** Base 64 encode a string * @param src The string to encode * @param target Where the encoded string is placed */ extern CoreExport void B64Encode(const string &src, string &target); - + /** Base 64 decode a string * @param src The base64 encoded string * @param target The plain text result @@ -462,14 +476,6 @@ namespace Anope */ extern CoreExport void Encrypt(const Anope::string &src, Anope::string &dest); - /** Decrypts what is in 'src' to 'dest'. - * @param src The source string to decrypt - * @param dest The destination where the decrypted string is placed - * @return true if decryption was successful. This is usually not the case - * as most encryption methods we use are one way. - */ - extern CoreExport bool Decrypt(const Anope::string &src, Anope::string &dest); - /** Returns a sequence of data formatted as the format argument specifies. ** After the format parameter, the function expects at least as many ** additional arguments as specified in format. @@ -504,21 +510,21 @@ namespace Anope * @param nc The account to use language settings for to translate this string, if applicable * @return A human readable string, eg "1 minute" */ - extern CoreExport Anope::string Duration(time_t seconds, const NickCore *nc = NULL); + extern CoreExport Anope::string Duration(time_t seconds, NickServ::Account *nc = NULL); /** Generates a human readable string of type "expires in ..." * @param expires time in seconds * @param nc The account to use language settings for to translate this string, if applicable * @return A human readable string, eg "expires in 5 days" */ - extern CoreExport Anope::string Expires(time_t seconds, const NickCore *nc = NULL); + extern CoreExport Anope::string Expires(time_t seconds, NickServ::Account *nc = NULL); /** Converts a time in seconds (epoch) to a human readable format. * @param t The time * @param nc The account to use language settings for to translate this string, if applicable * @param short_output If true, the output is just a date (eg, "Apr 12 20:18:22 2009 MSD"), else it includes the date and how long ago/from now that date is, (eg "Apr 12 20:18:22 2009 MSD (1313 days, 9 hours, 32 minutes ago)" */ - extern CoreExport Anope::string strftime(time_t t, const NickCore *nc = NULL, bool short_output = false); + extern CoreExport Anope::string strftime(time_t t, NickServ::Account *nc = NULL, bool short_output = false); /** Normalize buffer, stripping control characters and colors * @param A string to be parsed for control and color codes @@ -633,7 +639,7 @@ class spacesepstream : public sepstream public: /** Initialize with space seperator */ - spacesepstream(const Anope::string &source) : sepstream(source, ' ') { } + spacesepstream(const Anope::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty) { } }; /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception. @@ -706,6 +712,22 @@ class ConvertException : public CoreException virtual ~ConvertException() throw() { } }; +template<typename T> +typename std::enable_if<std::is_enum<T>::value, std::ostream &>::type +operator<<(std::ostream &os, T t) +{ + // XXX + return os; +} + +template<typename T> +typename std::enable_if<std::is_enum<T>::value, std::istream &>::type +operator>>(std::istream &is, T& t) +{ + // XXX + return is; +} + /** Convert something to a string */ template<typename T> inline Anope::string stringify(const T &x) @@ -778,4 +800,66 @@ template<typename T, typename O> inline T anope_dynamic_static_cast(O ptr) } #endif -#endif // ANOPE_H +struct kwarg +{ + Anope::string name, value; + + template<typename T> + kwarg& operator=(const T& rhs) + { + value = stringify(rhs); + return *this; + } +}; + +inline kwarg operator"" _kw(const char *literal, size_t n) +{ + return { literal }; +} + +struct FormatInfo +{ + Anope::string format; + std::vector<kwarg> parameters; + unsigned int pos = 0; + + FormatInfo(const Anope::string &fmt, size_t size) : format(fmt), parameters(size) { } + + template<typename T> + void Add(T& arg) + { + parameters[pos] = kwarg{ stringify(pos).c_str() } = stringify(arg); + ++pos; + } + + template<typename Arg, typename... Args> + void Format(Arg &&arg, Args&&... args) + { + Add(arg); + Format(std::forward<Args>(args)...); + } + + void Format() + { + for (kwarg& arg : parameters) + format = format.replace_all_cs("{" + arg.name + "}", arg.value); + } +}; + +template<> +inline void FormatInfo::Add(kwarg &arg) +{ + parameters[pos++] = arg; +} + +namespace Anope +{ + template<typename... Args> + inline Anope::string Format(const Anope::string &format, Args&&... args) + { + FormatInfo fi(format, sizeof...(Args)); + fi.Format(std::forward<Args>(args)...); + return fi.format; + } +} + |