-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAttributeProvider.h
89 lines (81 loc) · 2.72 KB
/
AttributeProvider.h
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
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef ATTRIBUTEPROVIDER_H
#define ATTRIBUTEPROVIDER_H
#include "GenericAttribute.h"
#include "StringIdentifier.h"
#include <memory>
namespace Util {
/**
* Mixin class to provide a GenericAttributeMap including access functions for classes inheriting it.
* The map itself is created when the first attribute is added;
* so that the minimal memory overhead is of the size of a single pointer.
* @ingroup generic_attr
*/
class AttributeProvider {
private:
std::unique_ptr<GenericAttributeMap> _attributes;
inline GenericAttributeMap * _createAndGetAttributes() {
if(!_attributes)
_attributes.reset(new GenericAttributeMap);
return _attributes.get();
}
public:
AttributeProvider() {
}
AttributeProvider(const AttributeProvider & other) {
copyAttributesFrom(other);
}
AttributeProvider(AttributeProvider &&) = default;
AttributeProvider& operator=(const AttributeProvider & other) {
if(&other != this)
copyAttributesFrom(other);
return *this;
}
AttributeProvider& operator=(AttributeProvider &&) = default;
~AttributeProvider() = default;
void copyAttributesFrom(const AttributeProvider & other) {
if (other.hasAttributes())
_attributes.reset(other.getAttributes()->clone());
}
GenericAttribute * getAttribute(const StringIdentifier & key) const {
return _attributes ? _attributes->getValue(key) : nullptr;
}
template<class AttrType>
AttrType * getAttribute(const StringIdentifier & key) const {
return _attributes ? dynamic_cast<AttrType*>(_attributes->getValue(key)) : nullptr;
}
GenericAttributeMap * getAttributes() const {
return _attributes.get();
}
bool hasAttributes() const {
return _attributes.get() != nullptr;
}
bool isAttributeSet(const StringIdentifier & key) const {
return _attributes && _attributes->contains(key);
}
void removeAttributes() {
_attributes.reset();
}
void setAttributes(GenericAttributeMap * newAttributes) {
_attributes.reset(newAttributes);
}
void setAttribute(const StringIdentifier & key, GenericAttribute * value) {
if(value)
_createAndGetAttributes()->setValue(key, value);
else
unsetAttribute(key);
}
bool unsetAttribute(const StringIdentifier & key) {
return _attributes && _attributes->unsetValue(key);
}
};
}
#endif // ATTRIBUTEPROVIDER_H