Changeset View
Changeset View
Standalone View
Standalone View
duchain/types/indexedcontainer.cpp
- This file was added.
1 | /** | ||||
---|---|---|---|---|---|
2 | This file is part of KDevelop | ||||
3 | Copyright (C) 2011 Sven Brauch <svenbrauch@googlemail.com> | ||||
4 | | ||||
5 | This program is free software: you can redistribute it and/or modify | ||||
6 | it under the terms of the GNU General Public License as published by | ||||
7 | the Free Software Foundation, either version 2 of the License, or | ||||
8 | (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | GNU General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License | ||||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
17 | **/ | ||||
18 | | ||||
19 | | ||||
20 | #include "indexedcontainer.h" | ||||
21 | | ||||
22 | #include <language/duchain/types/typeregister.h> | ||||
23 | #include <language/duchain/duchain.h> | ||||
24 | #include <language/duchain/duchainlock.h> | ||||
25 | | ||||
26 | #include <KLocalizedString> | ||||
27 | | ||||
28 | using namespace KDevelop; | ||||
29 | | ||||
30 | namespace Php { | ||||
31 | | ||||
32 | DEFINE_LIST_MEMBER_HASH(IndexedContainerData, m_values, IndexedType) | ||||
33 | REGISTER_TYPE(IndexedContainer); | ||||
34 | | ||||
35 | IndexedContainer::IndexedContainer() : Php::StructureType(createData<IndexedContainer>()) | ||||
36 | { | ||||
37 | | ||||
38 | } | ||||
39 | | ||||
40 | IndexedContainer::IndexedContainer(const IndexedContainer& rhs) | ||||
41 | : StructureType(copyData<IndexedContainer>(*rhs.d_func())) | ||||
42 | { | ||||
43 | | ||||
44 | } | ||||
45 | | ||||
46 | IndexedContainer::IndexedContainer(IndexedContainerData& data) | ||||
47 | : StructureType(data) | ||||
48 | { | ||||
49 | | ||||
50 | } | ||||
51 | | ||||
52 | void IndexedContainer::addEntry(AbstractType::Ptr typeToAdd) | ||||
53 | { | ||||
54 | Q_ASSERT(typeToAdd && "trying to add a null type to indexedContainer"); | ||||
55 | d_func_dynamic()->m_valuesList().append(typeToAdd->indexed()); | ||||
56 | } | ||||
57 | | ||||
58 | const IndexedType& IndexedContainer::typeAt(int index) const | ||||
59 | { | ||||
60 | Q_ASSERT((uint) index < d_func()->m_valuesSize()); | ||||
61 | return d_func()->m_values()[index]; | ||||
62 | } | ||||
63 | | ||||
64 | void IndexedContainer::replaceType(int index, AbstractType::Ptr newType) | ||||
65 | { | ||||
66 | Q_ASSERT((uint) index < d_func()->m_valuesSize()); | ||||
67 | d_func_dynamic()->m_valuesList()[index] = newType->indexed(); | ||||
68 | } | ||||
69 | | ||||
70 | KDevelop::AbstractType* IndexedContainer::clone() const | ||||
71 | { | ||||
72 | IndexedContainer* n = new IndexedContainer(*this); | ||||
73 | return n; | ||||
74 | } | ||||
75 | | ||||
76 | QString IndexedContainer::toString() const | ||||
77 | { | ||||
78 | QString prefix = Php::StructureType::toString(); | ||||
79 | QStringList typesArray; | ||||
80 | for ( int i = 0; i < typesCount(); i++ ) { | ||||
81 | if ( i >= 5 ) { | ||||
82 | // Don't print more than five types explicitly | ||||
83 | typesArray << "..."; | ||||
84 | break; | ||||
85 | } | ||||
86 | typesArray << typeAt(i).abstractType()->toString(); | ||||
87 | } | ||||
88 | const QString contentType = QStringLiteral("(") + typesArray.join(", ") + ")"; | ||||
89 | return i18nc("as in list of int, set of string", "%1 of %2", prefix, contentType); | ||||
90 | } | ||||
91 | | ||||
92 | QString IndexedContainer::containerToString() const | ||||
93 | { | ||||
94 | return Php::StructureType::toString(); | ||||
95 | } | ||||
96 | | ||||
97 | int IndexedContainer::typesCount() const | ||||
98 | { | ||||
99 | return d_func()->m_valuesSize(); | ||||
100 | } | ||||
101 | | ||||
102 | bool IndexedContainer::equals(const AbstractType* rhs) const | ||||
103 | { | ||||
104 | if ( this == rhs ) { | ||||
105 | return true; | ||||
106 | } | ||||
107 | if ( ! Php::StructureType::equals(rhs) ) { | ||||
108 | return false; | ||||
109 | } | ||||
110 | const IndexedContainer* c = dynamic_cast<const IndexedContainer*>(rhs); | ||||
111 | if ( ! c ) { | ||||
112 | return false; | ||||
113 | } | ||||
114 | if ( typesCount() != c->typesCount() ) { | ||||
115 | return false; | ||||
116 | } | ||||
117 | for ( int i = 0; i < typesCount(); i++ ) { | ||||
118 | if ( c->typeAt(i) != typeAt(i) ) { | ||||
119 | return false; | ||||
120 | } | ||||
121 | } | ||||
122 | return true; | ||||
123 | } | ||||
124 | | ||||
125 | uint IndexedContainer::hash() const | ||||
126 | { | ||||
127 | uint h = StructureType::hash(); | ||||
128 | for ( uint i = 0; i < d_func()->m_valuesSize(); i++ ) { | ||||
129 | h += i*d_func()->m_values()[i]; | ||||
130 | } | ||||
131 | return h; | ||||
132 | } | ||||
133 | | ||||
134 | } |