aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/fake/fake_package_id.cc
blob: ede593c0db55df2f5e0964ff7a2aed4335f7a804 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Ciaran McCreesh
 *
 * 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/repositories/fake/fake_package_id.hh>
#include <paludis/repositories/fake/fake_repository_base.hh>
#include <paludis/repositories/e/dep_parser.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/name.hh>
#include <paludis/action.hh>
#include <paludis/environment.hh>
#include <paludis/version_spec.hh>
#include <paludis/formatter.hh>
#include <paludis/literal_metadata_key.hh>
#include <paludis/dep_spec.hh>
#include <paludis/hashed_containers.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/set.hh>
#include <paludis/util/create_iterator-impl.hh>
#include <paludis/util/save.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/make_shared_ptr.hh>

#include <list>
#include <sstream>

using namespace paludis;

namespace paludis
{
#ifndef PALUDIS_NO_DOUBLE_TEMPLATE
    template <>
#endif
    template <typename C_>
    struct Implementation<FakeMetadataCollectionKey<C_> >
    {
        tr1::shared_ptr<C_> collection;
        const PackageID * const id;
        const Environment * const env;

        Implementation(const PackageID * const i, const Environment * const e) :
            id(i),
            env(e)
        {
        }
    };
}

template <typename C_>
FakeMetadataCollectionKey<C_>::FakeMetadataCollectionKey(
        const std::string & r, const std::string & h, const MetadataKeyType t, const PackageID * const i,
        const Environment * const e) :
    MetadataCollectionKey<C_>(r, h, t),
    PrivateImplementationPattern<FakeMetadataCollectionKey<C_> >(new Implementation<FakeMetadataCollectionKey<C_> >(i, e)),
    _imp(PrivateImplementationPattern<FakeMetadataCollectionKey<C_> >::_imp)
{
}

template <typename C_>
FakeMetadataCollectionKey<C_>::~FakeMetadataCollectionKey()
{
}

template <typename C_>
const tr1::shared_ptr<const C_>
FakeMetadataCollectionKey<C_>::value() const
{
    return _imp->collection;
}

FakeMetadataKeywordSetKey::FakeMetadataKeywordSetKey(const std::string & r,
        const std::string & h, const std::string & v, const MetadataKeyType t,
        const PackageID * const i, const Environment * const e) :
    FakeMetadataCollectionKey<KeywordNameSet>(r, h, t, i, e)
{
    set_from_string(v);
}

void
FakeMetadataKeywordSetKey::set_from_string(const std::string & s)
{
    _imp->collection.reset(new KeywordNameSet);
    tokenise_whitespace(s, create_inserter<KeywordName>(_imp->collection->inserter()));
}

FakeMetadataIUseSetKey::FakeMetadataIUseSetKey(const std::string & r,
        const std::string & h, const std::string & v, const IUseFlagParseMode m, const MetadataKeyType t,
        const PackageID * const i, const Environment * const e) :
    FakeMetadataCollectionKey<IUseFlagSet>(r, h, t, i, e)
{
    set_from_string(v, m);
}

void
FakeMetadataIUseSetKey::set_from_string(const std::string & s, const IUseFlagParseMode m)
{
    _imp->collection.reset(new IUseFlagSet);
    std::list<std::string> tokens;
    tokenise_whitespace(s, std::back_inserter(tokens));
    for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
            t != t_end ; ++t)
        _imp->collection->insert(IUseFlag(*t, m, std::string::npos));
}

namespace paludis
{
#ifndef PALUDIS_NO_DOUBLE_TEMPLATE
    template <>
#endif
    template <typename C_>
    struct Implementation<FakeMetadataSpecTreeKey<C_> >
    {
        tr1::shared_ptr<const typename C_::ConstItem> value;
        std::string string_value;
        const tr1::function<const tr1::shared_ptr<const typename C_::ConstItem> (const std::string &)> func;

        Implementation(const tr1::function<const tr1::shared_ptr<const typename C_::ConstItem> (const std::string &)> & f) :
            func(f)
        {
        }
    };

    template <>
    struct Implementation<FakeMetadataSpecTreeKey<FetchableURISpecTree> >
    {
        tr1::shared_ptr<const FetchableURISpecTree::ConstItem> value;
        std::string string_value;
        const tr1::function<const tr1::shared_ptr<const FetchableURISpecTree::ConstItem> (const std::string &)> func;
        tr1::shared_ptr<const URILabel> initial_label;

