va_list_ref
Overview
va_list_ref
is a helper class to store a reference to a std::va_list
starting with a call of macro va_start.
In C++98, there’s the lack of the std::va_copy
macro and thus only
references to std::va_list
types can be used. Unfortunately the handling of
these references is platform- specific. Therefore more than one implementation
is provided and va_list_ref is a typedef.
std::va_list
is a complete object type (in practice, a unique built-in type
or char*) suitable for holding the information needed by the macros
std::va_start
(enables access), std::va_copy
(makes a copy),
std::va_arg
(accesses the next argument), and std::va_end
(ends
traversal). These objects contains the pointer to the argument list which gets
passed to variadic functions (functions that can take a variable number of
arguments).
Example
This example shows how to create va_list_ref object as reference to variable :
uint32_t t = 17U;
estd::internal::_va_list_ref<uint32_t> cut(t);
EXPECT_EQ(17U, cut.get());
estd::internal::_va_list_ref<uint32_t> copy(cut);
EXPECT_EQ(17U, copy.get());
This example shows how to create va_list_ref object as reference to array :
using ArrayType = uint32_t[2];
uint32_t t[2] = {17U, 18U};
estd::internal::_va_list_ref<ArrayType> cut(t);
{
ArrayType& value = cut.get();
EXPECT_EQ(17U, value[0]);
EXPECT_EQ(18U, value[1]);
}
estd::internal::_va_list_ref<ArrayType> copy(cut);
{
ArrayType& value = copy.get();
EXPECT_EQ(17U, value[0]);
EXPECT_EQ(18U, value[1]);
}
This example shows how to create va_list_ref object to std::va_list as reference to std::va_list :
static char const* cptr = "a";
void testCopyConstructor(int start, ...)
{
va_list ap;
va_start(ap, start);
estd::va_list_ref ref(ap);
estd::va_list_ref cut(ref);
ASSERT_EQ(cptr, va_arg(cut.get(), char const*));
ASSERT_EQ(2U, va_arg(cut.get(), uint32_t));
ASSERT_EQ(45345ULL, va_arg(cut.get(), uint64_t));
va_end(ap);
}
testCopyConstructor(1, cptr, 2, 45345ULL);