Provide string representations for data type names.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide string representations for data type names.
-
- Classes:
-
- See also:
- http://www.w3.org/TR/xmlschema-2/#built-in-datatypes
-
- Description:
- This component defines a structure
bdlat_TypeName
which provides a namespace for functions returning information about the object types. Functions in this namespace allow users to get access to three categories of information:
-
class name from the metadata associated with the object type
-
generic type name
-
XML/XSD type name, based on object type and formatting mode.
-
- Class Name Information:
- The template function
className
returns the object class name from the metadata associated with given object type. Metadata is available for the C++ types that have one the following traits: 'bdlat_TypeTraitBasicChoice'
'bdlat_TypeTraitBasicSequence'
'bdlat_TypeTraitBasicCustomizedType'
'bdlat_TypeTraitBasicEnumeration'
If metadata is not available for the object type, the function className
returns 0 unless the function bdlat_TypeName_className
is overloaded by developer.
-
- Overloable Class Name Functions For User Defined Classes:
- To provide the custom name for the given user-defined C++ class, the developer should overload the template function
bdlat_TypeName_className
for this type in the namespace where the type is defined.
- WARNING! Do not extend
bdlat_TypeName_Overloadable
namespace.
-
- Generic Type Name Information:
- The template functions
name
returns the generic type name for the given object. The generic type name is one of the following: o predefined name for fundamental types
o class name from 'bdlat_TypeName_className', if such function returns a
non-null value
o name obtained from 'type_info' object provided by C++ runtime, if no
class name is available
-
- XSD Type Name Information:
- The template functions
xsdName
returns the XML/XSD type name, based on the object type and formatting mode. The returned value is one of the following: o predefined name for built-in XSD types
o class name from 'bdlat_TypeName_className', if such function returns
a non-null value
o the "anyType" string, if no class name is available
This component also defines the XSD names for the following C++ types and formatting modes:
-
- Usage:
- We begin by creating abbreviations for formatting modes and by declaring objects of a number of types:
int main() {
static const int DEFAULT = bdlat_FormattingMode::DEFAULT;
static const int DEC = bdlat_FormattingMode::DEC;
static const int HEX = bdlat_FormattingMode::HEX;
static const int BASE64 = bdlat_FormattingMode::BASE64;
static const int TEXT = bdlat_FormattingMode::TEXT;
short theShort;
unsigned theUint;
float theFloat;
const char *theCharPtr;
bsl::string theString;
bdlt::Date theDate;
bdlt::DatetimeTz theDatetime;
bsl::vector<char> theCharVector;
bsl::vector<bsl::string> theStrVector;
None of these types are generated types with metadata, so className
will return a null pointer for each of them: The name
function will never return a null pointer. For each of the fundamental and vocabulary types, it returns the known type name. For vector types, it returns the appropriate "vector<X>" string: Each of the above types except vector<string>
has one or more corresponding XSD types. The XSD type is affected by a formatting mode so that, for example, a vector<char>
can be represented as a text string (formatting mode TEXT
) or as a sequence of binary bytes (formatting mode HEX
or BASE64
). assert(0 == bsl::strcmp("short",
bdlat_TypeName::xsdName(theShort, DEFAULT)));
assert(0 == bsl::strcmp("unsignedInt",
bdlat_TypeName::xsdName(theUint, DEFAULT)));
assert(0 == bsl::strcmp("float",
bdlat_TypeName::xsdName(theFloat, DEFAULT)));
assert(0 == bsl::strcmp("decimal",
bdlat_TypeName::xsdName(theFloat, DEC)));
assert(0 == bsl::strcmp("base64Binary",
bdlat_TypeName::xsdName(theCharVector, DEFAULT)));
assert(0 == bsl::strcmp("string",
bdlat_TypeName::xsdName(theCharVector, TEXT)));
For types that have not corresponding XSD type, xsdName
returns "anyType", regardless of formatting mode: If we create our own class: namespace MyNamespace {
class MyClass {
};
Then we can assign it a printable name by overloading the bdlat_TypeName_className
function in the class's namespace: Note that bdlat_TypeName_className
must return a string that is valid and does not change for remaining duration the program. The overloaded bdlat_TypeName_className
function is automatically used for name
and xsdName
, as well as for className
: int main()
{
static const int DEFAULT = bdlat_FormattingMode::DEFAULT;
MyNamespace::MyClass myClassObj;
assert(0 == bsl::strcmp("MyClass",
bdlat_TypeName::className(myClassObj)));
assert(0 == bsl::strcmp("MyClass", bdlat_TypeName::name(myClassObj)));
assert(0 == bsl::strcmp("MyClass",
bdlat_TypeName::xsdName(myClassObj, DEFAULT)));
return 0;
}