Skip to content

Commit 73dfa02

Browse files
committed
copy with UT
1 parent fa80da6 commit 73dfa02

6 files changed

Lines changed: 97 additions & 26 deletions

File tree

med/container.hpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,30 @@ struct seq_clear<>
115115
};
116116

117117
//-----------------------------------------------------------------------
118-
template <class IE>
119-
std::enable_if_t<!is_multi_field_v<IE>>
120-
inline field_copy(IE& to, IE const& from)
118+
template <class IE, class ...ARGS>
119+
std::enable_if_t<!is_multi_field_v<IE>, MED_RESULT>
120+
inline field_copy(IE& to, IE const& from, ARGS&&... args)
121121
{
122-
to.ref_field() = from.ref_field();
122+
return to.ref_field().copy(from.ref_field(), std::forward<ARGS>(args)...);
123123
}
124124

125-
template <class IE>
126-
std::enable_if_t<is_multi_field_v<IE>>
127-
inline field_copy(IE& to, IE const& from)
125+
template <class IE, class ...ARGS>
126+
std::enable_if_t<is_multi_field_v<IE>, MED_RESULT>
127+
inline field_copy(IE& to, IE const& from, ARGS&&... args)
128128
{
129129
to.clear();
130130
for (auto const& rhs : from)
131131
{
132-
if (auto* p = to.push_back())
132+
if (auto* p = to.push_back(std::forward<ARGS>(args)...))
133+
{
134+
MED_CHECK_FAIL(p->copy(rhs, std::forward<ARGS>(args)...));
135+
}
136+
else
133137
{
134-
*p = rhs;
138+
MED_RETURN_FAILURE;
135139
}
136140
}
141+
MED_RETURN_SUCCESS;
137142
}
138143

139144
template <class... IES>
@@ -142,19 +147,22 @@ struct seq_copy;
142147
template <class IE, class... IES>
143148
struct seq_copy<IE, IES...>
144149
{
145-
template <class FIELDS>
146-
static inline void copy(FIELDS& to, FIELDS const& from)
150+
template <class FIELDS, class... ARGS>
151+
static inline MED_RESULT copy(FIELDS& to, FIELDS const& from, ARGS&&... args)
147152
{
148-
field_copy<IE>(to, from);
149-
seq_copy<IES...>::copy(to, from);
153+
return field_copy<IE>(to, from, std::forward<ARGS>(args)...)
154+
MED_AND seq_copy<IES...>::copy(to, from, std::forward<ARGS>(args)...);
150155
}
151156
};
152157

153158
template <>
154159
struct seq_copy<>
155160
{
156-
template <class FIELDS>
157-
static constexpr void copy(FIELDS&, FIELDS const&) { }
161+
template <class FIELDS, class... ARGS>
162+
static constexpr MED_RESULT copy(FIELDS&, FIELDS const&, ARGS&&...)
163+
{
164+
MED_RETURN_SUCCESS;
165+
}
158166
};
159167

160168
//-----------------------------------------------------------------------
@@ -266,7 +274,11 @@ class container : public IE<CONTAINER>
266274
bool is_set() const { return sl::seq_is_set<IES...>::is_set(this->m_ies); }
267275
std::size_t calc_length() const { return sl::container_for<IES...>::calc_length(this->m_ies); }
268276

269-
void copy(container_t const& rhs) { sl::seq_copy<IES...>::copy(this->m_ies, rhs.m_ies); }
277+
template <class... ARGS>
278+
MED_RESULT copy(container_t const& from, ARGS&&... args)
279+
{
280+
return sl::seq_copy<IES...>::copy(this->m_ies, from.m_ies, std::forward<ARGS>(args)...);
281+
}
270282

271283
protected:
272284
struct ies_t : IES...

med/decoder_context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class decoder_context
6666
}
6767

6868
template <std::size_t SIZE, typename T, std::size_t ALLOC_SIZE>
69-
decoder_context(uint8_t const (&data)[SIZE], T const (&alloc_data)[ALLOC_SIZE])
69+
decoder_context(uint8_t const (&data)[SIZE], T (&alloc_data)[ALLOC_SIZE])
7070
: decoder_context(data, SIZE, alloc_data, ALLOC_SIZE*sizeof(T))
7171
{
7272
}

med/multi_field.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,6 @@ class multi_field
121121
field_type const* first() const { return &m_fields[0]; }
122122
field_type const* last() const { return &m_tail->value; }
123123

