feat add attributes
This commit is contained in:
parent
2df2fcb687
commit
3ca77d457b
@ -345,7 +345,12 @@ public:
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
template<typename ValueType>
|
template<typename ValueType>
|
||||||
inline Any(ValueType &&value) : value_(std::move(value))
|
inline Any(
|
||||||
|
ValueType &&value,
|
||||||
|
typename std::enable_if<!std::is_same<Any &, ValueType>::value>::type
|
||||||
|
* = 0,
|
||||||
|
typename std::enable_if<!std::is_const<ValueType>::value>::type * = 0)
|
||||||
|
: value_(std::forward<ValueType &&>(value))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
// ~Any() noexcept {}
|
// ~Any() noexcept {}
|
||||||
@ -371,13 +376,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename ValueType>
|
template<typename ValueType>
|
||||||
inline auto Cast() const -> ValueType
|
inline auto Cast() const -> ValueType const
|
||||||
{
|
{
|
||||||
return any_cast<ValueType>(value_);
|
return any_cast<ValueType>(value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ValueType>
|
template<typename ValueType>
|
||||||
inline auto Cast() -> ValueType
|
inline ValueType Cast() const &&
|
||||||
{
|
{
|
||||||
return any_cast<ValueType>(value_);
|
return any_cast<ValueType>(value_);
|
||||||
}
|
}
|
||||||
@ -414,8 +419,9 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
any value_;
|
any value_;
|
||||||
|
|
||||||
|
private:
|
||||||
};
|
};
|
||||||
|
|
||||||
}// namespace sled
|
}// namespace sled
|
||||||
|
5
include/sled/lang/attributes.h
Normal file
5
include/sled/lang/attributes.h
Normal file
@ -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
|
@ -9,6 +9,7 @@
|
|||||||
#ifndef SLED_SYNCHRONIZATION_MUTEX_H
|
#ifndef SLED_SYNCHRONIZATION_MUTEX_H
|
||||||
#define SLED_SYNCHRONIZATION_MUTEX_H
|
#define SLED_SYNCHRONIZATION_MUTEX_H
|
||||||
|
|
||||||
|
#include "sled/lang/attributes.h"
|
||||||
#include "sled/units/time_delta.h"
|
#include "sled/units/time_delta.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
@ -75,7 +76,7 @@ private:
|
|||||||
template<typename TLock,
|
template<typename TLock,
|
||||||
typename std::enable_if<internal::HasLockAndUnlock<TLock>::value,
|
typename std::enable_if<internal::HasLockAndUnlock<TLock>::value,
|
||||||
TLock>::type * = nullptr>
|
TLock>::type * = nullptr>
|
||||||
class LockGuard final {
|
class SLED_DEPRECATED LockGuard final {
|
||||||
public:
|
public:
|
||||||
LockGuard(const LockGuard &) = delete;
|
LockGuard(const LockGuard &) = delete;
|
||||||
LockGuard &operator=(const LockGuard &) = delete;
|
LockGuard &operator=(const LockGuard &) = delete;
|
||||||
@ -89,23 +90,23 @@ private:
|
|||||||
friend class ConditionVariable;
|
friend class ConditionVariable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MutexGuard final {
|
class MutexLock final {
|
||||||
public:
|
public:
|
||||||
MutexGuard(Mutex *mutex) : lock_(*mutex) {}
|
MutexLock(Mutex *mutex) : lock_(*mutex) {}
|
||||||
|
|
||||||
MutexGuard(const MutexGuard &) = delete;
|
MutexLock(const MutexLock &) = delete;
|
||||||
MutexGuard &operator=(const MutexGuard &) = delete;
|
MutexLock &operator=(const MutexLock &) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class ConditionVariable;
|
friend class ConditionVariable;
|
||||||
marl::lock lock_;
|
marl::lock lock_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using MutexLock = MutexGuard;
|
using MutexGuard SLED_DEPRECATED = MutexLock;
|
||||||
// using MutexGuard = marl::lock;
|
// using MutexGuard = marl::lock;
|
||||||
// using MutexLock = LockGuard<Mutex>;
|
// using MutexLock = LockGuard<Mutex>;
|
||||||
// using MutexGuard = LockGuard<Mutex>;
|
// using MutexGuard = LockGuard<Mutex>;
|
||||||
using RecursiveMutexLock = LockGuard<RecursiveMutex>;
|
using RecursiveMutexLock SLED_DEPRECATED = LockGuard<RecursiveMutex>;
|
||||||
|
|
||||||
// class MutexLock final {
|
// class MutexLock final {
|
||||||
// public:
|
// public:
|
||||||
|
@ -1,4 +1,48 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <sled/any.h>
|
#include <sled/any.h>
|
||||||
|
#include <sled/log/log.h>
|
||||||
|
|
||||||
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<int>(), 1);
|
||||||
|
EXPECT_EQ(any4.Cast<int>(), 1);
|
||||||
|
EXPECT_EQ(any5.Cast<int>(), 1);
|
||||||
|
EXPECT_EQ(any3.CastOr<std::string>("def"), "def");
|
||||||
|
EXPECT_EQ(any4.CastOr<std::string>("def"), "def");
|
||||||
|
EXPECT_EQ(any5.CastOr<std::string>("def"), "def");
|
||||||
|
EXPECT_EQ(any3.CastOr<int>(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<int>(), 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<int>(), 2);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user