OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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"
24 #include "Channels/DirectChannel.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 
50 double 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 
61 bool AttributeSet::hasAttribute(const std::string &aKey) const {
62  return (itsMap.find(aKey) != itsMap.end());
63 }
64 
65 
66 void AttributeSet::removeAttribute(const std::string &aKey) {
67  itsMap.erase(aKey);
68 }
69 
70 
71 void 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 
79 Channel *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 NULL;
89  } else {
90  return new DirectChannel((*index).second);
91  }
92 }
93 
94 
95 const 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 }
Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
Map of std::string versus double value.
Definition: AttributeSet.h:41
NameMap::const_iterator const_iterator
An iterator for a map of name versus value.
Definition: AttributeSet.h:49
Direct access to a [b]double[/b] variable.
Definition: DirectChannel.h:31
bool hasAttribute(const std::string &aKey) const
Test for presence of an attribute.
Abstract interface for read-only access to variable.
Definition: ConstChannel.h:29
virtual ~AttributeSet()
void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
NameMap itsMap
The attribute map.
Definition: AttributeSet.h:98
Abstract interface for read/write access to variable.
Definition: Channel.h:32
const AttributeSet & operator=(const AttributeSet &)
AttributeSet()
Default constructor.
void removeAttribute(const std::string &aKey)
Remove an existing attribute.
std::string::iterator iterator
Definition: MSLang.h:16
double getAttribute(const std::string &aKey) const
Get attribute value.
const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.