        Implementation(const tr1::function<const tr1::shared_ptr<const FetchableURISpecTree::ConstItem> (const std::string &)> & f) :
            func(f),
            initial_label(new URIListedThenMirrorsLabel("listed-then-mirrors"))
        {
        }
    };
}

template <typename C_>
FakeMetadataSpecTreeKey<C_>::FakeMetadataSpecTreeKey(const std::string & r, const std::string & h, const std::string & v,
        const tr1::function<const tr1::shared_ptr<const typename C_::ConstItem> (const std::string &)> & f, const MetadataKeyType t) :
    MetadataSpecTreeKey<C_>(r, h, t),
    PrivateImplementationPattern<FakeMetadataSpecTreeKey<C_> >(new Implementation<FakeMetadataSpecTreeKey<C_> >(f)),
    _imp(PrivateImplementationPattern<FakeMetadataSpecTreeKey<C_> >::_imp)
{
    set_from_string(v);
}

template <typename C_>
FakeMetadataSpecTreeKey<C_>::~FakeMetadataSpecTreeKey()
{
}

template <typename C_>
void
FakeMetadataSpecTreeKey<C_>::set_from_string(const std::string & s)
{
    _imp->string_value = s;
    _imp->value = _imp->func(s);
}

template <typename C_>
const tr1::shared_ptr<const typename C_::ConstItem>
FakeMetadataSpecTreeKey<C_>::value() const
{
    return _imp->value;
}

template <typename C_>
std::string
FakeMetadataSpecTreeKey<C_>::pretty_print(const typename C_::ItemFormatter &) const
{
    return _imp->string_value;
}

template <typename C_>
std::string
FakeMetadataSpecTreeKey<C_>::pretty_print_flat(const typename C_::ItemFormatter &) const
{
    return _imp->string_value;
}

FakeMetadataSpecTreeKey<FetchableURISpecTree>::FakeMetadataSpecTreeKey(const std::string & r, const std::string & h, const std::string & v,
        const tr1::function<const tr1::shared_ptr<const FetchableURISpecTree::ConstItem> (const std::string &)> & f, const MetadataKeyType t) :
    MetadataSpecTreeKey<FetchableURISpecTree>(r, h, t),
    PrivateImplementationPattern<FakeMetadataSpecTreeKey<FetchableURISpecTree> >(
            new Implementation<FakeMetadataSpecTreeKey<FetchableURISpecTree> >(f)),
    _imp(PrivateImplementationPattern<FakeMetadataSpecTreeKey<FetchableURISpecTree> >::_imp)
{
    set_from_string(v);
}

FakeMetadataSpecTreeKey<FetchableURISpecTree>::~FakeMetadataSpecTreeKey()
{
}

void
FakeMetadataSpecTreeKey<FetchableURISpecTree>::set_from_string(const std::string & s)
{
    _imp->string_value = s;
    _imp->value = _imp->func(s);
}

const tr1::shared_ptr<const FetchableURISpecTree::ConstItem>
FakeMetadataSpecTreeKey<FetchableURISpecTree>::value() const
{
    return _imp->value;
}

std::string
FakeMetadataSpecTreeKey<FetchableURISpecTree>::pretty_print(const FetchableURISpecTree::ItemFormatter &) const
{
    return _imp->string_value;
}

std::string
FakeMetadataSpecTreeKey<FetchableURISpecTree>::pretty_print_flat(const FetchableURISpecTree::ItemFormatter &) const
{
    return _imp->string_value;
}

const tr1::shared_ptr<const URILabel>
FakeMetadataSpecTreeKey<FetchableURISpecTree>::initial_label() const
{
    return _imp->initial_label;
}

namespace paludis
{
    template <>
    struct Implementation<FakeUnacceptedMask>
    {
        const char key;
        const std::string description;
        const tr1::shared_ptr<const MetadataKey> unaccepted_key;

        Implementation(const char k, const std::string & d, const tr1::shared_ptr<const MetadataKey> & u) :
            key(k),
            description(d),
            unaccepted_key(u)
        {
        }
    };
}

FakeUnacceptedMask::FakeUnacceptedMask(const char c, const std::string & s, const tr1::shared_ptr<const MetadataKey> & k) :
    PrivateImplementationPattern<FakeUnacceptedMask>(new Implementation<FakeUnacceptedMask>(c, s, k))
{
}

FakeUnacceptedMask::~FakeUnacceptedMask()
{
}

