text
stringlengths
0
2.2M
// USAGE EXAMPLE
// ----------------------------------------------------------------------------
///Usage
///-----
// In this section we show intended usage of this component.
//
///Example 1: Compile-Time Dispatch by Category Type
///- - - - - - - - - - - - - - - - - - - - - - - - -
// The 'bdlat_typecategory' framework provides facilities to control actions
// based on the 'bdlat' type category of the objects being used. Dispatching
// on type category can be achieved both at compile time and at runtime.
// Depending on that context, different facilities, having different
// restrictions/requirements are used. There are interesting differences when
// dealing with objects in the "dynamic" type category.
//
// This first example explores compile-time dispatch. Suppose we have an
// object that is compliant with one of the 'bdlat' type categories and that we
// wish to examine it to the extent of determining into which type category it
// falls and what value it has.
//
// First, we declare a 'printCategoryAndValue' function that has a template
// parameter 'TYPE':
//..
namespace BloombergLP {
namespace mine {
template <class TYPE>
void printCategoryAndValue(bsl::ostream& stream, const TYPE& object);
// Print the category of the specified 'object' followed by the value
// of 'object' to the specified output 'stream'.
//..
// Then, to implement this function, we will use a set of helper functions that
// are overloaded based on the category tag. The first set of helper functions
// address the category aspect of our assigned goal:
//..
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::Array)
{
stream << "Array";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::Choice)
{
stream << "Choice";
}
void printCategory(bsl::ostream& stream,
bdlat_TypeCategory::CustomizedType)
{
stream << "CustomizedType";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::DynamicType)
{
stream << "DynamicType";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::Enumeration)
{
stream << "Enumeration";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::NullableValue)
{
stream << "NullableValue";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::Sequence)
{
stream << "Sequence";
}
void printCategory(bsl::ostream& stream, bdlat_TypeCategory::Simple)
{
stream << "Simple";
}
//..
// Next, we implement another helper function template to handle the value
// aspect of our goal:
//..
template <class TYPE, class CATEGORY>
void printValue(bsl::ostream& stream,
const TYPE& object,
CATEGORY )
{
bdlb::PrintMethods::print(stream, object, 0, -1);
}
template <class TYPE>
void printValue(bsl::ostream& stream,
const TYPE& ,
bdlat_TypeCategory::DynamicType )
{
stream << "Printing dynamic types requires extra work.";
}
//..
// Notice that a partial specialization was created for objects falling into
// the "dynamic" category. Determining the value of such objects will be
// explored in {Example 3}.
//