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

/*
 * Copyright (c) 2009, 2010 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/output_manager_factory.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/instantiation_policy-impl.hh>
#include <paludis/util/member_iterator-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/join.hh>
#include <paludis/util/named_value.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/set-impl.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/system.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/about.hh>
#include <tr1/unordered_map>
#include <list>

#include <paludis/buffer_output_manager.hh>
#include <paludis/file_output_manager.hh>
#include <paludis/format_messages_output_manager.hh>
#include <paludis/forward_at_finish_output_manager.hh>
#include <paludis/standard_output_manager.hh>
#include <paludis/tee_output_manager.hh>

using namespace paludis;

namespace paludis
{
    namespace n
    {
        typedef Name<struct create_function_name> create_function;
    }
}

namespace
{
    struct Funcs
    {
        NamedValue<n::create_function, OutputManagerFactory::CreateFunction> create_function;
    };

    typedef std::tr1::unordered_map<std::string, Funcs> Keys;

    const Funcs & fetch(const Keys & keys, const std::string & key)
    {
        if (key.empty())
            throw ConfigurationError("Key 'handler' not specified when creating an output manager");

        Keys::const_iterator i(keys.find(key));
        if (i == keys.end())
            throw ConfigurationError("Format '" + key + "' not supported when creating an output manager (known formats are { "
                    + join(first_iterator(keys.begin()), first_iterator(keys.end()), ", ") + " })");

        return i->second;
    }
}

namespace paludis
{
    template <>
    struct Implementation<OutputManagerFactory>
    {
        Keys keys;
        std::list<void *> dl_opened;
    };

    template <>
    struct WrappedForwardIteratorTraits<OutputManagerFactory::ConstIteratorTag>
    {
        typedef FirstIteratorTypes<Keys::const_iterator>::Type UnderlyingIterator;
    };
}

OutputManagerFactory::OutputManagerFactory() :
    PrivateImplementationPattern<OutputManagerFactory>(new Implementation<OutputManagerFactory>)
{
    /* we might want to make this plugin loadable at some point */
    add_manager(BufferOutputManager::factory_managers(), BufferOutputManager::factory_create);
    add_manager(FileOutputManager::factory_managers(), FileOutputManager::factory_create);
    add_manager(FormatMessagesOutputManager::factory_managers(), FormatMessagesOutputManager::factory_create);
    add_manager(ForwardAtFinishOutputManager::factory_managers(), ForwardAtFinishOutputManager::factory_create);
    add_manager(StandardOutputManager::factory_managers(), StandardOutputManager::factory_create);
    add_manager(TeeOutputManager::factory_managers(), TeeOutputManager::factory_create);
}

OutputManagerFactory::~OutputManagerFactory()
{
}

const std::tr1::shared_ptr<OutputManager>
OutputManagerFactory::create(
        const KeyFunction & key_function,
        const CreateChildFunction & create_child_function,
        const ReplaceVarsFunc & replace_vars_func
        ) const
{
    Context context("When creating output manager:");
    return fetch(_imp->keys, key_function("handler")).create_function()(key_function, create_child_function, replace_vars_func);
}

OutputManagerFactory::ConstIterator
OutputManagerFactory::begin_keys() const
{
    return ConstIterator(first_iterator(_imp->keys.begin()));
}

OutputManagerFactory::ConstIterator
OutputManagerFactory::end_keys() const
{
    return ConstIterator(first_iterator(_imp->keys.end()));
}

void
OutputManagerFactory::add_manager(
        const std::tr1::shared_ptr<const Set<std::string> > & formats,
        const CreateFunction & create_function
        )
{
    for (Set<std::string>::ConstIterator f(formats->begin()), f_end(formats->end()) ;
            f != f_end ; ++f)
    {
        if (! _imp->keys.insert(std::make_pair(*f, make_named_values<Funcs>(
                            n::create_function() = create_function
                            ))).second)
            throw ConfigurationError("Handler for output manager format '" + stringify(*f) + "' already exists");
    }
}

template class PrivateImplementationPattern<OutputManagerFactory>;
template class InstantiationPolicy<OutputManagerFactory, instantiation_method::SingletonTag>;
template class WrappedForwardIterator<OutputManagerFactory::ConstIteratorTag, const std::string>;