aboutsummaryrefslogtreecommitdiff
path: root/paludis/resolver/reason.cc
blob: 382e6e1db1a343e9021241d579aed41cc5d31df9 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009 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/resolver/reason.hh>
#include <paludis/resolver/qpn_s.hh>
#include <paludis/resolver/sanitised_dependencies.hh>
#include <paludis/resolver/serialise-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>

using namespace paludis;
using namespace paludis::resolver;

std::ostream &
paludis::resolver::operator<< (std::ostream & s, const Reason & r)
{
    s << r.as_string();
    return s;
}

Reason::~Reason()
{
}

std::string
TargetReason::as_string() const
{
    return "Target()";
}

void
TargetReason::serialise(Serialiser & s) const
{
    s.object("TargetReason")
        ;
}

namespace paludis
{
    template <>
    struct Implementation<DependencyReason>
    {
        const std::tr1::shared_ptr<const PackageID> from_id;
        const SanitisedDependency dep;

        Implementation(const std::tr1::shared_ptr<const PackageID> & i, const SanitisedDependency & d) :
            from_id(i),
            dep(d)
        {
        }
    };
}

DependencyReason::DependencyReason(const std::tr1::shared_ptr<const PackageID> & i,
        const SanitisedDependency & d) :
    PrivateImplementationPattern<DependencyReason>(new Implementation<DependencyReason>(i, d))
{
}

DependencyReason::~DependencyReason()
{
}

const std::tr1::shared_ptr<const PackageID>
DependencyReason::from_id() const
{
    return _imp->from_id;
}

const SanitisedDependency &
DependencyReason::sanitised_dependency() const
{
    return _imp->dep;
}

std::string
DependencyReason::as_string() const
{
    return "Dependency(package: " + stringify(*_imp->from_id) + " dep: " + stringify(_imp->dep) + ")";
}

void
DependencyReason::serialise(Serialiser & s) const
{
    s.object("DependencyReason")
        .member(SerialiserFlags<serialise::might_be_null>(), "from_id", from_id())
        .member(SerialiserFlags<>(), "sanitised_dependency", sanitised_dependency())
        ;
}

std::string
PresetReason::as_string() const
{
    return "Preset()";
}

void
PresetReason::serialise(Serialiser & s) const
{
    s.object("PresetReason")
        ;
}

namespace paludis
{
    template <>
    struct Implementation<SetReason>
    {
        const SetName set_name;
        const std::tr1::shared_ptr<const Reason> reason_for_set;

        Implementation(const SetName & s, const std::tr1::shared_ptr<const Reason> & r) :
            set_name(s),
            reason_for_set(r)
        {
        }
    };
}

SetReason::SetReason(const SetName & s, const std::tr1::shared_ptr<const Reason> & r) :
    PrivateImplementationPattern<SetReason>(new Implementation<SetReason>(s, r))
{
}

SetReason::~SetReason()
{
}

const SetName
SetReason::set_name() const
{
    return _imp->set_name;
}

const std::tr1::shared_ptr<const Reason>
SetReason::reason_for_set() const
{
    return _imp->reason_for_set;
}

std::string
SetReason::as_string() const
{
    return "Set(set: " + stringify(_imp->set_name) + " because: " + stringify(*_imp->reason_for_set) + ")";
}

void
SetReason::serialise(Serialiser & s) const
{
    s.object("SetReason")
        .member(SerialiserFlags<serialise::might_be_null>(), "reason_for_set", reason_for_set())
        .member(SerialiserFlags<>(), "set_name", stringify(set_name()))
        ;
}

const std::tr1::shared_ptr<Reason>
Reason::deserialise(Deserialisation & d)
{
    if (d.class_name() == "TargetReason")
    {
        Deserialisator v(d, "TargetReason");
        return make_shared_ptr(new TargetReason);
    }
    else if (d.class_name() == "PresetReason")
    {
        Deserialisator v(d, "PresetReason");
        return make_shared_ptr(new PresetReason);
    }
    else if (d.class_name() == "SetReason")
    {
        Deserialisator v(d, "SetReason");
        return make_shared_ptr(new SetReason(
                    SetName(v.member<std::string>("set_name")),
                    v.member<std::tr1::shared_ptr<Reason> >("reason_for_set")
                    ));
    }
    else if (d.class_name() == "DependencyReason")
    {
        Deserialisator v(d, "DependencyReason");
        const std::tr1::shared_ptr<const PackageID> from_id(v.member<std::tr1::shared_ptr<const PackageID> >("from_id"));
        return make_shared_ptr(new DependencyReason(
                    from_id,
                    SanitisedDependency::deserialise(*v.find_remove_member("sanitised_dependency"), from_id))
                );
    }
    else
        throw InternalError(PALUDIS_HERE, "unknown class '" + stringify(d.class_name()) + "'");
}

template class PrivateImplementationPattern<DependencyReason>;
template class PrivateImplementationPattern<SetReason>;