8000 Fix warning C4722 by whuaegeanse · Pull Request #2391 · colmap/colmap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix warning C4722 #2391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/colmap/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ COLMAP_ADD_TEST(
SRCS endian_test.cc
LINK_LIBS colmap_util
)
COLMAP_ADD_TEST(
NAME logging_test
SRCS logging_test.cc
LINK_LIBS colmap_util
)
COLMAP_ADD_TEST(
NAME misc_test
SRCS misc_test.cc
Expand Down
24 changes: 21 additions & 3 deletions src/colmap/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "colmap/util/string.h"

#include <exception>
#include <iostream>

#include <glog/logging.h>
Expand Down Expand Up @@ -138,6 +139,11 @@ inline std::string __MakeExceptionPrefix(const char* file, int line) {
std::to_string(line) + "] ";
}

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4722)
#endif

template <typename T>
class LogMessageFatalThrow : public google::LogMessage {
public:
Expand All @@ -158,22 +164,34 @@ class LogMessageFatalThrow : public google::LogMessage {
// so we do it here.
delete result.str_;
};
[[noreturn]] ~LogMessageFatalThrow() noexcept(false) {
~LogMessageFatalThrow() noexcept(false) {
Flush();
throw T(prefix_ + message_);
#if defined(__cpp_lib_uncaught_exceptions) && \
(__cpp_lib_uncaught_exceptions >= 201411L)
if (std::uncaught_exceptions() == 0)
#else
if (!std::uncaught_exception())
#endif
{
throw T(prefix_ + message_);
}
};

private:
std::string message_;
std::string prefix_;
};

#ifdef _MSC_VER
#pragma warning(pop)
#endif

using LogMessageFatalThrowDefault = LogMessageFatalThrow<std::invalid_argument>;

template <typename T>
T ThrowCheckNotNull(const char* file, int line, const char* names, T&& t) {
if (t == nullptr) {
LogMessageFatalThrowDefault(file, line, new std::string(names));
LogMessageFatalThrowDefault(file, line).stream() << names;
}
return std::forward<T>(t);
}
Expand Down
69 changes: 69 additions & 0 deletions src/colmap/util/logging_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023, ETH Zurich and UNC Chapel Hill.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "colmap/util/logging.h"

#include <gtest/gtest.h>

namespace colmap {
namespace {

std::string PrintingFn(const std::string& message) {
if (message.empty()) {
LOG(FATAL_THROW) << "Error in PrintingFn";
}
return message;
}

void ThrowCheck(const bool cond) { THROW_CHECK(cond) << "Error!"; }

void ThrowCheckEqual(const int val) { THROW_CHECK_EQ(val, 1) << "Error!"; }

TEST(ExceptionLogging, Nominal) {
EXPECT_NO_THROW(ThrowCheck(true));
EXPECT_THROW(ThrowCheck(false), std::invalid_argument);
EXPECT_NO_THROW(ThrowCheckEqual(1));
EXPECT_THROW(ThrowCheckEqual(0), std::invalid_argument);
EXPECT_THROW(THROW_CHECK_NOTNULL(nullptr), std::invalid_argument);
EXPECT_THROW({ LOG(FATAL_THROW) << "Error!"; }, std::invalid_argument);
EXPECT_THROW({ LOG_FATAL_THROW(std::logic_error) << "Error!"; },
std::logic_error);
}

TEST(ExceptionLogging, Nested) {
EXPECT_NO_THROW(PrintingFn("message"));
EXPECT_THROW(PrintingFn(""), std::invalid_argument);
EXPECT_THROW({ LOG(FATAL_THROW) << "Error: " << PrintingFn("message"); },
std::invalid_argument);
EXPECT_THROW({ LOG(FATAL_THROW) << "Error: " << PrintingFn(""); },
std::invalid_argument);
}

} // namespace
} // namespace colmap
1 change: 1 addition & 0 deletions src/colmap/util/sqlite3_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ inline int SQLite3CallHelper(int result_code,
default:
LogMessageFatalThrow<std::runtime_error>(filename.c_str(), line).stream()
<< "SQLite error: " << sqlite3_errstr(result_code);
return result_code;
}
}

Expand Down
0