Skip to content

Commit 2775018

Browse files
authored
Implement new util functions (#16)
* Implement new util functions Signed-off-by: jparisu <javierparis@eprosima.com> * Make ReturnCode convertible Signed-off-by: jparisu <javierparis@eprosima.com> * add headers to custom enum Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com> Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent 532126b commit 2775018

8 files changed

Lines changed: 96 additions & 9 deletions

File tree

cpp_utils/include/cpp_utils/ReturnCode.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class ReturnCode : public eprosima::fastrtps::types::ReturnCode_t
3737
//! Inherit Parent class constructors
3838
using eprosima::fastrtps::types::ReturnCode_t::ReturnCode_t;
3939

40+
ReturnCode(
41+
const eprosima::fastrtps::types::ReturnCode_t& other);
42+
4043
//! Specify the operator so OK code could be translated to True.
4144
CPP_UTILS_DllAPI bool operator ()() const noexcept;
4245

@@ -62,5 +65,3 @@ CPP_UTILS_DllAPI std::ostream& operator <<(
6265

6366
} /* namespace utils */
6467
} /* namespace eprosima */
65-
66-

cpp_utils/include/cpp_utils/impl/utils.ipp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#pragma once
2020

21+
#include <cstring>
22+
2123
namespace eprosima {
2224
namespace utils {
2325

@@ -153,7 +155,15 @@ std::string generic_to_string(
153155
return ss.str();
154156
}
155157

158+
template <typename T>
159+
void* copy_to_void_ptr(
160+
const T* source,
161+
size_t size /* = sizeof(T) */)
162+
{
163+
void* new_ptr = std::malloc(size);
164+
std::memcpy(new_ptr, source, size);
165+
return new_ptr;
166+
}
167+
156168
} /* namespace utils */
157169
} /* namespace eprosima */
158-
159-

cpp_utils/include/cpp_utils/macros/custom_enumeration.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
#pragma once
2222

23-
#include <string>
2423
#include <array>
24+
#include <ostream>
25+
#include <string>
26+
#include <vector>
2527

2628
#include <cpp_utils/exception/InitializationException.hpp>
2729
#include <cpp_utils/macros/macros.hpp>

cpp_utils/include/cpp_utils/types/Fuzzy.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,3 @@ CPP_UTILS_DllAPI std::ostream& operator <<(
221221

222222
// Include implementation template file
223223
#include <cpp_utils/types/impl/Fuzzy.ipp>
224-
225-
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file cast.hpp
17+
*/
18+
19+
#pragma once
20+
21+
#include <cpp_utils/library/library_dll.h>
22+
23+
namespace eprosima {
24+
namespace utils {
25+
26+
template <typename T>
27+
CPP_UTILS_DllAPI void* cast_to_void_ptr(
28+
const T* c);
29+
30+
} /* namespace utils */
31+
} /* namespace eprosima */
32+
33+
// Include implementation template file
34+
#include <cpp_utils/types/impl/cast.ipp>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License")
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file cast.ipp
17+
*/
18+
19+
#pragma once
20+
21+
namespace eprosima {
22+
namespace utils {
23+
24+
template <typename T>
25+
void* cast_to_void_ptr(
26+
const T* c)
27+
{
28+
return static_cast<void*>(const_cast<T*>(c));
29+
}
30+
31+
} /* namespace utils */
32+
} /* namespace eprosima */

cpp_utils/include/cpp_utils/utils.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,13 @@ template <typename T>
172172
CPP_UTILS_DllAPI std::string generic_to_string(
173173
const T& element);
174174

175+
template <typename T>
176+
CPP_UTILS_DllAPI void* copy_to_void_ptr(
177+
const T* source,
178+
size_t size = sizeof(T));
179+
175180
} /* namespace utils */
176181
} /* namespace eprosima */
177182

178183
// Include implementation template file
179184
#include <cpp_utils/impl/utils.ipp>
180-
181-

cpp_utils/src/cpp/ReturnCode.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const std::map<ReturnCode, std::string> ReturnCode::to_string_conversion_ =
4141
{ReturnCode::RETCODE_NOT_ALLOWED_BY_SECURITY, "NotAllowedBySecurity"},
4242
};
4343

44+
ReturnCode::ReturnCode(
45+
const eprosima::fastrtps::types::ReturnCode_t& other)
46+
: eprosima::fastrtps::types::ReturnCode_t(other())
47+
{
48+
// Do nothing
49+
}
50+
4451
bool ReturnCode::operator ()() const noexcept
4552
{
4653
return RETCODE_OK == *this;

0 commit comments

Comments
 (0)