OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ElementFactory.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: ElementFactory.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: ElementFactory
10 // This class is the main CLASSIC element factory.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: Construction
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:32:35 $
17 // $Author: fci $
18 //
19 // ------------------------------------------------------------------------
20 
24 
25 
26 // Class ElementFactory
27 // ------------------------------------------------------------------------
28 
30  inventory()
31 {}
32 
33 
35 {}
36 
37 
39  std::string name = newElement->getName();
40  MapType::value_type value(name, newElement);
41  std::pair<MapType::iterator, bool> index = inventory.insert(value);
42 
43  if(index.second) {
44  // Insertion took place (no duplicate).
45  return true;
46  } else {
47  // Insertion rejected.
48  delete newElement;
49  return false;
50  }
51 }
52 
53 
54 void ElementFactory::erase(const std::string &name) {
55  inventory.erase(name);
56 }
57 
58 
59 ElementBase *ElementFactory::find(const std::string &name) const {
60  MapType::const_iterator index = inventory.find(name);
61 
62  if(index == inventory.end()) {
63  return 0;
64  } else {
65  return (*index).second;
66  }
67 }
68 
69 
71  const std::string &name,
72  const AttributeSet &set) {
73  ElementBase *model = find(type);
74  ElementBase *copy = 0;
75 
76  try {
77  if(model != 0) {
78  ElementBase *copy = model->clone();
79  copy->setName(name);
80  copy->update(set);
81  storeElement(copy);
82  return copy;
83  }
84  } catch(...) {
85  delete copy;
86  }
87 
88  return 0;
89 }
90 
91 
93  std::string name = newElement->getName();
94  MapType::value_type value(name, newElement);
95  std::pair<MapType::iterator, bool> index = inventory.insert(value);
96 
97  if(index.second) {
98  // Insertion took place (no duplicate).
99  return true;
100  } else {
101  // Insertion was a replacement.
102  (*index.first).second = newElement;
103  return false;
104  }
105 }
virtual ElementBase * find(const std::string &name) const
Find element by name.
ElementFactory()
Default constructor.
Interface for basic beam line object.
Definition: ElementBase.h:128
Map of std::string versus double value.
Definition: AttributeSet.h:41
virtual ElementBase * makeElement(const std::string &type, const std::string &name, const AttributeSet &set)
Make new element.
virtual void setName(const std::string &name)
Set element name.
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
virtual bool define(ElementBase *newElement)
Define a new element.
virtual bool storeElement(ElementBase *newElement)
Define a new element.
virtual ElementBase * clone() const =0
Return clone.
T * value_type(const SliceIterator< T > &)
virtual ~ElementFactory()
const std::string name
virtual void erase(const std::string &name)
Erase element by name.
bool update(const AttributeSet &)
Update element.