]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/convto.h
Add some overloads of IRCv3::Replies::Reply#Send.
[user/henk/code/inspircd.git] / include / convto.h
index c306283fc7e85a4333164239a22c51db479cf15d..2f808d1fb3a722128a8047f8e6ca1973016a8fcf 100644 (file)
@@ -89,17 +89,8 @@ template <class T> inline std::string ConvToStr(const T& in)
        return tmp.str();
 }
 
-/** Template function to convert any input type to any other type
- * (usually an integer or numeric type)
+/** Template function to convert a std::string to any numeric type.
  */
-template<typename T> inline long ConvToInt(const T& in)
-{
-       std::stringstream tmp;
-       if (!(tmp << in))
-               return 0;
-       return atol(tmp.str().c_str());
-}
-
 template<typename TOut> inline TOut ConvToNum(const std::string& in)
 {
        TOut ret;
@@ -108,3 +99,19 @@ template<typename TOut> inline TOut ConvToNum(const std::string& in)
                return 0;
        return ret;
 }
+
+template<> inline char ConvToNum<char>(const std::string& in)
+{
+       // We specialise ConvToNum for char to avoid istringstream treating
+       // the input as a character literal.
+       uint16_t num = ConvToNum<uint16_t>(in);
+       return num <= UINT8_MAX ? num : 0;
+}
+
+template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
+{
+       // We specialise ConvToNum for unsigned char to avoid istringstream
+       // treating the input as a character literal.
+       int16_t num = ConvToNum<int16_t>(in);
+       return num >= INT8_MIN && num <= INT8_MAX ? num : 0;
+}