Skip to content

Commit 79ceb40

Browse files
authored
Add a new operator to access fuzzy value (#66)
* Add a new operator to access fuzzy value Signed-off-by: jparisu <javierparis@eprosima.com> * Add tests and docs Signed-off-by: jparisu <javierparis@eprosima.com> --------- Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent acb7eca commit 79ceb40

4 files changed

Lines changed: 63 additions & 23 deletions

File tree

cpp_utils/include/cpp_utils/types/Fuzzy.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ class Fuzzy
147147
bool operator !=(
148148
const T& other) const noexcept;
149149

150+
//! Operator to access the internal object
151+
const T* operator ->() const noexcept;
152+
153+
//! Operator to access the internal object
154+
T* operator ->() noexcept;
155+
150156
/////////////////////////
151157
// GET METHODS
152158
/////////////////////////
@@ -194,15 +200,15 @@ class Fuzzy
194200
*
195201
* By default this value would be initialized with default constructor.
196202
*/
197-
T value = T();
203+
T value_ = T();
198204

199205
/**
200206
* @brief Fuzzy level of this object.
201207
*
202208
* This defines the certainty with which the internal \c value has been set.
203209
* By default is set to \c fuzzy_level_default if the internal value has not been set.
204210
*/
205-
FuzzyLevelType fuzzy_level = FuzzyLevelValues::fuzzy_level_default;
211+
FuzzyLevelType fuzzy_level_ = FuzzyLevelValues::fuzzy_level_default;
206212
};
207213

208214

cpp_utils/include/cpp_utils/types/impl/Fuzzy.ipp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ template <typename T>
2626
Fuzzy<T>::Fuzzy(
2727
const T& other,
2828
FuzzyLevelType level /* = FuzzyLevelValues::set */)
29-
: value(other)
30-
, fuzzy_level(level)
29+
: value_(other)
30+
, fuzzy_level_(level)
3131
{
3232
}
3333

3434
template <typename T>
3535
Fuzzy<T>::Fuzzy(
3636
T&& other,
3737
FuzzyLevelType level /* = FuzzyLevelValues::set */)
38-
: value(std::move(other))
39-
, fuzzy_level(level)
38+
: value_(std::move(other))
39+
, fuzzy_level_(level)
4040
{
4141
}
4242

@@ -47,13 +47,13 @@ Fuzzy<T>::Fuzzy(
4747
template <typename T>
4848
Fuzzy<T>::operator T&() noexcept
4949
{
50-
return value;
50+
return value_;
5151
}
5252

5353
template <typename T>
5454
Fuzzy<T>::operator T() const noexcept
5555
{
56-
return value;
56+
return value_;
5757
}
5858

5959
template <typename T>
@@ -67,7 +67,7 @@ bool Fuzzy<T>::operator ==(
6767
}
6868
else
6969
{
70-
return this->fuzzy_level == other.fuzzy_level && this->value == other.get_reference();
70+
return this->fuzzy_level_ == other.fuzzy_level_ && this->value_ == other.get_reference();
7171
}
7272
}
7373

@@ -81,7 +81,7 @@ bool Fuzzy<T>::operator ==(
8181
}
8282
else
8383
{
84-
return this->value == other;
84+
return this->value_ == other;
8585
}
8686
}
8787

@@ -99,44 +99,56 @@ bool Fuzzy<T>::operator !=(
9999
return !(this->operator ==(other));
100100
}
101101

102+
template <typename T>
103+
const T* Fuzzy<T>::operator ->() const noexcept
104+
{
105+
return &value_;
106+
}
107+
108+
template <typename T>
109+
T* Fuzzy<T>::operator ->() noexcept
110+
{
111+
return &value_;
112+
}
113+
102114
/////////////////////////
103115
// GET METHODS
104116
/////////////////////////
105117

106118
template <typename T>
107119
bool Fuzzy<T>::is_valid() const noexcept
108120
{
109-
return fuzzy_level >= FuzzyLevelValues::fuzzy_level_default;
121+
return fuzzy_level_ >= FuzzyLevelValues::fuzzy_level_default;
110122
}
111123

112124
template <typename T>
113125
bool Fuzzy<T>::is_set() const noexcept
114126
{
115-
return fuzzy_level >= FuzzyLevelValues::fuzzy_level_fuzzy;
127+
return fuzzy_level_ >= FuzzyLevelValues::fuzzy_level_fuzzy;
116128
}
117129

118130
template <typename T>
119131
T& Fuzzy<T>::get_reference() noexcept
120132
{
121-
return value;
133+
return value_;
122134
}
123135

124136
template <typename T>
125137
const T& Fuzzy<T>::get_reference() const noexcept
126138
{
127-
return value;
139+
return value_;
128140
}
129141

130142
template <typename T>
131143
T Fuzzy<T>::get_value() const noexcept
132144
{
133-
return value;
145+
return value_;
134146
}
135147

136148
template <typename T>
137149
FuzzyLevelType Fuzzy<T>::get_level() const noexcept
138150
{
139-
return fuzzy_level;
151+
return fuzzy_level_;
140152
}
141153

142154
/////////////////////////
@@ -146,24 +158,24 @@ FuzzyLevelType Fuzzy<T>::get_level() const noexcept
146158
template <typename T>
147159
void Fuzzy<T>::unset()
148160
{
149-
// It is not needed to change value, as it would be used as it is not set
150-
fuzzy_level = FuzzyLevelValues::fuzzy_level_unset;
161+
// It is not needed to change value_, as it would be used as it is not set
162+
fuzzy_level_ = FuzzyLevelValues::fuzzy_level_unset;
151163
}
152164

153165
template <typename T>
154166
void Fuzzy<T>::set_value(
155167
const T& new_value,
156168
FuzzyLevelType level /* = FuzzyLevelValues::SET */)
157169
{
158-
value = new_value;
159-
fuzzy_level = level;
170+
value_ = new_value;
171+
fuzzy_level_ = level;
160172
}
161173

162174
template <typename T>
163175
void Fuzzy<T>::set_level(
164176
FuzzyLevelType level /* = FuzzyLevelValues::fuzzy_level_set */)
165177
{
166-
fuzzy_level = level;
178+
fuzzy_level_ = level;
167179
}
168180

169181
/////////////////////////
@@ -181,5 +193,3 @@ std::ostream& operator <<(
181193

182194
} /* namespace utils */
183195
} /* namespace eprosima */
184-
185-

cpp_utils/test/unittest/types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(TEST_LIST
2828
default_construct
2929
construct
3030
assign_operator
31+
arrow_operator
3132
)
3233

3334
set(TEST_EXTRA_LIBRARIES

cpp_utils/test/unittest/types/fuzzyTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ TEST(fuzzyTest, assign_operator)
155155
}
156156
}
157157

158+
/**
159+
* Test Fuzzy arrow operator
160+
*
161+
* CASES:
162+
* - const
163+
* - no const
164+
*/
165+
TEST(fuzzyTest, arrow_operator)
166+
{
167+
// no const
168+
{
169+
Fuzzy<std::string> f;
170+
f = std::string("hello");
171+
ASSERT_EQ(f->size(), 5);
172+
}
173+
174+
// const
175+
{
176+
const Fuzzy<std::string> f (std::string("hello"));
177+
ASSERT_EQ(f->size(), 5);
178+
}
179+
}
180+
158181
int main(
159182
int argc,
160183
char** argv)

0 commit comments

Comments
 (0)