From c59e814eed5ba5a6f453d2b76d164b34756f868c Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 22 Jan 2024 11:23:03 +0800 Subject: [PATCH] feat update --- 3party/nonstd/{ => ulib}/any.h | 0 3party/nonstd/{ => ulib}/iconv.h | 0 3party/nonstd/{ => ulib}/optional.h | 0 CMakeLists.txt | 1 + src/ulib/base/location.cpp | 16 ++++++++++++++++ src/ulib/base/location.h | 12 ++++++++++++ tests/CMakeLists.txt | 1 + tests/ulib/base/location_unittest.cpp | 19 +++++++++++++++++++ 8 files changed, 49 insertions(+) rename 3party/nonstd/{ => ulib}/any.h (100%) rename 3party/nonstd/{ => ulib}/iconv.h (100%) rename 3party/nonstd/{ => ulib}/optional.h (100%) create mode 100644 tests/ulib/base/location_unittest.cpp diff --git a/3party/nonstd/any.h b/3party/nonstd/ulib/any.h similarity index 100% rename from 3party/nonstd/any.h rename to 3party/nonstd/ulib/any.h diff --git a/3party/nonstd/iconv.h b/3party/nonstd/ulib/iconv.h similarity index 100% rename from 3party/nonstd/iconv.h rename to 3party/nonstd/ulib/iconv.h diff --git a/3party/nonstd/optional.h b/3party/nonstd/ulib/optional.h similarity index 100% rename from 3party/nonstd/optional.h rename to 3party/nonstd/ulib/optional.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d78fd22..c5222ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,7 @@ target_include_directories( 3party/mongoose 3party/nlohmann 3party/nonstd + 3party/nonstd/ulib 3party/sigslot 3party/rxcpp/Rx/v2/src 3party/rxcpp/Ix/CPP/src diff --git a/src/ulib/base/location.cpp b/src/ulib/base/location.cpp index 31013e6..83ca2a9 100644 --- a/src/ulib/base/location.cpp +++ b/src/ulib/base/location.cpp @@ -57,4 +57,20 @@ Location::ToString() const return ss.str(); } +Location +Location::Current(const char *function_name, + const char *file_name, + int line_number) +{ + return Location(function_name, file_name, line_number); +} + +Location::Location(const char *function_name, + const char *file_name, + int line_number) + : function_name_(function_name), + file_name_(file_name + StrippedFilePathPrefixLength()), + line_number_(line_number) +{} + }// namespace ulib diff --git a/src/ulib/base/location.h b/src/ulib/base/location.h index aafdc81..6dc5783 100644 --- a/src/ulib/base/location.h +++ b/src/ulib/base/location.h @@ -24,6 +24,18 @@ public: const char *file_name = __builtin_FILE(), int line_number = __builtin_LINE()); + bool operator==(const Location &other) const + { + return this->ToString() == other.ToString(); + } + + bool operator!=(const Location &other) const { return !(*this == other); } + + bool operator<(const Location &other) const + { + return this->ToString() < other.ToString(); + } + private: Location(const char *function_name, const char *file_name, int line_number); const char *function_name_ = nullptr; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a376e3e..7804c14 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) add_executable( ulib_test + ulib/base/location_unittest.cpp ulib/base/types_unittest.cpp ulib/log/log_unittest.cpp ulib/concorrency/mutex_unittest.cpp diff --git a/tests/ulib/base/location_unittest.cpp b/tests/ulib/base/location_unittest.cpp new file mode 100644 index 0000000..1a69d27 --- /dev/null +++ b/tests/ulib/base/location_unittest.cpp @@ -0,0 +1,19 @@ +#include +#include + +ulib::Location +LocationTest(ulib::Location location = ULIB_FROM_HERE) +{ + return location; +} + +TEST(Location, base) +{ + auto l1 = LocationTest(); + auto l2 = LocationTest(); + EXPECT_NE(l1, l2); + + EXPECT_EQ(l1.line_number() + 1, l2.line_number()); + EXPECT_EQ(l1.function_name(), l2.function_name()); + EXPECT_EQ(l1.file_name(), l2.file_name()); +}