Skip to content

Commit f9c99b6

Browse files
authored
Update Owner Ptr and other smart ptrs with const and noexcept (#13)
* Update Owner Ptr and other smart ptrs with const and noexcept Signed-off-by: jparisu <javierparis@eprosima.com> * apply suggestions Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com> Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent 303b97c commit f9c99b6

8 files changed

Lines changed: 95 additions & 210 deletions

File tree

cpp_utils/include/cpp_utils/memory/impl/GuardedPtr.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ class GuardedPtr final
5151
// CONSTRUCTORS
5252
///////////////////////
5353

54-
//! Move constructor
55-
GuardedPtr(
56-
GuardedPtr&&);
57-
5854
//! Copy constructor not allowed
5955
GuardedPtr(
6056
const GuardedPtr&) = delete;
@@ -63,9 +59,13 @@ class GuardedPtr final
6359
GuardedPtr<T>& operator =(
6460
const GuardedPtr<T>& other) = delete;
6561

62+
//! Move constructor
63+
GuardedPtr(
64+
GuardedPtr&&) noexcept = default;
65+
6666
//! Move operator not allowed
6767
GuardedPtr<T>& operator =(
68-
GuardedPtr<T>&& other) = delete;
68+
GuardedPtr<T>&& other) noexcept = default;
6969

7070
//! Unlock the shared mutex
7171
~GuardedPtr();
@@ -75,10 +75,10 @@ class GuardedPtr final
7575
///////////////////////
7676

7777
//! Secure access to internal ptr as long as this object is valid
78-
T* operator ->();
78+
T* operator ->() const noexcept;
7979

8080
//! Secure access to internal ptr as long as this object is valid
81-
T& operator *();
81+
T& operator *() const noexcept;
8282

8383
/**
8484
* @brief Access to raw ptr of the data (do not check if the internal data is valid)
@@ -88,7 +88,7 @@ class GuardedPtr final
8888
*
8989
* @return T* raw ptr to data
9090
*/
91-
T* get();
91+
T* get() const noexcept;
9292

9393
//! Check whether the internal ptr is valid
9494
operator bool() const noexcept;
@@ -110,7 +110,7 @@ class GuardedPtr final
110110
* @param data_reference
111111
*/
112112
GuardedPtr(
113-
std::shared_ptr<InternalPtrData<T>> data_reference);
113+
std::shared_ptr<InternalPtrData<T>> data_reference) noexcept;
114114

115115
////////////////////////////
116116
// INTERNAL VARIABLES
@@ -142,5 +142,3 @@ bool operator ==(
142142

143143
// Include implementation template file
144144
#include <cpp_utils/memory/impl/GuardedPtr.ipp>
145-
146-

cpp_utils/include/cpp_utils/memory/impl/GuardedPtr.ipp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ namespace utils {
2525

2626
template<typename T>
2727
GuardedPtr<T>::GuardedPtr(
28-
std::shared_ptr<InternalPtrData<T>> data_reference)
28+
std::shared_ptr<InternalPtrData<T>> data_reference) noexcept
2929
: data_reference_(data_reference)
3030
{
31+
// Do nothing
3132
}
3233

3334
template<typename T>
@@ -41,26 +42,19 @@ GuardedPtr<T>::~GuardedPtr()
4142
}
4243

4344
template<typename T>
44-
GuardedPtr<T>::GuardedPtr(
45-
GuardedPtr&& other)
46-
: data_reference_(std::move(other.data_reference_))
47-
{
48-
}
49-
50-
template<typename T>
51-
T* GuardedPtr<T>::operator ->()
45+
T* GuardedPtr<T>::operator ->() const noexcept
5246
{
5347
return data_reference_->operator ->();
5448
}
5549

5650
template<typename T>
57-
T& GuardedPtr<T>::operator *()
51+
T& GuardedPtr<T>::operator *() const noexcept
5852
{
5953
return data_reference_->operator *();
6054
}
6155

6256
template<typename T>
63-
T* GuardedPtr<T>::get()
57+
T* GuardedPtr<T>::get() const noexcept
6458
{
6559
return data_reference_->get();
6660
}
@@ -89,5 +83,3 @@ bool operator ==(
8983

9084
} /* namespace utils */
9185
} /* namespace eprosima */
92-
93-

cpp_utils/include/cpp_utils/memory/impl/InternalPtrData.hpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,41 +53,41 @@ class InternalPtrData
5353
///////////////////////
5454

5555
//! Create an empty (non valid) data
56-
InternalPtrData();
56+
InternalPtrData() noexcept;
5757

5858
//! Move constructor
5959
InternalPtrData(
60-
InternalPtrData&& other);
60+
InternalPtrData&& other) noexcept;
6161