char
FakeUnacceptedMask::key() const
{
    return _imp->key;
}

const std::string
FakeUnacceptedMask::description() const
{
    return _imp->description;
}

const tr1::shared_ptr<const MetadataKey>
FakeUnacceptedMask::unaccepted_key() const
{
    return _imp->unaccepted_key;
}

namespace paludis
{
    using namespace tr1::placeholders;

    template <>
    struct Implementation<FakePackageID>
    {
        mutable Mutex mutex;

        const Environment * const env;
        const tr1::shared_ptr<const FakeRepositoryBase> repository;
        const QualifiedPackageName name;
        const VersionSpec version;
        SlotName slot;

        tr1::shared_ptr<LiteralMetadataPackageIDKey> package_id;
        tr1::shared_ptr<LiteralMetadataPackageIDKey> virtual_for;
        tr1::shared_ptr<FakeMetadataKeywordSetKey> keywords;
        tr1::shared_ptr<FakeMetadataIUseSetKey> iuse;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<LicenseSpecTree> > license;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<ProvideSpecTree> > provide;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> > build_dependencies;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> > run_dependencies;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> > post_dependencies;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> > suggested_dependencies;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<RestrictSpecTree> > restrictions;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<FetchableURISpecTree> > src_uri;
        tr1::shared_ptr<FakeMetadataSpecTreeKey<SimpleURISpecTree> > homepage;

        mutable bool has_masks;

