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
|
/** @file registry.cc
* @brief Class for looking up user subclasses during unserialisation.
*/
/* Copyright (C) 2006,2007,2008,2009,2010 Olly Betts
* Copyright (C) 2006,2007,2009 Lemur Consulting Ltd
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include "xapian/registry.h"
#include "xapian/error.h"
#include "xapian/matchspy.h"
#include "xapian/postingsource.h"
#include "xapian/weight.h"
#include "registryinternal.h"
#include "debuglog.h"
#include <algorithm>
#include <map>
using namespace std;
template<class T>
static inline void
register_object(map<string, T*> & registry, const T & obj)
{
string name = obj.name();
if (rare(name.empty())) {
throw Xapian::InvalidOperationError("Unable to register object - name() method returned empty string");
}
pair<typename map<string, T *>::iterator, bool> r;
r = registry.insert(make_pair(name, static_cast<T*>(NULL)));
if (!r.second) {
// Existing element with this key, so replace the pointer with NULL
// and delete the existing pointer.
//
// If the delete throws, this will leave a NULL entry in the map, but
// that won't affect behaviour as we return NULL for "not found"
// anyway. The memory used will be leaked if the dtor throws, but
// throwing exceptions from the dtor is bad form, so that's not a big
// problem.
T * p = NULL;
swap(p, r.first->second);
delete p;
}
T * clone = obj.clone();
if (rare(!clone)) {
throw Xapian::InvalidOperationError("Unable to register object - clone() method returned NULL");
}
r.first->second = clone;
}
template<class T>
static inline const T *
lookup_object(map<string, T*> registry, const string & name)
{
typename map<string, T*>::const_iterator i = registry.find(name);
if (i == registry.end()) {
return NULL;
}
return i->second;
}
namespace Xapian {
Registry::Registry(const Registry & other)
: internal(other.internal)
{
LOGCALL_CTOR(API, "Registry", other);
}
Registry &
Registry::operator=(const Registry & other)
{
LOGCALL(API, Xapian::Registry &, "Xapian::Registry::operator=", other);
internal = other.internal;
RETURN(*this);
}
Registry::Registry()
: internal(new Registry::Internal())
{
LOGCALL_CTOR(API, "Registry", NO_ARGS);
}
Registry::~Registry()
{
LOGCALL_DTOR(API, "Registry");
// Note - we don't need to do anything special in this destructor, but it
// does need to be explicitly defined because the definition of the
// internals is not visible externally, which results in an error if the
// compiler tries to generate a default destructor.
}
void
Registry::register_weighting_scheme(const Xapian::Weight &wt)
{
LOGCALL_VOID(API, "Xapian::Registry::register_weighting_scheme", wt.name());
register_object(internal->wtschemes, wt);
}
const Xapian::Weight *
Registry::get_weighting_scheme(const string & name) const
{
LOGCALL(API, const Xapian::Weight *, "Xapian::Registry::get_weighting_scheme", name);
RETURN(lookup_object(internal->wtschemes, name));
}
void
Registry::register_posting_source(const Xapian::PostingSource &source)
{
LOGCALL_VOID(API, "Xapian::Registry::register_posting_source", source.name());
register_object(internal->postingsources, source);
}
const Xapian::PostingSource *
Registry::get_posting_source(const string & name) const
{
LOGCALL(API, const Xapian::PostingSource *, "Xapian::Registry::get_posting_source", name);
RETURN(lookup_object(internal->postingsources, name));
}
void
Registry::register_match_spy(const Xapian::MatchSpy &spy)
{
LOGCALL_VOID(API, "Xapian::Registry::register_match_spy", spy.name());
register_object(internal->matchspies, spy);
}
const Xapian::MatchSpy *
Registry::get_match_spy(const string & name) const
{
LOGCALL(API, const Xapian::MatchSpy *, "Xapian::Registry::get_match_spy", name);
RETURN(lookup_object(internal->matchspies, name));
}
Registry::Internal::Internal()
{
add_defaults();
}
Registry::Internal::~Internal()
{
clear_weighting_schemes();
clear_posting_sources();
clear_match_spies();
}
void
Registry::Internal::add_defaults()
{
Xapian::Weight * weighting_scheme;
weighting_scheme = new Xapian::BM25Weight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::BoolWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
weighting_scheme = new Xapian::TradWeight;
wtschemes[weighting_scheme->name()] = weighting_scheme;
Xapian::PostingSource * source;
source = new Xapian::ValueWeightPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::DecreasingValueWeightPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::ValueMapPostingSource(0);
postingsources[source->name()] = source;
source = new Xapian::FixedWeightPostingSource(0.0);
postingsources[source->name()] = source;
Xapian::MatchSpy * spy;
spy = new Xapian::ValueCountMatchSpy();
matchspies[spy->name()] = spy;
}
void
Registry::Internal::clear_weighting_schemes()
{
map<string, Xapian::Weight*>::const_iterator i;
for (i = wtschemes.begin(); i != wtschemes.end(); ++i) {
delete i->second;
}
}
void
Registry::Internal::clear_posting_sources()
{
map<string, Xapian::PostingSource *>::const_iterator i;
for (i = postingsources.begin(); i != postingsources.end(); ++i) {
delete i->second;
}
}
void
Registry::Internal::clear_match_spies()
{
map<string, Xapian::MatchSpy *>::const_iterator i;
for (i = matchspies.begin(); i != matchspies.end(); ++i) {
delete i->second;
}
}
}
|