Lucene++ - a full-featured, c++ search engine
API Documentation

Loading...
Searching...
No Matches
AttributeSource.h
Go to the documentation of this file.
1
2// Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3// Distributable under the terms of either the Apache License (Version 2.0)
4// or the GNU Lesser General Public License.
6
7#ifndef ATTRIBUTESOURCE_H
8#define ATTRIBUTESOURCE_H
9
10#include "LuceneObject.h"
11
12 namespace Lucene {
13
14 class LPPAPI AttributeFactory : public LuceneObject {
15 protected:
16 AttributeFactory();
17
18 public:
19 virtual ~AttributeFactory();
20
21 LUCENE_CLASS(AttributeFactory);
22
23 public:
25 virtual AttributePtr createAttributeInstance(const String& className);
26
27 template <class ATTR>
28 AttributePtr createInstance(const String& className) {
29 AttributePtr attrImpl = createAttributeInstance(className);
30 return attrImpl ? attrImpl : newLucene<ATTR>();
31 }
32
35 static AttributeFactoryPtr DEFAULT_ATTRIBUTE_FACTORY();
36};
37
43 class LPPAPI AttributeSource : public LuceneObject {
44 public:
46 AttributeSource();
47
49 AttributeSource(const AttributeSourcePtr& input);
50
53 AttributeSource(const AttributeFactoryPtr& factory);
54
55 virtual ~AttributeSource();
56
57 LUCENE_CLASS(AttributeSource);
58
59 protected:
60 AttributeFactoryPtr factory;
61 MapStringAttribute attributes;
62 AttributeSourceStatePtr currentState;
63
64 public:
66 AttributeFactoryPtr getAttributeFactory();
67
70 template <class ATTR>
71 boost::shared_ptr<ATTR> addAttribute() {
72 String className(ATTR::_getClassName());
73 boost::shared_ptr<ATTR> attrImpl(boost::dynamic_pointer_cast<ATTR>(getAttribute(className)));
74 if (!attrImpl) {
75 attrImpl = boost::dynamic_pointer_cast<ATTR>(factory->createInstance<ATTR>(className));
76 if (!attrImpl) {
77 boost::throw_exception(IllegalArgumentException(L"Could not instantiate implementing class for " + className));
78 }
79 addAttribute(className, attrImpl);
80 }
81 return attrImpl;
82 }
83
85 void addAttribute(const String& className, const AttributePtr& attrImpl);
86
88 bool hasAttributes();
89
91 template <class ATTR>
92 bool hasAttribute() {
93 return getAttribute(ATTR::_getClassName()).get() != NULL;
94 }
95
97 template <class ATTR>
98 boost::shared_ptr<ATTR> getAttribute() {
99 String className(ATTR::_getClassName());
100 boost::shared_ptr<ATTR> attr(boost::dynamic_pointer_cast<ATTR>(getAttribute(className)));
101 if (!attr) {
102 boost::throw_exception(IllegalArgumentException(L"This AttributeSource does not have the attribute '" + className + L"'."));
103 }
104 return attr;
105 }
106
109 void clearAttributes();
110
113 AttributeSourceStatePtr captureState();
114
124 void restoreState(const AttributeSourceStatePtr& state);
125
127 virtual int32_t hashCode();
128
130 virtual bool equals(const LuceneObjectPtr& other);
131
133 virtual String toString();
134
138 AttributeSourcePtr cloneAttributes();
139
141 Collection<AttributePtr> getAttributes();
142
143 protected:
146 AttributePtr getAttribute(const String& className);
147
149 bool hasAttribute(const String& className);
150
151 void computeCurrentState();
152};
153
154 class LPPAPI DefaultAttributeFactory : public AttributeFactory {
155 public:
156 virtual ~DefaultAttributeFactory();
157
158 LUCENE_CLASS(DefaultAttributeFactory);
159
160 public:
162 virtual AttributePtr createAttributeInstance(const String& className);
163};
164
168 class LPPAPI AttributeSourceState : public LuceneObject {
169 public:
170 virtual ~AttributeSourceState();
171
172 LUCENE_CLASS(AttributeSourceState);
173
174 protected:
175 AttributePtr attribute;
176 AttributeSourceStatePtr next;
177
178 public:
179 virtual LuceneObjectPtr clone(const LuceneObjectPtr& other = LuceneObjectPtr());
180
181 friend class AttributeSource;
182};
183
184}
185
186#endif
LuceneObject.h
LUCENE_CLASS
#define LUCENE_CLASS(Name)
Definition LuceneObject.h:24
Lucene::AttributeFactory
Definition AttributeSource.h:14
Lucene::AttributeFactory::~AttributeFactory
virtual ~AttributeFactory()
Lucene::AttributeFactory::DEFAULT_ATTRIBUTE_FACTORY
static AttributeFactoryPtr DEFAULT_ATTRIBUTE_FACTORY()
This is the default factory that creates Attributes using the class name of the supplied Attribute in...
Lucene::AttributeFactory::createAttributeInstance
virtual AttributePtr createAttributeInstance(const String &className)
returns an Attribute.
Lucene::AttributeFactory::AttributeFactory
AttributeFactory()
Lucene::AttributeFactory::createInstance
AttributePtr createInstance(const String &className)
Definition AttributeSource.h:28
Lucene::AttributeSourceState
This class holds the state of an AttributeSource.
Definition AttributeSource.h:168
Lucene::AttributeSourceState::~AttributeSourceState
virtual ~AttributeSourceState()
Lucene::AttributeSourceState::clone
virtual LuceneObjectPtr clone(const LuceneObjectPtr &other=LuceneObjectPtr())
Return clone of this object.
Lucene::AttributeSourceState::attribute
AttributePtr attribute
Definition AttributeSource.h:175
Lucene::AttributeSourceState::next
AttributeSourceStatePtr next
Definition AttributeSource.h:176
Lucene::AttributeSource
An AttributeSource contains a list of different Attributes, and methods to add and get them....
Definition AttributeSource.h:43
Lucene::AttributeSource::captureState
AttributeSourceStatePtr captureState()
Captures the state of all Attributes. The return value can be passed to restoreState to restore the s...
Lucene::AttributeSource::equals
virtual bool equals(const LuceneObjectPtr &other)
Return whether two objects are equal.
Lucene::AttributeSource::hasAttributes
bool hasAttributes()
Returns true if this AttributeSource has any attributes.
Lucene::AttributeSource::currentState
AttributeSourceStatePtr currentState
Definition AttributeSource.h:62
Lucene::AttributeSource::hashCode
virtual int32_t hashCode()
Return hash code for this object.
Lucene::AttributeSource::AttributeSource
AttributeSource(const AttributeSourcePtr &input)
An AttributeSource that uses the same attributes as the supplied one.
Lucene::AttributeSource::clearAttributes
void clearAttributes()
Resets all Attributes in this AttributeSource by calling AttributeImpl#clear() on each Attribute impl...
Lucene::AttributeSource::addAttribute
void addAttribute(const String &className, const AttributePtr &attrImpl)
Adds a custom Attribute instance.
Lucene::AttributeSource::getAttributes
Collection< AttributePtr > getAttributes()
Return a vector of attributes based on currentState.
Lucene::AttributeSource::getAttribute
AttributePtr getAttribute(const String &className)
The caller must pass in a className value. This method checks if an instance of that class is already...
Lucene::AttributeSource::addAttribute
boost::shared_ptr< ATTR > addAttribute()
This method first checks if an instance of that class is already in this AttributeSource and returns ...
Definition AttributeSource.h:71
Lucene::AttributeSource::restoreState
void restoreState(const AttributeSourceStatePtr &state)
Restores this state by copying the values of all attribute implementations that this state contains i...
Lucene::AttributeSource::toString
virtual String toString()
Returns a string representation of the object.
Lucene::AttributeSource::computeCurrentState
void computeCurrentState()
Lucene::AttributeSource::hasAttribute
bool hasAttribute(const String &className)
Returns true, if this AttributeSource contains the passed-in Attribute.
Lucene::AttributeSource::AttributeSource
AttributeSource(const AttributeFactoryPtr &factory)
An AttributeSource using the supplied AttributeFactory for creating new Attribute instances.
Lucene::AttributeSource::AttributeSource
AttributeSource()
An AttributeSource using the default attribute factory DefaultAttributeFactory.
Lucene::AttributeSource::hasAttribute
bool hasAttribute()
Returns true, if this AttributeSource contains the passed-in Attribute.
Definition AttributeSource.h:92
Lucene::AttributeSource::factory
AttributeFactoryPtr factory
Definition AttributeSource.h:60
Lucene::AttributeSource::attributes
MapStringAttribute attributes
Definition AttributeSource.h:61
Lucene::AttributeSource::getAttribute
boost::shared_ptr< ATTR > getAttribute()
Returns the instance of the passed in Attribute contained in this AttributeSource.
Definition AttributeSource.h:98
Lucene::AttributeSource::getAttributeFactory
AttributeFactoryPtr getAttributeFactory()
returns the used AttributeFactory.
Lucene::AttributeSource::~AttributeSource
virtual ~AttributeSource()
Lucene::AttributeSource::cloneAttributes
AttributeSourcePtr cloneAttributes()
Performs a clone of all AttributeImpl instances returned in a new AttributeSource instance....
Lucene::Collection
Utility template class to handle collections that can be safely copied and shared.
Definition Collection.h:17
Lucene::DefaultAttributeFactory
Definition AttributeSource.h:154
Lucene::DefaultAttributeFactory::~DefaultAttributeFactory
virtual ~DefaultAttributeFactory()
Lucene::DefaultAttributeFactory::createAttributeInstance
virtual AttributePtr createAttributeInstance(const String &className)
returns an Attribute.
Lucene::ExceptionTemplate
Definition LuceneException.h:67
Lucene::LuceneObject
Base class for all Lucene classes.
Definition LuceneObject.h:31
Lucene
Definition AbstractAllTermDocs.h:12
Lucene::LuceneObjectPtr
boost::shared_ptr< LuceneObject > LuceneObjectPtr
Definition LuceneTypes.h:539
Lucene::AttributeSourcePtr
boost::shared_ptr< AttributeSource > AttributeSourcePtr
Definition LuceneTypes.h:520
Lucene::AttributeSourceStatePtr
boost::shared_ptr< AttributeSourceState > AttributeSourceStatePtr
Definition LuceneTypes.h:521
Lucene::AttributeFactoryPtr
boost::shared_ptr< AttributeFactory > AttributeFactoryPtr
Definition LuceneTypes.h:519
Lucene::AttributePtr
boost::shared_ptr< Attribute > AttributePtr
Definition LuceneTypes.h:518

clucene.sourceforge.net