        Implementation(const Environment * const e, const tr1::shared_ptr<const FakeRepositoryBase> & r,
                const QualifiedPackageName & q, const VersionSpec & v, const PackageID * const id) :
            env(e),
            repository(r),
            name(q),
            version(v),
            slot("0"),
            keywords(new FakeMetadataKeywordSetKey("KEYWORDS", "Keywords", "test", mkt_normal, id, env)),
            iuse(new FakeMetadataIUseSetKey("IUSE", "Used USE flags", "", iuse_pm_permissive, mkt_normal, id, env)),
            license(new FakeMetadataSpecTreeKey<LicenseSpecTree>("LICENSE", "Licenses",
                        "", tr1::bind(&erepository::parse_license, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_normal)),
            provide(new FakeMetadataSpecTreeKey<ProvideSpecTree>("PROVIDE", "Provided packages",
                        "", tr1::bind(&erepository::parse_provide, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_normal)),
            build_dependencies(new FakeMetadataSpecTreeKey<DependencySpecTree>("DEPEND", "Build dependencies",
                        "", tr1::bind(&erepository::parse_depend, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_dependencies)),
            run_dependencies(new FakeMetadataSpecTreeKey<DependencySpecTree>("RDEPEND", "Run dependencies",
                        "", tr1::bind(&erepository::parse_depend, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_dependencies)),
            post_dependencies(new FakeMetadataSpecTreeKey<DependencySpecTree>("PDEPEND", "Post dependencies",
                        "", tr1::bind(&erepository::parse_depend, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_dependencies)),
            suggested_dependencies(new FakeMetadataSpecTreeKey<DependencySpecTree>("SDEPEND", "Suggested dependencies",
                        "", tr1::bind(&erepository::parse_depend, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_dependencies)),
            src_uri(new FakeMetadataSpecTreeKey<FetchableURISpecTree>("SRC_URI", "Source URIs",
                        "", tr1::bind(&erepository::parse_fetchable_uri, _1,
                            *erepository::EAPIData::get_instance()->eapi_from_string("0")), mkt_dependencies)),
            has_masks(false)
        {
        }
    };
}

FakePackageID::FakePackageID(const Environment * const e, const tr1::shared_ptr<const FakeRepositoryBase> & r,
        const QualifiedPackageName & q, const VersionSpec & v) :
    PrivateImplementationPattern<FakePackageID>(new Implementation<FakePackageID>(e, r, q, v, this)),
    _imp(PrivateImplementationPattern<FakePackageID>::_imp)
{
    add_metadata_key(_imp->keywords);
    add_metadata_key(_imp->iuse);
    add_metadata_key(_imp->license);
    add_metadata_key(_imp->provide);
    add_metadata_key(_imp->build_dependencies);
    add_metadata_key(_imp->run_dependencies);
    add_metadata_key(_imp->post_dependencies);
    add_metadata_key(_imp->suggested_dependencies);
}

FakePackageID::~FakePackageID()
{
}

const std::string
FakePackageID::canonical_form(const PackageIDCanonicalForm f) const
{
    switch (f)
    {
        case idcf_full:
            return stringify(_imp->name) + "-" + stringify(_imp->version) + ":" + stringify(slot()) + "::" + stringify(_imp->repository->name());

        case idcf_version:
            return stringify(_imp->version);

        case idcf_no_version:
            return stringify(_imp->name) + ":" + stringify(slot()) + "::" + stringify(_imp->repository->name());

        case last_idcf:
            break;
    }

    throw InternalError(PALUDIS_HERE, "Bad PackageIDCanonicalForm");
}

const QualifiedPackageName
FakePackageID::name() const
{
    return _imp->name;
}

const VersionSpec
FakePackageID::version() const
{
    return _imp->version;
}

const SlotName
FakePackageID::slot() const
{
    return _imp->slot;
}

const tr1::shared_ptr<const Repository>
FakePackageID::repository() const
{
    return _imp->repository;
}

const tr1::shared_ptr<const MetadataPackageIDKey>
FakePackageID::virtual_for_key() const
{
    return _imp->virtual_for;
}

const tr1::shared_ptr<const MetadataCollectionKey<KeywordNameSet> >
FakePackageID::keywords_key() const
{
    return _imp->keywords;
}

const tr1::shared_ptr<const MetadataCollectionKey<IUseFlagSet> >
FakePackageID::iuse_key() const
{
    return _imp->iuse;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<LicenseSpecTree> >
FakePackageID::license_key() const
{
    return _imp->license;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<ProvideSpecTree> >
FakePackageID::provide_key() const
{
    return _imp->provide;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::build_dependencies_key() const
{
    return _imp->build_dependencies;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::run_dependencies_key() const
{
    return _imp->run_dependencies;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::post_dependencies_key() const
{
    return _imp->post_dependencies;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::suggested_dependencies_key() const
{
    return _imp->suggested_dependencies;
}

const tr1::shared_ptr<FakeMetadataKeywordSetKey>
FakePackageID::keywords_key()
{
    return _imp->keywords;
}

const tr1::shared_ptr<FakeMetadataIUseSetKey>
FakePackageID::iuse_key()
{
    return _imp->iuse;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<ProvideSpecTree> >
FakePackageID::provide_key()
{
    return _imp->provide;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::build_dependencies_key()
{
    return _imp->build_dependencies;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::run_dependencies_key()
{
    return _imp->run_dependencies;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::post_dependencies_key()
{
    return _imp->post_dependencies;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<DependencySpecTree> >
FakePackageID::suggested_dependencies_key()
{
    return _imp->suggested_dependencies;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<FetchableURISpecTree> >
FakePackageID::fetches_key() const
{
    return _imp->src_uri;
}

const tr1::shared_ptr<FakeMetadataSpecTreeKey<FetchableURISpecTree> >
FakePackageID::fetches_key()
{
    return _imp->src_uri;
}

const tr1::shared_ptr<const MetadataSpecTreeKey<SimpleURISpecTree> >
FakePackageID::homepage_key() const
{
    return _imp->homepage;
}

const tr1::shared_ptr<const MetadataStringKey>
FakePackageID::short_description_key() const
{
    return tr1::shared_ptr<const MetadataStringKey>();
}

const tr1::shared_ptr<const MetadataStringKey>
FakePackageID::long_description_key() const
{
    return tr1::shared_ptr<const MetadataStringKey>();
}

const tr1::shared_ptr<const MetadataContentsKey>
FakePackageID::contents_key() const
{
    return tr1::shared_ptr<const MetadataContentsKey>();
}

const tr1::shared_ptr<const MetadataTimeKey>
FakePackageID::installed_time_key() const
{
    return tr1::shared_ptr<const MetadataTimeKey>();
}

const tr1::shared_ptr<const MetadataStringKey>
FakePackageID::source_origin_key() const
{
    return tr1::shared_ptr<const MetadataStringKey>();
}

const tr1::shared_ptr<const MetadataStringKey>
FakePackageID::binary_origin_key() const
{
    return tr1::shared_ptr<const MetadataStringKey>();
}

void
FakePackageID::set_slot(const SlotName & s)
{
    _imp->slot = s;
}

bool
FakePackageID::arbitrary_less_than_comparison(const PackageID & other) const
{
    return slot().data() < other.slot().data();
}

void
FakePackageID::need_keys_added() const
{
}

std::size_t
FakePackageID::extra_hash_value() const
{
    return CRCHash<SlotName>()(slot());
}

bool
FakePackageID::supports_action(const SupportsActionTestBase & b) const
{
    return repository()->some_ids_might_support_action(b);
}

namespace
{
    struct LicenceChecker :
        ConstVisitor<LicenseSpecTree>,
        ConstVisitor<LicenseSpecTree>::VisitConstSequence<LicenceChecker, AllDepSpec>
    {
        using ConstVisitor<LicenseSpecTree>::VisitConstSequence<LicenceChecker, AllDepSpec>::visit_sequence;

        bool ok;
        const Environment * const env;
        bool  (Environment::* const func) (const std::string &, const PackageID &) const;
        const PackageID * const id;

        LicenceChecker(const Environment * const e,
                bool (Environment::* const f) (const std::string &, const PackageID &) const,
                const PackageID * const d) :
            ok(true),
            env(e),
            func(f),
            id(d)
        {
        }

        void visit_sequence(const AnyDepSpec &,
                LicenseSpecTree::ConstSequenceIterator begin,
                LicenseSpecTree::ConstSequenceIterator end)
        {
            bool local_ok(false);

            if (begin == end)
                local_ok = true;
            else
            {
                for ( ; begin != end ; ++begin)
                {
                    Save<bool> save_ok(&ok, true);
                    begin->accept(*this);
                    local_ok |= ok;
                }
            }

            ok &= local_ok;
        }

        void visit_sequence(const UseDepSpec & spec,
                LicenseSpecTree::ConstSequenceIterator begin,
                LicenseSpecTree::ConstSequenceIterator end)
        {
            if (env->query_use(spec.flag(), *id))
                std::for_each(begin, end, accept_visitor(*this));
        }

        void visit_leaf(const LicenseDepSpec & spec)
        {
            if (! (env->*func)(spec.text(), *id))
                ok = false;
        }
    };
}

void
FakePackageID::need_masks_added() const
{
    Lock l(_imp->mutex);

    if (_imp->has_masks)
        return;

    _imp->has_masks = true;

    Context context("When generating masks for ID '" + canonical_form(idcf_full) + "':");

    if (keywords_key())
        if (! _imp->env->accept_keywords(keywords_key()->value(), *this))
            add_mask(make_shared_ptr(new FakeUnacceptedMask('K', "keywords", keywords_key())));

    if (license_key())
    {
        LicenceChecker c(_imp->env, &Environment::accept_license, this);
        license_key()->value()->accept(c);
        if (! c.ok)
            add_mask(make_shared_ptr(new FakeUnacceptedMask('L', "license", license_key())));
    }

    if (! _imp->env->unmasked_by_user(*this))
    {
        tr1::shared_ptr<const Mask> user_mask(_imp->env->mask_for_user(*this));
        if (user_mask)
            add_mask(user_mask);
    }

    tr1::shared_ptr<const Mask> breaks_mask(_imp->env->mask_for_breakage(*this));
    if (breaks_mask)
        add_mask(breaks_mask);
}

void
FakePackageID::invalidate_masks() const
{
    Lock l(_imp->mutex);

    if (! _imp->has_masks)
        return;

    _imp->has_masks = false;
    PackageID::invalidate_masks();
}

namespace
{
    struct PerformAction :
        ConstVisitor<ActionVisitorTypes>
    {
        const PackageID * const id;

        PerformAction(const PackageID * const i) :
            id(i)
        {
        }

        void visit(const InstallAction & a)
        {
            SupportsActionTest<InstallAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const InstalledAction & a)
        {
            SupportsActionTest<InstalledAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const UninstallAction & a)
        {
            SupportsActionTest<UninstallAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const FetchAction & a)
        {
            SupportsActionTest<FetchAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const ConfigAction & a)
        {
            SupportsActionTest<ConfigAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const InfoAction & a)
        {
            SupportsActionTest<InfoAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }

        void visit(const PretendAction & a)
        {
            SupportsActionTest<PretendAction> t;
            if (! id->repository()->some_ids_might_support_action(t))
                throw UnsupportedActionError(*id, a);
        }
    };
}

void
FakePackageID::perform_action(Action & a) const
{
    PerformAction b(this);
    a.accept(b);
}

bool
FakePackageID::breaks_portage() const
{
    return (version().has_try_part() || version().has_scm_part());
}

const tr1::shared_ptr<const MetadataCollectionKey<PackageIDSequence> >
FakePackageID::contains_key() const
{
    return tr1::shared_ptr<const MetadataCollectionKey<PackageIDSequence> >();
}

const tr1::shared_ptr<const MetadataPackageIDKey>
FakePackageID::contained_in_key() const
{
    return tr1::shared_ptr<const MetadataPackageIDKey>();
}

const tr1::shared_ptr<const MetadataFSEntryKey>
FakePackageID::fs_location_key() const
{
    return tr1::shared_ptr<const MetadataFSEntryKey>();
}

std::string
FakeMetadataIUseSetKey::pretty_print_flat(const Formatter<IUseFlag> & f) const
{
    std::string result;
    for (IUseFlagSet::ConstIterator i(value()->begin()), i_end(value()->end()) ;
            i != i_end ; ++i)
    {
        if (! result.empty())
            result.append(" ");

        if (_imp->id->repository()->use_interface && _imp->id->repository()->use_interface->query_use_mask(i->flag, *_imp->id))
            result.append(f.format(*i, format::Masked()));
        else if (_imp->id->repository()->use_interface && _imp->id->repository()->use_interface->query_use_force(i->flag, *_imp->id))
            result.append(f.format(*i, format::Forced()));
        else if (_imp->env->query_use(i->flag, *_imp->id))
            result.append(f.format(*i, format::Enabled()));
        else
            result.append(f.format(*i, format::Disabled()));
    }

    return result;
}

std::string
FakeMetadataIUseSetKey::pretty_print_flat_with_comparison(
        const Environment * const env,
        const tr1::shared_ptr<const PackageID> & id,
        const Formatter<IUseFlag> & f) const
{
    std::string result;
    for (IUseFlagSet::ConstIterator i(value()->begin()), i_end(value()->end()) ;
            i != i_end ; ++i)
    {
        if (! result.empty())
            result.append(" ");

        std::string l;
        bool n;

        if (_imp->id->repository()->use_interface && _imp->id->repository()->use_interface->query_use_mask(i->flag, *_imp->id))
        {
            l = f.format(*i, format::Masked());
            n = false;
        }
        else if (_imp->id->repository()->use_interface && _imp->id->repository()->use_interface->query_use_force(i->flag, *_imp->id))
        {
            l = f.format(*i, format::Forced());
            n = true;
        }
        else if (_imp->env->query_use(i->flag, *_imp->id))
        {
            l = f.format(*i, format::Enabled());
            n = true;
        }
        else
        {
            l = f.format(*i, format::Disabled());
            n = true;
        }

        if (! id->iuse_key())
            l = f.decorate(*i, l, format::Added());
        else
        {
            using namespace tr1::placeholders;
            IUseFlagSet::ConstIterator p(std::find_if(id->iuse_key()->value()->begin(), id->iuse_key()->value()->end(),
                        tr1::bind(std::equal_to<UseFlagName>(), i->flag, tr1::bind<const UseFlagName>(&IUseFlag::flag, _1))));

            if (p == id->iuse_key()->value()->end())
                l = f.decorate(*i, l, format::Added());
            else if (n != env->query_use(i->flag, *id))
                l = f.decorate(*i, l, format::Changed());
        }

        result.append(l);
    }

    return result;
}


std::string
FakeMetadataKeywordSetKey::pretty_print_flat(const Formatter<KeywordName> & f) const
{
    std::string result;
    for (KeywordNameSet::ConstIterator i(value()->begin()), i_end(value()->end()) ;
            i != i_end ; ++i)
    {
        if (! result.empty())
            result.append(" ");

        tr1::shared_ptr<KeywordNameSet> k(new KeywordNameSet);
        k->insert(*i);
        if (_imp->env->accept_keywords(k, *_imp->id))
            result.append(f.format(*i, format::Accepted()));
        else
            result.append(f.format(*i, format::Unaccepted()));
    }

    return result;
}

template class FakeMetadataSpecTreeKey<LicenseSpecTree>;
template class FakeMetadataSpecTreeKey<ProvideSpecTree>;
template class FakeMetadataSpecTreeKey<DependencySpecTree>;
template class FakeMetadataSpecTreeKey<RestrictSpecTree>;
#ifndef PALUDIS_NO_EXPLICIT_FULLY_SPECIALISED
template class FakeMetadataSpecTreeKey<FetchableURISpecTree>;
#endif
template class FakeMetadataSpecTreeKey<SimpleURISpecTree>;

template class FakeMetadataCollectionKey<KeywordNameSet>;
template class FakeMetadataCollectionKey<IUseFlagSet>;