TypeInfo
metaobjects represent types. Because C++ deals with
derived types such as pointer types and array types, Class
metaobjects are not used for primary representation of types. TypeInfo
metaobjects do not treat typedef
ed
types as independent types. They are treated just as aliases of
the original types.
ClassMetaobject()
Dereference(TypeInfo& t)
Dereference()
FullTypeName()
IsArray()
IsBuiltInType()
IsClass(Class*& metaobject)
IsConst()
IsEllipsis()
IsEnum()
IsEnum(Ptree*& spec)
IsFunction()
IsNoReturnType()
IsPointerToMember()
IsPointerType()
IsReferenceType()
IsTemplateClass()
IsVolatile()
MakePtree(Ptree* varname = nil)
NthArgument(int nth, TypeInfo& t)
NumOfArguments()
Reference(TypeInfo& t)
Reference()
WhatIs()
The followings are member functions on TypeInfo
metaobjects:
TypeInfoId WhatIs()
This returns an enum
constant that corresponds to the kind of
the type: BuiltInType
, ClassType
(including class
, struct
, and union
), EnumType
, TemplateType
, PointerType
, ReferenceType
, PointerToMemberType
, ArrayType
, FunctionType
, TemplateType
,
or UndefType
(the type is unknown).
Ptree* FullTypeName()
This returns the full name of the type if the type is a built-in type,
a class type, an enum
type, or a template class type. Otherwise,
this returns nil
.
For example, if the type is a nested class Y
defined within
a class X
, this returns X::Y
.
bool IsConst()
This returns true
if the type is const.
bool IsVolatile()
This returns true
if the type is volatile.
uint IsBuiltInType()
This returns a bit field that represents what the built-in type is.
If the type is not a built-in type, it simply returns 0
(false
).
To test the bit field, these masks are available: CharType
, IntType
, ShortType
, LongType
, SignedType
, UnsignedType
, FloatType
, DoubleType
, LongDoubleType
, BooleanType
, and VoidType
.
For example, IsBuiltInType() & LongType
is true
if
the type is long
, unsigned long
, or signed long
.
bool IsPointerType()
This returns true
if the type is a pointer type.
bool IsReferenceType()
This returns true
if the type is a reference type.
bool IsFunction()
This returns true
if the type is a function type.
To obtain the type of the returned value, examine the dereferenced type of the function type.
bool IsArray()
This returns true
if the type is an array type.
To obtain the type of the array components, examine the dereferenced type of the array type.
bool IsPointerToMember()
This returns true
if the type is a pointer to member.
bool IsTemplateClass()
This returns true
if the type is a class template.
bool IsEnum()
This returns true
if the type is an enum type.
bool IsEnum(Ptree*& spec)
This returns true
if the type is an enum type.
The Ptree
metaobject representing the enum
declaration
is stored in spec
.
bool IsClass(Class*& metaobject)
This returns true
if the type is a class type.
The Class
metaobject representing the class type is stored
in metaobject
.
Class* ClassMetaobject()
This returns a Class
metaobject that represent the type.
If the type is not a class
type, it simply returns nil
.
The TypeInfo
metaobjects also provide methods for computing
the dereferenced type. For example, those methods are used to get the type
of the value that a pointer points to. Suppose that the type of the pointer
is int*
. If the dereferenced type of that pointer type is
computed, then int
is obtained.
void Dereference(TypeInfo& t)
This returns the dereferenced type in t
.
If dereferencing is not possible, the Undef
type is returned
in t
.
void Dereference()
This is identical to Dereference(TypeInfo&)
except that
the TypeInfo
metaobject itself is changed to represent
the dereferenced type.
void Reference(TypeInfo& t)
This returns the referenced type in t
.
For example, if the type is int*
, then the referenced type is int**
.
void Reference()
This is identical to Reference(TypeInfo&)
except that
the TypeInfo
metaobject itself is changed to represent
the referenced type.
The dereferenced type of a function type is the type of the
return value. For example, if the function type is void f(char)
,
then the dereferenced type is void
. If no return type is
specified (e.g. constructors), the dereferenced type of the function type
is ``no return type.''
bool IsNoReturnType()
This returns true
if the return type of the function
is not specified.
The TypeInfo
metaobjects also provide a method to obtain
the types of function arguments.
int NumOfArguments()
This returns the number of the arguments. If the type is not a function type, then it returns -1.
bool NthArgument(int nth, TypeInfo& t)
If the type is FunctionType
,
this returns the type of the nth
(>= 0) argument
in t
.
If the type is not FunctionType
or the nth
argument
does not exist, this function returns false
.
If the nth
argument is ...
(ellipses), then the returned
type is an ellipsis type (see below.)
bool IsEllipsis()
This returns true
if the type is an ellipsis type.
Finally, we show a convenient method for constructing
a Ptree
metaobject that represents the declaration of
a variable of the type.
Ptree* MakePtree(Ptree* varname = nil)
This makes a Ptree
metaobject that represents the
declaration (or a function) of a variable of the type.
For example, if the type is pointer to integer, this returns [int *
varname ]
. varname
may be nil
.
If the type is a function type, MakePtree()
returns
a function prototype matching that type. The argument names in the
prototype is omitted. To construct a function prototype including
argument names, programmers need to write as follows. Suppose
that ftype
is the type of the function, atype
is the
type of the argument, the function name is Set
,
and the argument name is width
:
Ptree* arg = atype.MakePtree(Ptree::Make("width"));
Ptree* func = Ptree::qMake("Set(`arg`)");
ftype.Dereference();
Ptree* proto = ftype.MakePtree(func); // function prototype