2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "TypeComponentPath.h"
13 #include "StringUtils.h"
16 // #pragma mark - TypeComponent
20 TypeComponent::HasPrefix(const TypeComponent
& other
) const
25 return componentKind
== TYPE_COMPONENT_ARRAY_ELEMENT
26 && other
.componentKind
== TYPE_COMPONENT_ARRAY_ELEMENT
27 && name
.Compare(other
.name
, other
.name
.Length()) == 0;
32 TypeComponent::HashValue() const
34 uint32 hash
= ((uint32
)index
<< 8) | (componentKind
<< 4) | typeKind
;
35 return StringUtils::HashValue(name
) * 13 + hash
;
40 TypeComponent::Dump() const
58 case TYPE_ENUMERATION
:
67 case TYPE_UNSPECIFIED
:
68 printf("unspecified");
73 case TYPE_POINTER_TO_MEMBER
:
74 printf("pointer to member");
80 switch (componentKind
) {
81 case TYPE_COMPONENT_UNDEFINED
:
84 case TYPE_COMPONENT_BASE_TYPE
:
85 printf("base %" B_PRIu64
" \"%s\"", index
, name
.String());
87 case TYPE_COMPONENT_DATA_MEMBER
:
88 printf("member %" B_PRIu64
" \"%s\"", index
, name
.String());
90 case TYPE_COMPONENT_ARRAY_ELEMENT
:
91 printf("element %" B_PRIu64
" \"%s\"", index
, name
.String());
98 TypeComponent::operator==(const TypeComponent
& other
) const
100 return componentKind
== other
.componentKind
101 && typeKind
== other
.typeKind
102 && index
== other
.index
103 && name
== other
.name
;
107 // #pragma mark - TypeComponentPath
110 TypeComponentPath::TypeComponentPath()
112 fComponents(10, true)
117 TypeComponentPath::TypeComponentPath(const TypeComponentPath
& other
)
119 fComponents(10, true)
125 TypeComponentPath::~TypeComponentPath()
131 TypeComponentPath::CountComponents() const
133 return fComponents
.CountItems();
138 TypeComponentPath::ComponentAt(int32 index
) const
140 TypeComponent
* component
= fComponents
.ItemAt(index
);
141 return component
!= NULL
? *component
: TypeComponent();
146 TypeComponentPath::AddComponent(const TypeComponent
& component
)
148 TypeComponent
* myComponent
= new(std::nothrow
) TypeComponent(component
);
149 if (myComponent
== NULL
|| !fComponents
.AddItem(myComponent
)) {
159 TypeComponentPath::Clear()
161 fComponents
.MakeEmpty();
166 TypeComponentPath::CreateSubPath(int32 componentCount
) const
168 if (componentCount
< 0 || componentCount
> fComponents
.CountItems())
169 componentCount
= fComponents
.CountItems();
171 TypeComponentPath
* path
= new(std::nothrow
) TypeComponentPath
;
174 BReference
<TypeComponentPath
> pathReference(path
, true);
176 for (int32 i
= 0; i
< componentCount
; i
++) {
177 if (!path
->AddComponent(*fComponents
.ItemAt(i
)))
181 return pathReference
.Detach();
186 TypeComponentPath::HashValue() const
188 int32 count
= fComponents
.CountItems();
192 uint32 hash
= fComponents
.ItemAt(0)->HashValue();
194 for (int32 i
= 1; i
< count
; i
++)
195 hash
= hash
* 17 + fComponents
.ItemAt(i
)->HashValue();
202 TypeComponentPath::Dump() const
204 int32 count
= fComponents
.CountItems();
205 for (int32 i
= 0; i
< count
; i
++) {
210 fComponents
.ItemAt(i
)->Dump();
217 TypeComponentPath::operator=(const TypeComponentPath
& other
)
219 if (this != &other
) {
220 fComponents
.MakeEmpty();
223 TypeComponent
* component
= other
.fComponents
.ItemAt(i
); i
++) {
224 if (!AddComponent(*component
))
234 TypeComponentPath::operator==(const TypeComponentPath
& other
) const
236 int32 count
= fComponents
.CountItems();
237 if (count
!= other
.fComponents
.CountItems())
240 for (int32 i
= 0; i
< count
; i
++) {
241 if (*fComponents
.ItemAt(i
) != *other
.fComponents
.ItemAt(i
))