Skip to content

Commit ac85844

Browse files
Update repository for Fast DDS 3.0.0 compatibility (#101)
* Update repository for FastDDS 3.0.0 compatibility Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Fix tests Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Reimplement DBQueue internally Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Protect DBQueue against multiple inclusion Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Fix ReturnCodeTest serializator Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Remap not-included FastDDS ReturnCodes to ReturnCode::ERROR and refactor DBQueue code style to snake case Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> * Fix uncrustify Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com> --------- Signed-off-by: Lucia Echevarria <luciaechevarria@eprosima.com>
1 parent 6b66cec commit ac85844

29 files changed

Lines changed: 322 additions & 91 deletions

File tree

cmake_utils/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ Setting the CMake variable `<library>_MINIMUM_VERSION` will force the `find_pack
102102
e.g.
103103

104104
```cmake
105-
set(MODULE_FIND_PACKAGES fastrtps)
106-
set(fastrtps_MINIMUM_VERSION "2.8") # This will force to use a version of fastrtps higher or equal 2.8
105+
set(MODULE_FIND_PACKAGES fastdds)
106+
set(fastdds_MINIMUM_VERSION "3.0.0") # This will force to use a version of fastdds higher or equal 3.0.0
107107
```
108108

109109
---

cmake_utils/templates/cmake/packaging/library-Config.cmake.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ if(NOT fastcdr_FOUND)
2626
find_package(fastcdr)
2727
endif()
2828

29-
if(NOT fastrtps_FOUND)
30-
find_package(fastrtps)
29+
if(NOT fastdds_FOUND)
30+
find_package(fastdds)
3131
endif()
3232

3333
include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake)

cpp_utils/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ handler.close(); // This makes new threads that call wait() to wait until next o
125125

126126
## Dependencies
127127

128-
* `fastrtps`
128+
* `fastdds`
129129

130130
Only for Windows:
131131

@@ -147,7 +147,7 @@ target_link_libraries(${LIBRARY_TARGET_NAME} eprosima::utils)
147147

148148
<!-- TODO remove Fast DDS from here once and for all -->
149149

150-
This package should not be dependent of Fast DDS `fastrtps` so in future versions this would be changed.
150+
This package should not be dependent of Fast DDS `fastdds` so in future versions this would be changed.
151151
This dependant comes from:
152152

153153
* Log module

cpp_utils/include/cpp_utils/ReturnCode.hpp

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
#pragma once
2020

21-
#include <fastrtps/types/TypesBase.h>
21+
#include <cstdint>
22+
#include <map>
23+
#include <string>
24+
25+
#include <fastdds/dds/core/ReturnCode.hpp>
2226

2327
#include <cpp_utils/library/library_dll.h>
2428

@@ -30,36 +34,71 @@ namespace utils {
3034
*
3135
* It uses the fastdds ReturnCode_t
3236
*/
33-
class ReturnCode : public eprosima::fastrtps::types::ReturnCode_t
37+
class ReturnCode
3438
{
3539
public:
3640

37-
//! Inherit Parent class constructors
38-
using eprosima::fastrtps::types::ReturnCode_t::ReturnCode_t;
41+
enum ReturnCodeValue
42+
{
43+
OK = 0,
44+
ERROR = 1,
45+
UNKNOWN = 2,
46+
NO_DATA = 3,
47+
NOT_ENABLED = 4,
48+
PRECONDITION_NOT_MET = 5,
49+
};
50+
51+
CPP_UTILS_DllAPI
52+
ReturnCode()
53+
: value_(OK)
54+
{
55+
}
56+
57+
CPP_UTILS_DllAPI
58+
ReturnCode(
59+
const fastdds::dds::ReturnCode_t& value);
3960

40-
CPP_UTILS_DllAPI ReturnCode(
41-
const eprosima::fastrtps::types::ReturnCode_t& other);
61+
CPP_UTILS_DllAPI
62+
std::uint32_t operator ()() const noexcept;
4263

43-
//! Specify the operator so OK code could be translated to True.
44-
CPP_UTILS_DllAPI bool operator ()() const noexcept;
64+
CPP_UTILS_DllAPI
65+
bool operator ==(
66+
const ReturnCode& c) const noexcept;
4567

46-
//! Minor operator
47-
CPP_UTILS_DllAPI bool operator <(
68+
CPP_UTILS_DllAPI
69+
bool operator !=(
70+
const ReturnCode& c) const noexcept;
71+
72+
CPP_UTILS_DllAPI
73+
bool operator <(
4874
const ReturnCode& other) const noexcept;
4975

76+
CPP_UTILS_DllAPI
77+
bool operator !() const noexcept;
78+
79+
CPP_UTILS_DllAPI
80+
explicit operator bool() = delete;
81+
5082
protected:
5183

5284
//! Link every ReturnCode available with a string to deserialize
5385
static const std::map<ReturnCode, std::string> to_string_conversion_;
5486

87+
//! \c ReturnCode value
88+
std::uint32_t value_;
89+
90+
5591
// operator << needs access to the object
56-
CPP_UTILS_DllAPI friend std::ostream& operator <<(
92+
CPP_UTILS_DllAPI
93+
friend std::ostream& operator <<(
5794
std::ostream& os,
5895
const ReturnCode& code);
96+
5997
};
6098

6199
//! \c ReturnCode to stream serializator
62-
CPP_UTILS_DllAPI std::ostream& operator <<(
100+
CPP_UTILS_DllAPI
101+
std::ostream& operator <<(
63102
std::ostream& os,
64103
const ReturnCode& code);
65104

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright 2024 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 DBQueue.hpp
17+
*/
18+
19+
#ifndef DBQUEUE_HPP
20+
#define DBQUEUE_HPP
21+
22+
#include <condition_variable>
23+
#include <memory>
24+
#include <mutex>
25+
#include <queue>
26+
27+
namespace eprosima {
28+
namespace utils {
29+
namespace event {
30+
31+
/**
32+
* Double buffered, threadsafe queue for MPSC (multi-producer, single-consumer) comms.
33+
*/
34+
template<class T>
35+
class DBQueue
36+
{
37+
38+
public:
39+
40+
DBQueue()
41+
: m_foreground_queue(&m_queue_alpha)
42+
, m_background_queue(&m_queue_beta)
43+
{
44+
}
45+
46+
//! Clears foreground queue and swaps queues.
47+
void swap()
48+
{
49+
std::unique_lock<std::mutex> fg_guard(m_foreground_mutex);
50+
std::unique_lock<std::mutex> bg_guard(m_background_mutex);
51+
52+
// Clear the foreground queue.
53+
std::queue<T>().swap(*m_foreground_queue);
54+
55+
auto* swap = m_background_queue;
56+
m_background_queue = m_foreground_queue;
57+
m_foreground_queue = swap;
58+
}
59+
60+
//! Pushes to the background queue. Copy constructor.
61+
void push(
62+
const T& item)
63+
{
64+
std::unique_lock<std::mutex> guard(m_background_mutex);
65+
m_background_queue->push(item);
66+
}
67+
68+
//! Pushes to the background queue. Move constructor.
69+
void push(
70+
T&& item)
71+
{
72+
std::unique_lock<std::mutex> guard(m_background_mutex);
73+
m_background_queue->push(std::move(item));
74+
}
75+
76+
//! Returns a reference to the front element
77+
//! in the foregrund queue.
78+
T& front()
79+
{
80+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
81+
return m_foreground_queue->front();
82+
}
83+
84+
const T& front() const
85+
{
86+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
87+
return m_foreground_queue->front();
88+
}
89+
90+
//! Pops from the foreground queue.
91+
void pop()
92+
{
93+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
94+
m_foreground_queue->pop();
95+
}
96+
97+
//! Return the front element in the foreground queue by moving it and erase it from the queue.
98+
T front_and_pop()
99+
{
100+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
101+
102+
// Get value by moving the internal queue reference to a new value
103+
T value = std::move(m_foreground_queue->front());
104+
// At this point m_foreground_queue contains a non valid element, but mutex is taken and next instruction erase it
105+
106+
// Pop value from queue
107+
m_foreground_queue->pop();
108+
109+
// Return value (as it has been created in this scope, it will not be copied but moved or directly forwarded)
110+
return value;
111+
}
112+
113+
//! Reports whether the foreground queue is empty.
114+
bool empty() const
115+
{
116+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
117+
return m_foreground_queue->empty();
118+
}
119+
120+
//! Reports whether the both queues are empty.
121+
bool both_empty() const
122+
{
123+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
124+
std::unique_lock<std::mutex> bg_guard(m_background_mutex);
125+
return m_foreground_queue->empty() && m_background_queue->empty();
126+
}
127+
128+
//! Reports the size of the foreground queue.
129+
size_t size() const
130+
{
131+
std::unique_lock<std::mutex> guard(m_foreground_mutex);
132+
return m_foreground_queue->size();
133+
}
134+
135+
//! Clears foreground and background.
136+
void clear()
137+
{
138+
std::unique_lock<std::mutex> fg_guard(m_foreground_mutex);
139+
std::unique_lock<std::mutex> bg_guard(m_background_mutex);
140+
std::queue<T>().swap(*m_foreground_queue);
141+
std::queue<T>().swap(*m_background_queue);
142+
}
143+
144+
private:
145+
146+
// Underlying queues
147+
std::queue<T> m_queue_alpha;
148+
std::queue<T> m_queue_beta;
149+
150+
// Front and background queue references (double buffering)
151+
std::queue<T>* m_foreground_queue;
152+
std::queue<T>* m_background_queue;
153+
154+
mutable std::mutex m_foreground_mutex;
155+
mutable std::mutex m_background_mutex;
156+
};
157+
158+
} /* namespace event */
159+
} /* namespace utils */
160+
} /* namespace eprosima */
161+
162+
#endif // ifndef DBQUEUE_H

cpp_utils/include/cpp_utils/wait/DBQueueWaitHandler.hpp

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

1919
#pragma once
2020

21-
#include <fastrtps/utils/DBQueue.h>
21+
#include <cpp_utils/queue/DBQueue.hpp>
2222

2323
#include <cpp_utils/wait/ConsumerWaitHandler.hpp>
2424

@@ -71,7 +71,7 @@ class DBQueueWaitHandler : public ConsumerWaitHandler<T>
7171
T get_next_value_() override;
7272

7373
//! \c DBQueue variable that stores the data
74-
fastrtps::DBQueue<T> queue_;
74+
DBQueue<T> queue_;
7575

7676
//! Protect getting values from the queue so only one thread can do the swap at a time
7777
std::mutex pop_queue_mutex_;

cpp_utils/include/cpp_utils/wait/impl/DBQueueWaitHandler.ipp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ void DBQueueWaitHandler<T>::add_value_(
2828
T&& value)
2929
{
3030
logDebug(UTILS_WAIT_DBQUEUE, "Moving element to DBQueue.");
31-
queue_.Push(std::move(value));
31+
queue_.push(std::move(value));
3232
}
3333

3434
template <typename T>
3535
void DBQueueWaitHandler<T>::add_value_(
3636
const T& value)
3737
{
3838
logDebug(UTILS_WAIT_DBQUEUE, "Copying element to DBQueue.");
39-
queue_.Push(value);
39+
queue_.push(value);
4040
}
4141

4242
template <typename T>
@@ -46,21 +46,21 @@ T DBQueueWaitHandler<T>::get_next_value_()
4646
std::unique_lock<std::mutex> lock(pop_queue_mutex_);
4747

4848
// If front is empty, swap to back queue
49-
if (queue_.Empty())
49+
if (queue_.empty())
5050
{
5151
logDebug(UTILS_WAIT_DBQUEUE, "Swapping DBQueue to get element.");
52-
queue_.Swap();
52+
queue_.swap();
5353
}
5454

5555
// If queue is empty, there is a synchronization problem
56-
if (queue_.Empty())
56+
if (queue_.empty())
5757
{
5858
throw utils::InconsistencyException("Empty DBQueue, impossible to get value.");
5959
}
6060

6161
// TODO: Do it without copy
62-
auto value = queue_.Front();
63-
queue_.Pop();
62+
auto value = queue_.front();
63+
queue_.pop();
6464

6565
return value;
6666
}

cpp_utils/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<buildtool_depend>cmake</buildtool_depend>
1919

20-
<depend>fastrtps</depend>
20+
<depend>fastdds</depend>
2121
<depend>cmake_utils</depend>
2222

2323
<doc_depend>doxygen</doc_depend>

cpp_utils/project_settings.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(MODULE_SUMMARY
2424

2525
set(MODULE_FIND_PACKAGES
2626
fastcdr
27-
fastrtps
27+
fastdds
2828
)
2929

3030
set(MODULE_DEPENDENCIES

0 commit comments

Comments
 (0)