124-
// field_type* at(std::size_t index)
125-
// {
126-
// iterator it = begin(), ite = end();
127-
// while (index && it != ite) { --index; ++it; }
128-
// return it.get();
129-
// }
130-
131124
//ineffective read-only access
132125
field_type const* at(std::size_t index) const
133126
{

med/octet_string.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class octets_fix_intern
130130
public:
131131
bool empty() const { return !m_is_set; }
132132

133-
constexpr std::size_t size() const { return LEN; }
133+
static constexpr std::size_t size() { return LEN; }
134134
uint8_t const* data() const { return m_data; }
135135
uint8_t* data() { return m_data; }
136136

@@ -189,6 +189,14 @@ struct octet_string_impl : IE<IE_OCTET_STRING>
189189

190190
void clear() { m_value.clear(); }
191191

192+
template <class... ARGS>
193+
MED_RESULT copy(base_t const& from, ARGS&&...)
194+
{
195+
clear();
196+
m_value.assign(from.begin(), from.end());
197+
MED_RETURN_SUCCESS;
198+
}
199+
192200
template <class T = VALUE>
193201
decltype(std::declval<T>().resize(0)) resize(std::size_t new_size)
194202
{

med/value.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Distributed under the MIT License
1313

1414
#include "ie_type.hpp"
1515
#include "value_traits.hpp"
16+
#include "exception.hpp"
1617

1718

1819
namespace med {
@@ -116,6 +117,13 @@ struct integer : IE<IE_VALUE>
116117
void clear() { m_set = false; }
117118
bool is_set() const { return m_set; }
118119
explicit operator bool() const { return is_set(); }
120+
template <class... ARGS>
121+
MED_RESULT copy(base_t const& from, ARGS&&...)
122+
{
123+
m_value = from.m_value;
124+
m_set = from.m_set;
125+
MED_RETURN_SUCCESS;
126+
}
119127

120128
private:
121129
value_type m_value{};
@@ -140,10 +148,13 @@ struct const_integer : IE<const IE_VALUE>
140148

141149

142150
//NOTE: do not override!
151+
explicit operator bool() const { return is_set(); }
143152
static constexpr value_type get_encoded() { return traits::value; }
144153
static constexpr bool set_encoded(value_type v) { return traits::value == v; }
145154
static constexpr bool is_set() { return true; }
146155
static constexpr bool match(value_type v) { return traits::value == v; }
156+
template <class... ARGS>
157+
static constexpr MED_RESULT copy(base_t const&, ARGS&&...) { MED_RETURN_SUCCESS; }
147158
};
148159

149160

@@ -159,10 +170,13 @@ struct init_integer : IE<IE_VALUE>
159170
using base_t = init_integer;
160171

161172
static constexpr void clear() { }
173+
explicit operator bool() const { return is_set(); }
162174
//NOTE: do not override!
163175
static constexpr value_type get_encoded() { return traits::value; }
164176
static constexpr void set_encoded(value_type) { }
165177
static constexpr bool is_set() { return true; }
178+
template <class... ARGS>
179+
static constexpr MED_RESULT copy(base_t const&, ARGS&&...) { MED_RETURN_SUCCESS; }
166180
};
167181

168182

ut/med.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ TEST(decode, alloc_fail)
30233023
, 0x21, 0x35, 0xDA
30243024
, 0x42, 4, 0xFE, 0xE1, 0xAB, 0xBA //<T=0x42, L, FLD_IP>
30253025
, 0,2, 0x76, 0x54, 0x98, 0x76 //<C16, FLD_W>
3026-
, 0x62, 4, 0x01, 0x02, 0x03, 0x04 //[T=0x51, L, FLD_DW]
3026+
, 0x62, 4, 0x01, 0x02, 0x03, 0x04 //[T=0x51, L, FLD_DW]
30273027
, 9, 't', 'e', 's', 't', '.', 't', 'h', 'i', 's'
30283028
, 7, 't', 'e', 's', 't', '.', 'i', 't'
30293029
};
@@ -3053,6 +3053,50 @@ TEST(decode, alloc_fail)
30533053
EXPECT_NE(nullptr, toString(ctx.error_ctx()));
30543054
#endif
30553055
}
3056+
3057+
TEST(copy, mseq_open)
3058+
{
3059+
uint8_t const encoded[] = {
3060+
0x21, 0x35, 0xD9 //<T=0x21, FLD_U16>
3061+
, 0x21, 0x35, 0xDA
3062+
, 0x42, 4, 0xFE, 0xE1, 0xAB, 0xBA //<T=0x42, L, FLD_IP>
3063+
, 0,2, 0x76, 0x54, 0x98, 0x76 //<C16, FLD_W>
3064+
, 0x62, 4, 0x01, 0x02, 0x03, 0x04 //[T=0x51, L, FLD_DW]
3065+
, 9, 't', 'e', 's', 't', '.', 't', 'h', 'i', 's'
3066+
, 7, 't', 'e', 's', 't', '.', 'i', 't'
3067+
};
3068+
3069+
MSEQ_OPEN src_msg;
3070+
MSEQ_OPEN dst_msg;
3071+
3072+
uint8_t dec_buf[1024];
3073+
{
3074+
med::decoder_context<> ctx{ encoded, dec_buf };
3075+
#if (MED_EXCEPTIONS)
3076+
decode(make_octet_decoder(ctx), src_msg);
3077+
ASSERT_THROW(dst_msg.copy(src_msg), med::exception);
3078+
dst_msg.copy(src_msg, ctx);
3079+
#else
3080+
if (!decode(make_octet_decoder(ctx), src_msg)) { FAIL() << toString(ctx.error_ctx()); }
3081+
ASSERT_FALSE(dst_msg.copy(src_msg));
3082+
ASSERT_TRUE(dst_msg.copy(src_msg, ctx));
3083+
#endif
3084+
}
3085+
3086+
{
3087+
uint8_t buffer[1024];
3088+
med::encoder_context<> ctx{ buffer };
3089+
3090+
#if (MED_EXCEPTIONS)
3091+
encode(make_octet_encoder(ctx), dst_msg);
3092+
#else
3093+
if (!encode(make_octet_encoder(ctx), dst_msg)) { FAIL() << toString(ctx.error_ctx()); }
3094+
#endif
3095+
EXPECT_EQ(sizeof(encoded), ctx.buffer().get_offset());
3096+
EXPECT_TRUE(Matches(encoded, buffer));
3097+
}
3098+
}
3099+
30563100
#endif
30573101

30583102
#if 1

0 commit comments

Comments
 (0)