From 3ca77d457b17b51b0e40c9d983c6926cf7abf5c0 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:09:48 +0800 Subject: [PATCH] feat add attributes --- include/sled/any.h | 14 ++++++--- include/sled/lang/attributes.h | 5 +++ include/sled/synchronization/mutex.h | 15 ++++----- src/any_test.cc | 46 +++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 include/sled/lang/attributes.h diff --git a/include/sled/any.h b/include/sled/any.h index cfe3aee..25b89f0 100644 --- a/include/sled/any.h +++ b/include/sled/any.h @@ -345,7 +345,12 @@ public: {} template - inline Any(ValueType &&value) : value_(std::move(value)) + inline Any( + ValueType &&value, + typename std::enable_if::value>::type + * = 0, + typename std::enable_if::value>::type * = 0) + : value_(std::forward(value)) {} // ~Any() noexcept {} @@ -371,13 +376,13 @@ public: } template - inline auto Cast() const -> ValueType + inline auto Cast() const -> ValueType const { return any_cast(value_); } template - inline auto Cast() -> ValueType + inline ValueType Cast() const && { return any_cast(value_); } @@ -414,8 +419,9 @@ public: return *this; } -private: any value_; + +private: }; }// namespace sled diff --git a/include/sled/lang/attributes.h b/include/sled/lang/attributes.h new file mode 100644 index 0000000..6e06b92 --- /dev/null +++ b/include/sled/lang/attributes.h @@ -0,0 +1,5 @@ +#pragma once +#ifndef SLED_LANG_ATTRIBUTES_H +#define SLED_LANG_ATTRIBUTES_H +#define SLED_DEPRECATED __attribute__((deprecated)) +#endif// SLED_LANG_ATTRIBUTES_H diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index dc2cccf..3ff109c 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -9,6 +9,7 @@ #ifndef SLED_SYNCHRONIZATION_MUTEX_H #define SLED_SYNCHRONIZATION_MUTEX_H +#include "sled/lang/attributes.h" #include "sled/units/time_delta.h" #include #include @@ -75,7 +76,7 @@ private: template::value, TLock>::type * = nullptr> -class LockGuard final { +class SLED_DEPRECATED LockGuard final { public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; @@ -89,23 +90,23 @@ private: friend class ConditionVariable; }; -class MutexGuard final { +class MutexLock final { public: - MutexGuard(Mutex *mutex) : lock_(*mutex) {} + MutexLock(Mutex *mutex) : lock_(*mutex) {} - MutexGuard(const MutexGuard &) = delete; - MutexGuard &operator=(const MutexGuard &) = delete; + MutexLock(const MutexLock &) = delete; + MutexLock &operator=(const MutexLock &) = delete; private: friend class ConditionVariable; marl::lock lock_; }; -using MutexLock = MutexGuard; +using MutexGuard SLED_DEPRECATED = MutexLock; // using MutexGuard = marl::lock; // using MutexLock = LockGuard; // using MutexGuard = LockGuard; -using RecursiveMutexLock = LockGuard; +using RecursiveMutexLock SLED_DEPRECATED = LockGuard; // class MutexLock final { // public: diff --git a/src/any_test.cc b/src/any_test.cc index 1bbbc44..e7ee97a 100644 --- a/src/any_test.cc +++ b/src/any_test.cc @@ -1,4 +1,48 @@ #include #include +#include -TEST(Any, Assign) {} +TEST(Any, Assign) +{ + sled::Any any1; + sled::Any any2(any1); + sled::Any any3(1); + sled::Any any4(any3); + sled::Any any5 = 1; + EXPECT_FALSE(any1.HasValue()); + EXPECT_FALSE(any2.HasValue()); + EXPECT_TRUE(any3.HasValue()); + EXPECT_TRUE(any4.HasValue()); + EXPECT_TRUE(any5.HasValue()); + EXPECT_EQ(any3.Cast(), 1); + EXPECT_EQ(any4.Cast(), 1); + EXPECT_EQ(any5.Cast(), 1); + EXPECT_EQ(any3.CastOr("def"), "def"); + EXPECT_EQ(any4.CastOr("def"), "def"); + EXPECT_EQ(any5.CastOr("def"), "def"); + EXPECT_EQ(any3.CastOr(11), 1); +} + +TEST(Any, std_swap) +{ + sled::Any a; + sled::Any b = 2; + EXPECT_FALSE(a.HasValue()); + EXPECT_TRUE(b.HasValue()); + std::swap(a, b); + EXPECT_TRUE(a.HasValue()); + EXPECT_FALSE(b.HasValue()); + EXPECT_EQ(a.Cast(), 2); +} + +TEST(Any, custom_swap) +{ + sled::Any a; + sled::Any b = 2; + EXPECT_FALSE(a.HasValue()); + EXPECT_TRUE(b.HasValue()); + a.swap(b); + EXPECT_TRUE(a.HasValue()); + EXPECT_FALSE(b.HasValue()); + EXPECT_EQ(a.Cast(), 2); +}