OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
AttributeSet.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: AttributeSet.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.2 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: AttributeSet
10// A map of name (std::string) versus value (double) intended to store
11// user-defined attributes.
12//
13// ------------------------------------------------------------------------
14// Class category: AbsBeamline
15// ------------------------------------------------------------------------
16//
17// $Date: 2000/12/16 16:26:43 $
18// $Author: mad $
19//
20// ------------------------------------------------------------------------
21
23#include "Channels/Channel.h"
25
26
27// Class AttributeSet
28// ------------------------------------------------------------------------
29
31 itsMap()
32{}
33
34
36 itsMap(rhs.itsMap)
37{}
38
39
41{}
42
43
45 itsMap = rhs.itsMap;
46 return *this;
47}
48
49
50double AttributeSet::getAttribute(const std::string &aKey) const {
51 const_iterator index = itsMap.find(aKey);
52
53 if(index == itsMap.end()) {
54 return 0.0;
55 } else {
56 return index->second;
57 }
58}
59
60
61bool AttributeSet::hasAttribute(const std::string &aKey) const {
62 return (itsMap.find(aKey) != itsMap.end());
63}
64
65
66void AttributeSet::removeAttribute(const std::string &aKey) {
67 itsMap.erase(aKey);
68}
69
70
71void AttributeSet::setAttribute(const std::string &aKey, double value) {
72 itsMap[aKey] = value;
73}
74
75
76// This method is inlined so its const version can wrap it.
77// ada 3-7-2000 remove inline because KCC does not like it.
78
79Channel *AttributeSet::getChannel(const std::string &aKey, bool create) {
80 NameMap::iterator index = itsMap.find(aKey);
81
82 if(index == itsMap.end()) {
83 if(create) {
84 itsMap[aKey] = 0.0;
85 return new DirectChannel(itsMap[aKey]);
86 }
87 // for (NameMap::iterator index = itsMap.begin(); index != itsMap.end(); index++)
88 return nullptr;
89 } else {
90 return new DirectChannel((*index).second);
91 }
92}
93
94
95const ConstChannel *AttributeSet::getConstChannel(const std::string &aKey) const {
96 // Use const_cast to allow calling the non-const GetChannel().
97 // The const return value will nevertheless inhibit set().
98 return const_cast<AttributeSet *>(this)->getChannel(aKey);
99}
std::string::iterator iterator
Definition: MSLang.h:16
Map of std::string versus double value.
Definition: AttributeSet.h:41
virtual ~AttributeSet()
bool hasAttribute(const std::string &aKey) const
Test for presence of an attribute.
AttributeSet()
Default constructor.
const AttributeSet & operator=(const AttributeSet &)
void removeAttribute(const std::string &aKey)
Remove an existing attribute.
NameMap::const_iterator const_iterator
An iterator for a map of name versus value.
Definition: AttributeSet.h:49
Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
double getAttribute(const std::string &aKey) const
Get attribute value.
const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.
NameMap itsMap
The attribute map.
Definition: AttributeSet.h:98
Abstract interface for read/write access to variable.
Definition: Channel.h:32
Abstract interface for read-only access to variable.
Definition: ConstChannel.h:29
Direct access to a [b]double[/b] variable.
Definition: DirectChannel.h:31