aboutsummaryrefslogtreecommitdiff
path: root/paludis/package_id.cc
blob: 0707b7fe9d3bd0cf80d44541f1f3b654d09674e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/iterator.hh>
#include <paludis/util/tr1_functional.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/sequence-impl.hh>
#include <paludis/util/set.hh>
#include <paludis/util/set-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/name.hh>
#include <paludis/version_spec.hh>
#include <paludis/repository.hh>
#include <paludis/package_database.hh>
#include <paludis/hashed_containers.hh>

#include <list>
#include <algorithm>

#include <libwrapiter/libwrapiter_forward_iterator.hh>

using namespace paludis;

#include <paludis/package_id-se.cc>

template class Sequence<tr1::shared_ptr<const PackageID> >;
template class Set<tr1::shared_ptr<const PackageID>, PackageIDSetComparator>;

namespace paludis
{
    template <>
    struct Implementation<PackageID>
    {
        mutable std::list<tr1::shared_ptr<const MetadataKey> > keys;
        mutable std::list<tr1::shared_ptr<const Mask> > masks;
    };
}

PackageID::PackageID() :
    PrivateImplementationPattern<PackageID>(new Implementation<PackageID>)
{
}

PackageID::~PackageID()
{
}

void
PackageID::add_metadata_key(const tr1::shared_ptr<const MetadataKey> & k) const
{
    using namespace tr1::placeholders;

    if (_imp->keys.end() != std::find_if(_imp->keys.begin(), _imp->keys.end(),
                tr1::bind(std::equal_to<std::string>(), k->raw_name(), tr1::bind(tr1::mem_fn(&MetadataKey::raw_name), _1))))
        throw ConfigurationError("Tried to add duplicate key '" + k->raw_name() + "' to ID '" + stringify(*this) + "'");

    _imp->keys.push_back(k);
}

PackageID::MetadataIterator
PackageID::begin_metadata() const
{
    need_keys_added();
    return MetadataIterator(_imp->keys.begin());
}

PackageID::MetadataIterator
PackageID::end_metadata() const
{
    need_keys_added();
    return MetadataIterator(_imp->keys.end());
}

void
PackageID::add_mask(const tr1::shared_ptr<const Mask> & k) const
{
    _imp->masks.push_back(k);
}

PackageID::MasksIterator
PackageID::begin_masks() const
{
    need_masks_added();
    return MasksIterator(_imp->masks.begin());
}

PackageID::MasksIterator
PackageID::end_masks() const
{
    need_masks_added();
    return MasksIterator(_imp->masks.end());
}

bool
PackageID::masked() const
{
    return begin_masks() != end_masks();
}

void
PackageID::invalidate_masks() const
{
    _imp->masks.clear();
}

PackageID::MetadataIterator
PackageID::find_metadata(const std::string & s) const
{
    using namespace tr1::placeholders;

    need_keys_added();
    return std::find_if(begin_metadata(), end_metadata(),
            tr1::bind(std::equal_to<std::string>(), s, tr1::bind(tr1::mem_fn(&MetadataKey::raw_name), _1)));
}

std::ostream &
paludis::operator<< (std::ostream & s, const PackageID & i)
{
    s << i.canonical_form(idcf_full);
    return s;
}

bool
PackageIDSetComparator::operator() (const tr1::shared_ptr<const PackageID> & a,
        const tr1::shared_ptr<const PackageID> & b) const
{
    if (a->name() < b->name())
        return true;

    if (a->name() > b->name())
        return false;

    if (a->version() < b->version())
        return true;

    if (a->version() > b->version())
        return false;

    if (a->repository()->name().data() < b->repository()->name().data())
        return true;

    if (a->repository()->name().data() > b->repository()->name().data())
        return false;

    return a->arbitrary_less_than_comparison(*b);
}

bool
paludis::operator== (const PackageID & a, const PackageID & b)
{
    return (a.name() == b.name())
        && (a.version() == b.version())
        && (a.repository()->name() == b.repository()->name())
        && (! a.arbitrary_less_than_comparison(b))
        && (! b.arbitrary_less_than_comparison(a));
}

namespace paludis
{
    template <>
    struct Implementation<PackageIDComparator>
    {
        MakeHashedMap<RepositoryName, unsigned>::Type m;
    };
}

PackageIDComparator::PackageIDComparator(const PackageDatabase * const db) :
    PrivateImplementationPattern<PackageIDComparator>(new Implementation<PackageIDComparator>)
{
    unsigned c(0);
    for (PackageDatabase::RepositoryIterator r(db->begin_repositories()),
            r_end(db->end_repositories()) ; r != r_end ; ++r)
        _imp->m.insert(std::make_pair((*r)->name(), ++c));
}

PackageIDComparator::~PackageIDComparator()
{
}

bool
PackageIDComparator::operator() (const tr1::shared_ptr<const PackageID> & a,
        const tr1::shared_ptr<const PackageID> & b) const
{
    if (a->name() < b->name())
        return true;

    if (a->name() > b->name())
        return false;

    if (a->version() < b->version())
        return true;

    if (a->version() > b->version())
        return false;

    MakeHashedMap<RepositoryName, unsigned>::Type::const_iterator
        ma(_imp->m.find(a->repository()->name())),
        mb(_imp->m.find(b->repository()->name()));

    if (ma == _imp->m.end() || mb == _imp->m.end())
        throw InternalError(PALUDIS_HERE, "Repository not in database");

    if (ma->second < mb->second)
        return true;
    if (ma->second > mb->second)
        return false;

    return a->arbitrary_less_than_comparison(*b);
}