Use nullptr where appropriate; also simulate it for ancient compilers.

Since it's hard to find all places where using 0 as a pointer literal, only format.* & posix.* are replaced ( missing expected though )
This commit is contained in:
carterl 2015-02-13 04:19:56 +01:00
parent f1dd53de02
commit adeb46ab09
4 changed files with 48 additions and 22 deletions

View File

@ -147,7 +147,7 @@ typedef void (*FormatFunc)(fmt::Writer &, int, fmt::StringRef);
// Buffer should be at least of size 1. // Buffer should be at least of size 1.
int safe_strerror( int safe_strerror(
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT { int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT {
assert(buffer != 0 && buffer_size != 0); assert(buffer != nullptr && buffer_size != 0);
int result = 0; int result = 0;
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || __ANDROID__ #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || __ANDROID__
// XSI-compliant version of strerror_r. // XSI-compliant version of strerror_r.
@ -457,7 +457,7 @@ FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) {
FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
int length = MultiByteToWideChar( int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, 0, 0); CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, nullptr, 0);
static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
if (length == 0) if (length == 0)
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
@ -476,12 +476,12 @@ FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) {
} }
FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) {
int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, 0, 0, 0, 0); int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_.resize(length); buffer_.resize(length);
length = WideCharToMultiByte( length = WideCharToMultiByte(
CP_UTF8, 0, s.c_str(), -1, &buffer_[0], length, 0, 0); CP_UTF8, 0, s.c_str(), -1, &buffer_[0], length, nullptr, nullptr);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
return 0; return 0;
@ -536,9 +536,9 @@ FMT_FUNC void fmt::internal::format_windows_error(
FMT_TRY { FMT_TRY {
String system_message; String system_message;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) { reinterpret_cast<LPWSTR>(system_message.ptr()), 0, nullptr)) {
UTF16ToUTF8 utf8_message; UTF16ToUTF8 utf8_message;
if (utf8_message.convert(system_message.c_str()) == ERROR_SUCCESS) { if (utf8_message.convert(system_message.c_str()) == ERROR_SUCCESS) {
out << message << ": " << utf8_message; out << message << ": " << utf8_message;
@ -648,7 +648,7 @@ void fmt::BasicWriter<Char>::write_str(
template <typename Char> template <typename Char>
inline Arg fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) { inline Arg fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
const char *error = 0; const char *error = nullptr;
Arg arg = *s < '0' || *s > '9' ? Arg arg = *s < '0' || *s > '9' ?
next_arg(error) : get_arg(parse_nonnegative_int(s), error); next_arg(error) : get_arg(parse_nonnegative_int(s), error);
if (error) { if (error) {
@ -713,7 +713,7 @@ void fmt::internal::PrintfFormatter<Char>::parse_flags(
template <typename Char> template <typename Char>
Arg fmt::internal::PrintfFormatter<Char>::get_arg( Arg fmt::internal::PrintfFormatter<Char>::get_arg(
const Char *s, unsigned arg_index) { const Char *s, unsigned arg_index) {
const char *error = 0; const char *error = nullptr;
Arg arg = arg_index == UINT_MAX ? Arg arg = arg_index == UINT_MAX ?
next_arg(error) : FormatterBase::get_arg(arg_index - 1, error); next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
if (error) if (error)
@ -1119,7 +1119,7 @@ FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) {
if (!GetConsoleScreenBufferInfo(handle, &info_con)) if (!GetConsoleScreenBufferInfo(handle, &info_con))
FMT_THROW(WindowsError(GetLastError(), "cannot get console information")); FMT_THROW(WindowsError(GetLastError(), "cannot get console information"));
WORD reset_color = info_con.wAttributes; WORD reset_color = info_con.wAttributes;
WORD color = static_cast<int>(c) >= ARRAYSIZE(WIN32_COLORS) ? reset_color : WIN32_COLORS[c]; WORD color = static_cast<size_t>(c) >= ARRAYSIZE(WIN32_COLORS) ? reset_color : WIN32_COLORS[c];
if (!SetConsoleTextAttribute(handle, color)) if (!SetConsoleTextAttribute(handle, color))
FMT_THROW(WindowsError(GetLastError(), "cannot set console color")); FMT_THROW(WindowsError(GetLastError(), "cannot set console color"));
print(format, args); print(format, args);

View File

@ -92,7 +92,7 @@
// since version 2013. // since version 2013.
# define FMT_USE_VARIADIC_TEMPLATES \ # define FMT_USE_VARIADIC_TEMPLATES \
(FMT_HAS_FEATURE(cxx_variadic_templates) || \ (FMT_HAS_FEATURE(cxx_variadic_templates) || \
(FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || _MSC_VER >= 1800) (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103L) || _MSC_VER >= 1800)
#endif #endif
#ifndef FMT_USE_RVALUE_REFERENCES #ifndef FMT_USE_RVALUE_REFERENCES
@ -103,7 +103,7 @@
# else # else
# define FMT_USE_RVALUE_REFERENCES \ # define FMT_USE_RVALUE_REFERENCES \
(FMT_HAS_FEATURE(cxx_rvalue_references) || \ (FMT_HAS_FEATURE(cxx_rvalue_references) || \
(FMT_GCC_VERSION >= 403 && __cplusplus >= 201103) || _MSC_VER >= 1600) (FMT_GCC_VERSION >= 403 && __cplusplus >= 201103L) || _MSC_VER >= 1600)
# endif # endif
#endif #endif
@ -113,7 +113,7 @@
// Define FMT_USE_NOEXCEPT to make C++ Format use noexcept (C++11 feature). // Define FMT_USE_NOEXCEPT to make C++ Format use noexcept (C++11 feature).
#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \ #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
(FMT_GCC_VERSION >= 408 && __cplusplus >= 201103) (FMT_GCC_VERSION >= 408 && __cplusplus >= 201103L)
# define FMT_NOEXCEPT noexcept # define FMT_NOEXCEPT noexcept
# define FMT_NOEXCEPT_EXPR(expr) noexcept(expr) # define FMT_NOEXCEPT_EXPR(expr) noexcept(expr)
#else #else
@ -124,7 +124,7 @@
// A macro to disallow the copy constructor and operator= functions // A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class // This should be used in the private: declarations for a class
#if FMT_HAS_FEATURE(cxx_deleted_functions)\ #if FMT_HAS_FEATURE(cxx_deleted_functions)\
|| (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103)\ || (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103L)\
|| (_MSC_VER >= 1800) || (_MSC_VER >= 1800)
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&) = delete; \ TypeName(const TypeName&) = delete; \
@ -135,6 +135,32 @@
void operator=(const TypeName&) void operator=(const TypeName&)
#endif #endif
#if FMT_HAS_FEATURE(cxx_nullptr)\
|| (FMT_GCC_VERSION >= 406 && __cplusplus >= 201103L)\
|| (_MSC_VER >= 1600)
// Use nullptr
#else
// Simulate it
namespace std {
class nullptr_t {
public:
template<typename T>
operator T*() const {
return 0;
}
template<typename C, typename T>
operator T C::*() const
{
return 0;
}
private:
void operator&() const;
};
}
std::nullptr_t const nullptr = {};
#endif
namespace fmt { namespace fmt {
// Fix the warning about long long on older versions of GCC // Fix the warning about long long on older versions of GCC
@ -273,7 +299,7 @@ class Buffer {
std::size_t size_; std::size_t size_;
std::size_t capacity_; std::size_t capacity_;
Buffer(T *ptr = 0, std::size_t capacity = 0) Buffer(T *ptr = nullptr, std::size_t capacity = 0)
: ptr_(ptr), size_(0), capacity_(capacity) {} : ptr_(ptr), size_(0), capacity_(capacity) {}
virtual void grow(std::size_t size) = 0; virtual void grow(std::size_t size) = 0;

View File

@ -92,7 +92,7 @@ void fmt::BufferedFile::close() {
if (!file_) if (!file_)
return; return;
int result = FMT_SYSTEM(fclose(file_)); int result = FMT_SYSTEM(fclose(file_));
file_ = 0; file_ = nullptr;
if (result != 0) if (result != 0)
throw SystemError(errno, "cannot close file"); throw SystemError(errno, "cannot close file");
} }

14
posix.h
View File

@ -113,7 +113,7 @@ class BufferedFile {
public: public:
// Constructs a BufferedFile object which doesn't represent any file. // Constructs a BufferedFile object which doesn't represent any file.
BufferedFile() FMT_NOEXCEPT : file_(0) {} BufferedFile() FMT_NOEXCEPT : file_(nullptr) {}
// Destroys the object closing the file it represents if any. // Destroys the object closing the file it represents if any.
~BufferedFile() FMT_NOEXCEPT; ~BufferedFile() FMT_NOEXCEPT;
@ -167,13 +167,13 @@ public:
public: public:
BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) { BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) {
other.file_ = 0; other.file_ = nullptr;
} }
BufferedFile& operator=(BufferedFile &&other) { BufferedFile& operator=(BufferedFile &&other) {
close(); close();
file_ = other.file_; file_ = other.file_;
other.file_ = 0; other.file_ = nullptr;
return *this; return *this;
} }
#endif #endif
@ -182,13 +182,13 @@ public:
BufferedFile(fmt::StringRef filename, fmt::StringRef mode); BufferedFile(fmt::StringRef filename, fmt::StringRef mode);
// Gets whether a file is opened // Gets whether a file is opened
bool is_open() const FMT_NOEXCEPT { return file_ != 0; } bool is_open() const FMT_NOEXCEPT { return file_ != nullptr; }
// Closes the file. // Closes the file.
void close(); void close();
// Returns the pointer to a FILE object representing this file. // Returns the pointer to a FILE object representing this file.
FILE *get() const FMT_NOEXCEPT{ return file_; } FILE *get() const FMT_NOEXCEPT { return file_; }
// Gets the file number of a file // Gets the file number of a file
int fileno() const; int fileno() const;
@ -198,14 +198,14 @@ public:
// Writes raw string without formatting // Writes raw string without formatting
std::size_t write_raw(fmt::StringRef str) { std::size_t write_raw(fmt::StringRef str) {
// FIXME: throw exception when failing // TODO: throw exception when failing
if (!is_open()) if (!is_open())
return 0; return 0;
return ::fwrite(str.c_str(), str.size(), 1, file_); return ::fwrite(str.c_str(), str.size(), 1, file_);
} }
// Writes a single charactor without formatting // Writes a single charactor without formatting
void write_raw(char c) { void write_raw(char c) {
// FIXME: throw exception when failing // TODO: throw exception when failing
if (!is_open()) if (!is_open())
return; return;
::fputc(c, file_); ::fputc(c, file_);