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

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AttributeSource.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 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 {
16 protected:
17 AttributeFactory();
18
19 public:
20 virtual ~AttributeFactory();
21
22 LUCENE_CLASS(AttributeFactory);
23
24 public:
26 virtual AttributePtr createAttributeInstance(const String& className);
27
28 template <class ATTR>
29 AttributePtr createInstance(const String& className)
30 {
31 AttributePtr attrImpl = createAttributeInstance(className);
32 return attrImpl ? attrImpl : newLucene<ATTR>();
33 }
34
37 static AttributeFactoryPtr DEFAULT_ATTRIBUTE_FACTORY();
38 };
39
45 class LPPAPI AttributeSource : public LuceneObject
46 {
47 public:
49 AttributeSource();
50
52 AttributeSource(AttributeSourcePtr input);
53
56 AttributeSource(AttributeFactoryPtr factory);
57
58 virtual ~AttributeSource();
59
60 LUCENE_CLASS(AttributeSource);
61
62 protected:
63 AttributeFactoryPtr factory;
64 MapStringAttribute attributes;
65 AttributeSourceStatePtr currentState;
66
67 public:
69 AttributeFactoryPtr getAttributeFactory();
70
73 template <class ATTR>
74 boost::shared_ptr<ATTR> addAttribute()
75 {
76 String className(ATTR::_getClassName());
77 boost::shared_ptr<ATTR> attrImpl(boost::dynamic_pointer_cast<ATTR>(getAttribute(className)));
78 if (!attrImpl)
79 {
80 attrImpl = boost::dynamic_pointer_cast<ATTR>(factory->createInstance<ATTR>(className));
81 if (!attrImpl)
82 boost::throw_exception(IllegalArgumentException(L"Could not instantiate implementing class for " + className));
83 addAttribute(className, attrImpl);
84 }
85 return attrImpl;
86 }
87
89 void addAttribute(const String& className, AttributePtr attrImpl);
90
92 bool hasAttributes();
93
95 template <class ATTR>
96 bool hasAttribute()
97 {
98 return getAttribute(ATTR::_getClassName());
99 }
100
102 template <class ATTR>
103 boost::shared_ptr<ATTR> getAttribute()
104 {
105 String className(ATTR::_getClassName());
106 boost::shared_ptr<ATTR> attr(boost::dynamic_pointer_cast<ATTR>(getAttribute(className)));
107 if (!attr)
108 boost::throw_exception(IllegalArgumentException(L"This AttributeSource does not have the attribute '" + className + L"'."));
109 return attr;
110 }
111
114 void clearAttributes();
115
118 AttributeSourceStatePtr captureState();
119
129 void restoreState(AttributeSourceStatePtr state);
130
132 virtual int32_t hashCode();
133
135 virtual bool equals(LuceneObjectPtr other);
136
138 virtual String toString();
139
143 AttributeSourcePtr cloneAttributes();
144
146 Collection<AttributePtr> getAttributes();
147
148 protected:
151 AttributePtr getAttribute(const String& className);
152
154 bool hasAttribute(const String& className);
155
156 void computeCurrentState();
157 };
158
159 class LPPAPI DefaultAttributeFactory : public AttributeFactory
160 {
161 public:
162 virtual ~DefaultAttributeFactory();
163
164 LUCENE_CLASS(DefaultAttributeFactory);
165
166 public:
168 virtual AttributePtr createAttributeInstance(const String& className);
169 };
170
174 class LPPAPI AttributeSourceState : public LuceneObject
175 {
176 public:
177 virtual ~AttributeSourceState();
178
179 LUCENE_CLASS(AttributeSourceState);
180
181 protected:
182 AttributePtr attribute;
183 AttributeSourceStatePtr next;
184
185 public:
186 virtual LuceneObjectPtr clone(LuceneObjectPtr other = LuceneObjectPtr());
187
188 friend class AttributeSource;
189 };
190 }
191
192 #endif

clucene.sourceforge.net