text
stringlengths
0
2.2M
return manipulator(object, bslmf::Nil());
}
template <class ACCESSOR>
int bdlat_typeCategoryAccessArray(const MyDynamicType& object,
ACCESSOR& accessor)
{
if (object.isVectorChar()) {
return accessor(object.theVectorChar(),
bdlat_TypeCategory::Array()); // RETURN
}
return accessor(object, bslmf::Nil());
}
template <class ACCESSOR>
int bdlat_typeCategoryAccessSimple(const MyDynamicType& object,
ACCESSOR& accessor)
{
if (object.isString()) {
return accessor(object.theString(),
bdlat_TypeCategory::Simple()); // RETURN
}
return accessor(object, bslmf::Nil());
}
} // close package namespace
} // close enterprise namespace
//..
// Notice that the customization points were implemented for just the two type
// categories that 'MyDynamicType' can achieve: "array" and "simple".
//
// Finally, we can see how the facilities we developed in {Example 1} and
// {Example 2} behave when given a "dynamic" type;
//..
void runUsageExample3()
{
//..
// We see that the 'Select' meta-function returns the expected value:
//..
ASSERT(bdlat_TypeCategory::e_DYNAMIC_CATEGORY
== static_cast<bdlat_TypeCategory::Value>(
bdlat_TypeCategory::Select<mine::MyDynamicType>::e_SELECTION));
//..
// We create an object of our dynamic type and observe that the specialization
// we created for printing the values (actually, for *not* printing the value
// of) "dynamic" types is invoked:
//..
bsl::ostringstream oss;
mine::MyDynamicType object;
mine::printCategoryAndValue(oss, object);
ASSERT("DynamicType: Printing dynamic types requires extra work."
== oss.str());
oss.str("");
//..
// We instruct object to behave as a vector and see that the 'bdlat' framework
// treats the object as a member of the "array" category and the
// 'PrintAccessor' we defined in {Example 2} treats 'object' as a member of the
// "array" category:
//..
object.makeVectorChar();
ASSERT(bdlat_TypeCategory::e_ARRAY_CATEGORY
== bdlat_TypeCategoryFunctions::select(object));
object.theVectorChar().push_back('H');
object.theVectorChar().push_back('e');
object.theVectorChar().push_back('l');
object.theVectorChar().push_back('l');
object.theVectorChar().push_back('o');
mine::PrintAccessor accessor(&oss);
int ret;
ret = bdlat_TypeCategoryUtil::accessByCategory(object, accessor);
ASSERT(0 == ret);
LOOP_ASSERT(oss.str(), "Array: \"Hello\"" == oss.str());
oss.str("");
//..
// Lastly, we instruct 'object' to behave as a string and find that the 'bdlat'
// framework now considers 'object' to be the "simple" category:
//..
object.makeString();
ASSERT(bdlat_TypeCategory::e_SIMPLE_CATEGORY
== bdlat_TypeCategoryFunctions::select(object));
object.theString() = "World";
ret = bdlat_TypeCategoryUtil::accessByCategory(object, accessor);
ASSERT(0 == ret);
ASSERT("Simple: World" == oss.str());
oss.str("");
}
//..
// Notice that the output of the accessor matches the state of the object,
// reporting an "array" type when the object 'isVector' and a "simple" type
// when the object 'isString'.