Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db41a7e

Browse files
committed
add Any::castPtr
1 parent 994650d commit db41a7e

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

include/behaviortree_cpp/utils/safe_any.hpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
#include <charconv>
1717
#endif
1818

19-
#include <exception>
20-
#include <algorithm>
21-
#include <iostream>
22-
#include <chrono>
2319
#include <string>
24-
#include <cstring>
2520
#include <type_traits>
2621
#include <typeindex>
2722

@@ -129,17 +124,17 @@ class Any
129124

130125
Any& operator = (const Any& other);
131126

132-
bool isNumber() const;
127+
[[nodiscard]] bool isNumber() const;
133128

134-
bool isIntegral() const;
129+
[[nodiscard]] bool isIntegral() const;
135130

136-
bool isString() const
131+
[[nodiscard]] bool isString() const
137132
{
138133
return _any.type() == typeid(SafeAny::SimpleString);
139134
}
140135

141136
template <typename T>
142-
bool isType() const
137+
[[nodiscard]] bool isType() const
143138
{
144139
return _any.type() == typeid(T);
145140
}
@@ -154,7 +149,7 @@ class Any
154149

155150
// same as tryCast, but throws if fails
156151
template <typename T>
157-
T cast() const {
152+
[[nodiscard]] T cast() const {
158153
if(auto res = tryCast<T>() )
159154
{
160155
return res.value();
@@ -164,17 +159,28 @@ class Any
164159
}
165160
}
166161

167-
const std::type_index& type() const noexcept
162+
// Method to access the value by pointer
163+
template <typename T>
164+
[[nodiscard]] T* castPtr() const {
165+
166+
if( _any.empty() )
167+
{
168+
return nullptr;
169+
}
170+
return linb::any_cast<T>(&_any);
171+
}
172+
173+
[[nodiscard]] const std::type_index& type() const noexcept
168174
{
169175
return _original_type;
170176
}
171177

172-
const std::type_info& castedType() const noexcept
178+
[[nodiscard]] const std::type_info& castedType() const noexcept
173179
{
174180
return _any.type();
175181
}
176182

177-
bool empty() const noexcept
183+
[[nodiscard]] bool empty() const noexcept
178184
{
179185
return _any.empty();
180186
}

0 commit comments

Comments
 (0)