-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany.h
48 lines (38 loc) · 888 Bytes
/
any.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* any.h
* Copyright (C) 2021 youfa.song <[email protected]>
*
* Distributed under terms of the GPLv2 license.
*/
#ifndef AVE_ANY_H
#define AVE_ANY_H
#include <memory>
#include <utility>
namespace ave {
namespace base {
// Deprecated, use std::any maybe better
class Any {
public:
Any() = default;
virtual ~Any() = default;
template <typename T, typename... Args>
void Set(Args&&... args) {
mData.reset(new T(std::forward<Args>(args)...),
[](void* ptr) { delete static_cast<T*>(ptr); });
}
template <typename T>
T* Get() {
if (!mData) {
return nullptr;
}
T* ptr = static_cast<T*>(mData.get());
return ptr;
}
operator bool() { return mData.operator bool(); }
bool empty() { return !operator bool(); }
private:
std::shared_ptr<void> mData;
};
} // namespace base
} // namespace ave
#endif /* !AVE_ANY_H */