text
stringlengths
0
2.2M
namespace BloombergLP {
namespace mine {
class MyDynamicType {
// This class can represent data in two forms: either a 'bsl::string'
// or as a 'bsl::vector' of 'char' values.
// PRIVATE DATA MEMBERS
bsl::vector<char> d_vectorChar; // Note: Production code should use a
bsl::string d_string; // union of object buffers.
int d_selector; // 0 = vectorChar, 1 = string
public:
// MANIPULATORS
void makeVectorChar() { d_selector = 0; }
void makeString() { d_selector = 1; }
bsl::vector<char>& theVectorChar()
{ ASSERT(isVectorChar()); return d_vectorChar; }
bsl::string& theString()
{ ASSERT(isString()); return d_string; }
// ACCESSORS
bool isVectorChar() const { return 0 == d_selector; }
bool isString() const { return 1 == d_selector; }
const bsl::vector<char>& theVectorChar() const
{ ASSERT(isVectorChar()); return d_vectorChar; }
const bsl::string& theString() const
{ ASSERT(isString()); return d_string; }
};
} // close package namespace
} // close enterprise namespace
//..
// When acting as a vector this class is a 'bdlat' "array" type and when
// holding a string, the class is a 'bdlat' "simple" type. Since this type can
// be in two type categories (determined at runtime) this class is deemed a
// "dynamic" class (for calculations at compile time).
//
// Then, to denote that this class is a dynamic type, we specialize the
// 'bdlat_TypeCategoryDeclareDynamic' meta-function in the 'BloombergLP'
// namespace:
//..
namespace BloombergLP {
template <>
struct bdlat_TypeCategoryDeclareDynamic<mine::MyDynamicType> {
enum { VALUE = 1 };
};
} // close enterprise namespace
//..
// Now, we define bdlat_typeCategorySelect', and a suite of four functions,
// 'bdlat_typeCategory(Manipulate|Access)(Array|Simple)', each overloaded for
// our type, 'MyDynamicType'.
//..
namespace BloombergLP {
namespace mine {
bdlat_TypeCategory::Value
bdlat_typeCategorySelect(const MyDynamicType& object)
{
if (object.isVectorChar()) {
return bdlat_TypeCategory::e_ARRAY_CATEGORY; // RETURN
}
else if (object.isString()) {
return bdlat_TypeCategory::e_SIMPLE_CATEGORY; // RETURN
}
ASSERT(!"Reached");
// Note that this 'return' is never reached and hence the returned
// value is immaterial.
return bdlat_TypeCategory::e_SIMPLE_CATEGORY;
}
template <class MANIPULATOR>
int bdlat_typeCategoryManipulateArray(MyDynamicType *object,
MANIPULATOR& manipulator)
{
if (object->isVectorChar()) {
return manipulator(&object->theVectorChar(),
bdlat_TypeCategory::Array()); // RETURN
}
return manipulator(object, bslmf::Nil());
}
template <class MANIPULATOR>
int bdlat_typeCategoryManipulateSimple(MyDynamicType *object,
MANIPULATOR& manipulator)
{
if (object->isString()) {
return manipulator(&object->theString(),
bdlat_TypeCategory::Simple()); // RETURN
}