text
stringlengths 0
2.2M
|
---|
// Now, we can implement the 'printCategoryAndValue' function in terms of the
|
// 'printCategory' and 'printValue' helper functions:
|
//..
|
template <class TYPE>
|
void printCategoryAndValue(bsl::ostream& stream, const TYPE& object)
|
{
|
typedef typename
|
bdlat_TypeCategory::Select<TYPE>::Type TypeCategory;
|
printCategory(stream, TypeCategory());
|
stream << ": ";
|
printValue(stream, object, TypeCategory());
|
}
|
} // close package namespace
|
} // close enterprise namespace
|
//..
|
// Finally, we can exercise the 'printCategoryAndValue' function on objects
|
// that fall in different (non-dynamic) type categories.
|
//..
|
using namespace BloombergLP;
|
void runUsageExample1()
|
{
|
bsl::ostringstream oss;
|
int intVal = 123;
|
mine::printCategoryAndValue(oss, intVal);
|
ASSERT("Simple: 123" == oss.str());
|
oss.str("");
|
bdlb::NullableValue<int> nullableInt;
|
mine::printCategoryAndValue(oss, nullableInt);
|
ASSERT("NullableValue: NULL" == oss.str());
|
oss.str("");
|
nullableInt = 321;
|
mine::printCategoryAndValue(oss, nullableInt);
|
ASSERT("NullableValue: 321" == oss.str());
|
oss.str("");
|
bsl::vector<int> vec;
|
vec.push_back(123);
|
vec.push_back(345);
|
vec.push_back(987);
|
mine::printCategoryAndValue(oss, vec);
|
ASSERT("Array: [ 123 345 987 ]" == oss.str());
|
}
|
//..
|
//
|
///Example 2: Run-Time Dispatch by 'bdlat_TypeCategoryUtil'
|
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// For run-time dispatching we can use the utility functions provided by
|
// 'bdlat_TypeCategoryUtil'. Suppose we wish to examine the type category and
|
// value of an arbitrary 'bdlat' compatible object, as we did in {Example 1}.
|
//
|
// First, we define 'mine::PrintAccessor', a functor that encapsulates the
|
// action to be taken:
|
//..
|
namespace BloombergLP {
|
namespace mine {
|
class PrintAccessor {
|
bsl::ostream *d_stream_p;
|
public:
|
PrintAccessor(bsl::ostream *stream)
|
: d_stream_p(stream) { ASSERT(stream); }
|
template <class TYPE>
|
int operator()(const TYPE& , bslmf::Nil )
|
{
|
*d_stream_p << "Nil";
|
return -1;
|
}
|
template <class TYPE>
|
int operator()(const TYPE& object, bdlat_TypeCategory::Array)
|
{
|
*d_stream_p << "Array" << ": ";
|
bdlb::PrintMethods::print(*d_stream_p, object, 0, -1);
|
return 0;
|
}
|
template <class TYPE>
|
int operator()(const TYPE& object, bdlat_TypeCategory::Choice)
|
{
|
*d_stream_p << "Choice" << ": ";
|
bdlb::PrintMethods::print(*d_stream_p, object, 0, -1);
|
return 0;
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.