6262
/**
6363
* @brief Destruct object
6464
*
6565
* It should not happen, but in case the ptr is still valid, it will be released.
6666
*/
67-
~InternalPtrData();
67+
~InternalPtrData() noexcept;
6868

6969
///////////////////////
7070
// INTERACTION METHODS
7171
///////////////////////
7272

7373
//! Lock this data in a shared way
74-
void lock_shared();
74+
void lock_shared() noexcept;
7575

7676
//! Unlock this data in a shared way
77-
void unlock_shared();
77+
void unlock_shared() noexcept;
7878

7979
///////////////////////
8080
// ACCESS DATA METHODS
8181
///////////////////////
8282

8383
//! Access to operator-> of the internal data (do not check if the internal data is valid)
84-
T* operator ->();
84+
T* operator ->() const noexcept;
8585

8686
//! Access to operator * of the internal data (do not check if the internal data is valid)
87-
T& operator *();
87+
T& operator *() const noexcept;
8888

8989
//! Access to raw ptr of the data (do not check if the internal data is valid)
90-
T* get();
90+
T* get() const noexcept;
9191

9292
//! Check if the internal data is valid (it is not nullptr)
9393
operator bool() const noexcept;
@@ -111,7 +111,7 @@ class InternalPtrData
111111
*/
112112
InternalPtrData(
113113
T* reference,
114-
const std::function<void(T*)>& deleter);
114+
const std::function<void(T*)>& deleter) noexcept;
115115

116116
/**
117117
* @brief Delete the internal data
@@ -129,7 +129,7 @@ class InternalPtrData
129129
T* reference_;
130130

131131
//! Mutex to guard the data while using or destroying it
132-
std::shared_timed_mutex shared_mutex_;
132+
mutable std::shared_timed_mutex shared_mutex_;
133133

134134
//! Deleter to use when dereferencing the data
135135
std::function<void(T*)> deleter_;
@@ -140,5 +140,3 @@ class InternalPtrData
140140

141141
// Include implementation template file
142142
#include <cpp_utils/memory/impl/InternalPtrData.ipp>
143-
144-

cpp_utils/include/cpp_utils/memory/impl/InternalPtrData.ipp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ namespace utils {
2929
///////////////////////
3030

3131
template<typename T>
32-
InternalPtrData<T>::InternalPtrData()
32+
InternalPtrData<T>::InternalPtrData() noexcept
3333
: reference_(nullptr)
3434
{
3535
}
3636

3737
template<typename T>
3838
InternalPtrData<T>::InternalPtrData(
39-
InternalPtrData&& other)
39+
InternalPtrData&& other) noexcept
4040
: reference_(std::move(other.reference_))
4141
, shared_mutex_(std::move(other.shared_mutex_))
4242
, deleter_(std::move(other.deleter_))
4343
{
4444
}
4545

4646
template<typename T>
47-
InternalPtrData<T>::~InternalPtrData()
47+
InternalPtrData<T>::~InternalPtrData() noexcept
4848
{
4949
// Release data in case it still exists
5050
release_reference_();
@@ -55,13 +55,13 @@ InternalPtrData<T>::~InternalPtrData()
5555
///////////////////////
5656

5757
template<typename T>
58-
void InternalPtrData<T>::lock_shared()
58+
void InternalPtrData<T>::lock_shared() noexcept
5959
{
6060
shared_mutex_.lock_shared();
6161
}
6262

6363
template<typename T>
64-
void InternalPtrData<T>::unlock_shared()
64+
void InternalPtrData<T>::unlock_shared() noexcept
6565
{
6666
shared_mutex_.unlock_shared();
6767
}
@@ -71,19 +71,19 @@ void InternalPtrData<T>::unlock_shared()
7171
///////////////////////
7272

7373
template<typename T>
74-
T* InternalPtrData<T>::operator ->()
74+
T* InternalPtrData<T>::operator ->() const noexcept
7575
{
7676
return reference_;
7777
}
7878

7979
template<typename T>
80-
T& InternalPtrData<T>::operator *()
80+
T& InternalPtrData<T>::operator *() const noexcept
8181
{
8282
return *reference_;
8383
}
8484

8585
template<typename T>
86-
T* InternalPtrData<T>::get()
86+
T* InternalPtrData<T>::get() const noexcept
8787
{
8888
return reference_;
8989
}
@@ -101,7 +101,7 @@ InternalPtrData<T>::operator bool() const noexcept
101101
template<typename T>
102102
InternalPtrData<T>::InternalPtrData(
103103
T* reference,
104-
const std::function<void(T*)>& deleter)
104+
const std::function<void(T*)>& deleter) noexcept
105105
: reference_(reference)
106106
, deleter_(deleter)
107107
{
@@ -121,5 +121,3 @@ void InternalPtrData<T>::release_reference_()
121121

122122
} /* namespace utils */
123123
} /* namespace eprosima */
124-
125-

