OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
EditInstall.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: EditInstall.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: EditInstall
10 // The class for the OPAL sequence editor INSTALL command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:38 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Editor/EditInstall.h"
24 #include "Attributes/Attributes.h"
25 #include "Editor/Edit.h"
26 #include "Parser/Statement.h"
28 #include "Utilities/ParseError.h"
29 #include "Utilities/Options.h"
30 #include <iostream>
31 
32 
33 // Class EditInstall
34 // ------------------------------------------------------------------------
35 
36 // The attributes of class EditInstall.
37 namespace {
38  enum {
39  AT, // The position.
40  FROM, // The place defining one or more positions.
41  SIZE
42  };
43 }
44 
45 
47  Editor(SIZE, "INSTALL",
48  "The \"INSTALL\" sub-command installs new elements in the sequence.") {
50  ("AT",
51  "Position in metres for installation relative to origin(s)");
53  ("FROM", "Position of element defining origin (default is start)");
54 
56 }
57 
58 
59 EditInstall::EditInstall(const std::string &name, EditInstall *parent):
60  Editor(name, parent)
61 {}
62 
63 
65 {}
66 
67 
68 EditInstall *EditInstall::clone(const std::string &name) {
69  return new EditInstall(name, this);
70 }
71 
72 
74  if(! itsAttr[AT]) {
75  throw OpalException("EditInstall::execute()",
76  "The \"AT\" attribute must be present.");
77  }
78 
79  double at = Attributes::getReal(itsAttr[AT]);
81  const PlaceRep from = Attributes::getPlace(itsAttr[FROM]);
82  int count;
83 
84  // Find where to install.
85  if(from.isSelected()) {
86  count = Edit::block->installMultiple(elm, at);
87  } else {
88  count = Edit::block->installSingle(from, elm, at);
89  }
90 
91  if(Options::info) {
92  const std::string &name = elm->getName();
93 
94  if(count == 0) {
95  std::cerr << "\nNo \"" << name << '"';
96  } else if(count == 1) {
97  std::cerr << "\n1 \"" << name << '"';
98  } else {
99  std::cerr << '\n' << count << " \"" << name << "\"'s";
100  }
101 
102  std::cerr << " installed.\n" << std::endl;
103  }
104 }
105 
106 
108  // Read object identifier.
109  std::string className;
110  std::string objectName = Expressions::parseString(stat, "Object name expected.");
111 
112  // Read class identifier.
113  if(stat.delimiter(':')) {
114  className = Expressions::parseString(stat, "Class name expected.");
115  } else {
116  className = objectName;
117  objectName = "";
118  }
119 
120  // Find exemplar object.
121  if(Object *object = OpalData::getInstance()->find(className)) {
122  // Instantiate, if necessary.
123  int defined = 0;
124  Pointer<Object> copy;
125 
126  if(stat.delimiter('(')) {
127  if(objectName.empty()) {
128  throw ParseError("EditInstall::parse()",
129  "An instantiation of \"" + className +
130  "\" in a sequence shoud have name.");
131  }
132  copy = object->makeInstance(objectName, stat, 0);
133  defined = 1;
134  } else if(! objectName.empty()) {
135  copy = object->clone(objectName);
136  defined = 2;
137  } else {
138  copy = object;
139  }
140 
141  // Check that there is no invalid redefinition.
142  if(defined) {
143  if(! objectName.empty() && OpalData::getInstance()->find(objectName)) {
144  throw ParseError("EditInstall::parse()",
145  "You cannot redefine \"" + objectName +
146  "\" within the sequence editor.");
147  }
148  }
149 
150  if(Element *element = dynamic_cast<Element *>(&*copy)) {
151  newElement = element;
152 
153  while(stat.delimiter(',')) {
154  std::string attrName =
155  Expressions::parseString(stat, "Attribute name expected.");
156  Attribute *attr = 0;
157 
158  if((attr = findAttribute(attrName)) != 0) {
159  // An attribute of the "INSTALL" command.
160  Expressions::parseDelimiter(stat, '=');
161  attr->parse(stat, true);
162  } else if(defined) {
163  // An attribute of the new element.
164  if((attr = newElement->findAttribute(attrName)) != 0) {
165  if(stat.delimiter('=')) {
166  attr->parse(stat, true);
167  } else if(stat.delimiter("=")) {
168  attr->parse(stat, false);
169  } else {
170  throw ParseError("EditInstall::parsePosition()",
171  "Delimiter \"=\" or \":=\" expected.");
172  }
173  } else {
174  throw ParseError("EditInstall::parsePosition()",
175  "Element \"" + newElement->getOpalName() +
176  "\" has no attribute \"" + attrName + "\".");
177  }
178  } else {
179  throw ParseError("EditInstall::parsePosition()",
180  "Overriding attributes not permitted here.");
181  }
182  }
183 
184  if(defined) OpalData::getInstance()->define(&*copy);
185  } else {
186  throw ParseError("EditInstall::parse()", "Object \"" +
187  objectName + "\" is not an element.");
188  }
189  } else {
190  throw ParseError("EditInstall::parse()",
191  "Element class name \"" + className + "\" is unknown.");
192  }
193 }
int installMultiple(ElementBase *, double)
Install multiple elements.
Definition: Edit.cpp:259
Interface for basic beam line object.
Definition: ElementBase.h:128
Parse exception.
Definition: ParseError.h:32
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:538
EditInstall()
Exemplar constructor.
Definition: EditInstall.cpp:46
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
virtual void parse(Statement &statement)
Parse the command.
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool info
Info flag.
Definition: Options.cpp:8
The base class for all OPAL sequence editor commands.
Definition: Editor.h:31
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
PlaceRep getPlace(const Attribute &attr)
Get place value.
Definition: Attributes.cpp:143
Representation of a place within a beam line or sequence.
Definition: PlaceRep.h:41
virtual EditInstall * clone(const std::string &name)
Make clone.
Definition: EditInstall.cpp:68
static OpalData * getInstance()
Definition: OpalData.cpp:209
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
The base class for all OPAL elements.
Definition: Element.h:46
A representation of an Object attribute.
Definition: Attribute.h:55
Interface for statements.
Definition: Statement.h:38
Reference-counted pointer.
Definition: Pointer.h:38
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
int installSingle(const PlaceRep &, ElementBase *, double)
Install element relative to place.
Definition: Edit.cpp:264
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
Definition: Object.cpp:94
bool isSelected() const
Return select flag.
Definition: PlaceRep.cpp:103
static Edit * block
Pointer to the edit data.
Definition: Edit.h:134
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
ElementBase * getElement() const
Return the embedded CLASSIC element.
Definition: Element.h:128
virtual ~EditInstall()
Definition: EditInstall.cpp:64
Attribute makePlace(const std::string &name, const std::string &help)
Create a place attribute.
Definition: Attributes.cpp:137
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
virtual Object * clone(const std::string &name)=0
Return a clone.
const std::string name
virtual void execute()
Execute the command.
Definition: EditInstall.cpp:73
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
std::string parseString(Statement &, const char msg[])
Parse string value.
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
The sequence editor INSTALL command.
Definition: EditInstall.h:30
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
void parse(Statement &stat, bool eval)
Parse attribute.
Definition: Attribute.cpp:125
Pointer< Element > newElement
Definition: EditInstall.h:59
Inform & endl(Inform &inf)
Definition: Inform.cpp:42