osb/source/core/StarLexicalCast.hpp
Kai Blaschke 431a9c00a5
Fixed a huge amount of Clang warnings
On Linux and macOS, using Clang to compile OpenStarbound produces about 400 MB worth of warnings during the build, making the compiler output unreadable and slowing the build down considerably.

99% of the warnings were unqualified uses of std::move and std::forward, which are now all properly qualified.

Fixed a few other minor warnings about non-virtual destructors and some uses of std::move preventing copy elision on temporary objects.

Most remaining warnings are now unused parameters.
2024-02-19 16:55:19 +01:00

48 lines
1.1 KiB
C++

#ifndef STAR_LEXICAL_CAST_HPP
#define STAR_LEXICAL_CAST_HPP
#include "StarFormat.hpp"
#include "StarString.hpp"
#include "StarStringView.hpp"
#include "StarMaybe.hpp"
#include <sstream>
#include <locale>
namespace Star {
STAR_EXCEPTION(BadLexicalCast, StarException);
// Very simple basic lexical cast using stream input. Always operates in the
// "C" locale.
template <typename Type>
Maybe<Type> maybeLexicalCast(StringView s, std::ios_base::fmtflags flags = std::ios_base::boolalpha) {
Type result;
std::istringstream stream(std::string(s.utf8()));
stream.flags(flags);
stream.imbue(std::locale::classic());
if (!(stream >> result))
return {};
// Confirm that we read everything out of the stream
char ch;
if (stream >> ch)
return {};
return std::move(result);
}
template <typename Type>
Type lexicalCast(StringView s, std::ios_base::fmtflags flags = std::ios_base::boolalpha) {
auto m = maybeLexicalCast<Type>(s, flags);
if (m)
return m.take();
else
throw BadLexicalCast(strf("Lexical cast failed on '{}'", s));
}
}
#endif