cpp_utils/include/cpp_utils/memory/impl/LesseePtr.hpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,27 @@ class LesseePtr final
6060
* This constructor is only used to create an empty object.
6161
* It could not contain any data.
6262
*/
63-
LesseePtr();
63+
LesseePtr() noexcept = default;
6464

6565
/**
6666
* @brief Destroy the Lessee Ptr object
6767
*
6868
* It does not destroy the data (it has not the ownership).
69+
* Mutex of Internal reference is handled by GuardedPtrs.
6970
*/
70-
~LesseePtr();
71+
~LesseePtr() = default;
7172

7273
//! Copy constructor from \c OwnerPtr
7374
LesseePtr(
74-
const OwnerPtr<T>& owner);
75+
const OwnerPtr<T>& owner) noexcept;
7576

7677
//! Copy constructor
7778
LesseePtr(
78-
const LesseePtr<T>& other);
79+
const LesseePtr<T>& other) noexcept = default;
7980

80-
// Move constructor
81+
//! Move constructor
8182
LesseePtr(
82-
LesseePtr<T>&& other);
83+
LesseePtr<T>&& other) noexcept = default;
8384

8485
/**
8586
* @brief copy assigment
@@ -90,7 +91,7 @@ class LesseePtr final
9091
* @return this object
9192
*/
9293
LesseePtr<T>& operator =(
93-
const LesseePtr<T>& other);
94+
const LesseePtr<T>& other) noexcept = default;
9495

9596
/**
9697
* @brief move assigment
@@ -102,7 +103,7 @@ class LesseePtr final
102103
* @return this object
103104
*/
104105
LesseePtr<T>& operator =(
105-
LesseePtr<T>&& other);
106+
LesseePtr<T>&& other) noexcept = default;
106107

107108
///////////////////////
108109
// ACCESS DATA METHODS
@@ -118,7 +119,7 @@ class LesseePtr final
118119
*
119120
* @throw ValueAccessException in case internal data no longer exists.
120121
*/
121-
T* operator ->();
122+
T* operator ->() const;
122123

123124
/**
124125
* @brief Access operator to internal ptr with exception calls in failure cases.
@@ -130,7 +131,7 @@ class LesseePtr final
130131
*
131132
* @throw ValueAccessException in case internal data no longer exists.
132133
*/
133-
T& operator *();
134+
T& operator *() const;
134135

135136
/**
136137
* @brief Create a smart reference to the data.
@@ -145,7 +146,7 @@ class LesseePtr final
145146
*
146147
* @return std::shared_ptr<T> to the data. nullptr if the reference is not valid anymore.
147148
*/
148-
GuardedPtr<T> lock() noexcept;
149+
GuardedPtr<T> lock() const noexcept;
149150

150151
/**
151152
* @brief Create a smart reference to the data or throw an exception if data is not available
@@ -160,13 +161,15 @@ class LesseePtr final
160161
* @return std::shared_ptr<T> to the data
161162
* @throw \c ValueAccessException if the data is not valid anymore.
162163
*/
163-
GuardedPtr<T> lock_with_exception();
164+
GuardedPtr<T> lock_with_exception() const;
164165

165166
/**
166167
* @brief Whether the internal data is still valid
167168
*
168169
* @warning this method does not protect the access to the internal data.
169170
* It could happen that data is destroyed right after this method returns.
171+
*
172+
* A \c true from this method is volatile, while a \c false is persistent.
170173
*/
171174
operator bool() const noexcept;
172175

@@ -188,21 +191,8 @@ class LesseePtr final
188191
* @param shared_mutex shared mutex between owner and this.
189192
*/
190193
LesseePtr(
191-
std::shared_ptr<InternalPtrData<T>> data_reference);
194+
std::shared_ptr<InternalPtrData<T>> data_reference) noexcept;
192195

193-
////////////////////////////
194-
// AUXILIARY METHODS
195-
////////////////////////////
196-
197-
/**
198-
* @brief Generic lock method that throws an exception or return nullptr depending on the argument.
199-
*
200-
* @param throw_exception whether the method must throw an exception in case of error
201-
* @return std::shared_ptr<T> to the data. nullptr if the reference is not valid anymore if not \c throw_exception .
202-
* @throw \c InitializationException if the data is not valid anymore if \c throw_exception is \c true .
203-
*/
204-
GuardedPtr<T> lock_(
205-
bool throw_exception);
206196

207197
////////////////////////////
208198
// INTERNAL VARIABLES
@@ -217,5 +207,3 @@ class LesseePtr final
217207

218208
// Include implementation template file
219209
#include <cpp_utils/memory/impl/LesseePtr.ipp>
220-
221-

0 commit comments

Comments
 (0)