Skip to content

Commit 4d4a1f8

Browse files
committedJan 20, 2022
Update error messages about static_variant
1 parent 02b7593 commit 4d4a1f8

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed
 

‎include/fc/static_variant.hpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ class static_variant {
7474

7575
void init_from_tag(tag_type tag)
7676
{
77-
FC_ASSERT( tag >= 0 );
78-
FC_ASSERT( static_cast<size_t>(tag) < count() );
77+
FC_ASSERT( tag >= 0, "Unable to init with a negative tag '${tag}'", ("tag",tag) );
78+
FC_ASSERT( static_cast<size_t>(tag) < count(),
79+
"Unable to init with tag '${tag}' when the number of supported tags is ${count}",
80+
("tag",tag) ("count",count()) );
7981
_tag = tag;
8082
typelist::runtime::dispatch(list(), tag, [this](auto t) {
8183
using T = typename decltype(t)::type;
@@ -235,15 +237,19 @@ class static_variant {
235237
if(_tag == typelist::index_of<list, X>()) {
236238
return *reinterpret_cast<X*>(storage.data());
237239
} else {
238-
FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename<X>::name()) );
240+
FC_THROW_EXCEPTION( fc::assert_exception,
241+
"static_variant does not contain a value of type ${t}",
242+
("t",fc::get_typename<X>::name()) );
239243
}
240244
}
241245
template<typename X, typename = type_in_typelist<X>>
242246
const X& get() const {
243247
if(_tag == typelist::index_of<list, X>()) {
244248
return *reinterpret_cast<const X*>(storage.data());
245249
} else {
246-
FC_THROW_EXCEPTION( fc::assert_exception, "static_variant does not contain a value of type ${t}", ("t",fc::get_typename<X>::name()) );
250+
FC_THROW_EXCEPTION( fc::assert_exception,
251+
"static_variant does not contain a value of type ${t}",
252+
("t",fc::get_typename<X>::name()) );
247253
}
248254
}
249255
template<typename visitor>
@@ -269,7 +275,9 @@ class static_variant {
269275
template<typename visitor>
270276
static typename visitor::result_type visit( tag_type tag, visitor& v, void* data )
271277
{
272-
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(), "Unsupported type ${tag}!", ("tag",tag) );
278+
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(),
279+
"Unsupported type '${tag}', the number of supported types is ${count}! ",
280+
("tag",tag) ("count",count()) );
273281
return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) {
274282
return v(*reinterpret_cast<typename decltype(t)::type*>(data));
275283
});
@@ -278,7 +286,9 @@ class static_variant {
278286
template<typename visitor>
279287
static typename visitor::result_type visit( tag_type tag, const visitor& v, void* data )
280288
{
281-
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(), "Unsupported type ${tag}!", ("tag",tag) );
289+
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(),
290+
"Unsupported type '${tag}', the number of supported types is ${count}! ",
291+
("tag",tag) ("count",count()) );
282292
return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) {
283293
return v(*reinterpret_cast<typename decltype(t)::type*>(data));
284294
});
@@ -287,7 +297,9 @@ class static_variant {
287297
template<typename visitor>
288298
static typename visitor::result_type visit( tag_type tag, visitor& v, const void* data )
289299
{
290-
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(), "Unsupported type ${tag}!", ("tag",tag) );
300+
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(),
301+
"Unsupported type '${tag}', the number of supported types is ${count}! ",
302+
("tag",tag) ("count",count()) );
291303
return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) {
292304
return v(*reinterpret_cast<const typename decltype(t)::type*>(data));
293305
});
@@ -296,16 +308,20 @@ class static_variant {
296308
template<typename visitor>
297309
static typename visitor::result_type visit( tag_type tag, const visitor& v, const void* data )
298310
{
299-
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(), "Unsupported type ${tag}!", ("tag",tag) );
311+
FC_ASSERT( tag >= 0 && static_cast<size_t>(tag) < count(),
312+
"Unsupported type '${tag}', the number of supported types is ${count}! ",
313+
("tag",tag) ("count",count()) );
300314
return typelist::runtime::dispatch(list(), tag, [&v, data](auto t) {
301315
return v(*reinterpret_cast<const typename decltype(t)::type*>(data));
302316
});
303317
}
304318

305319
static constexpr size_t count() { return typelist::length<list>(); }
306320
void set_which( tag_type tag ) {
307-
FC_ASSERT( tag >= 0 );
308-
FC_ASSERT( static_cast<size_t>(tag) < count() );
321+
FC_ASSERT( tag >= 0, "Unable to set a negative tag '${tag}'", ("tag",tag) );
322+
FC_ASSERT( static_cast<size_t>(tag) < count(),
323+
"Unable to set tag '${tag}' when the number of supported tags is ${count}",
324+
("tag",tag) ("count",count()) );
309325
clean();
310326
init_from_tag(tag);
311327
}

0 commit comments

Comments
 (0)
Please sign in to comment.