OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
EditReplace.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: EditReplace.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: EditReplace
10 // The class for the OPAL sequence editor REPLACE command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:38 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Editor/EditReplace.h"
23 #include "Attributes/Attributes.h"
24 #include "Editor/Edit.h"
26 #include "Utilities/Options.h"
27 #include <iostream>
28 
29 
30 // Class EditReplace
31 // ------------------------------------------------------------------------
32 
33 // The attributes of class EditReplace.
34 namespace {
35  enum {
36  SELECTED, // If true, replace selected elements.
37  CLASS, // The class to be replaced.
38  BY, // The replacement name.
39  SIZE
40  };
41 }
42 
43 
45  Editor(SIZE, "REPLACE",
46  "The \"REPLACE\" sub-command replaces element(s) in the sequence "
47  "being edited.") {
48  itsAttr[SELECTED] = Attributes::makeBool
49  ("SELECTED", "If true, all selected elements are replaced");
51  ("CLASS", "Name of element class to be replaced");
52  itsAttr[BY] = Attributes::makeString("BY", "name of replacement class");
53 
55 }
56 
57 
58 EditReplace::EditReplace(const std::string &name, EditReplace *parent):
59  Editor(name, parent)
60 {}
61 
62 
64 {}
65 
66 
67 EditReplace *EditReplace::clone(const std::string &name) {
68  return new EditReplace(name, this);
69 }
70 
71 
73  int count = 0;
74  std::string newName = Attributes::getString(itsAttr[BY]);
75 
76  // Check consistency.
77  if(! bool(itsAttr[CLASS]) || newName.empty()) {
78  throw OpalException("EditReplace::execute()",
79  "\"ELEMENT=SELECTED\" or \"ELEMENT=<name>\" and "
80  "\"BY=<name>\" are required for \"REPLACE\".");
81  }
82 
83  // Look up new name.
84  ElementBase *elem = Element::find(newName)->getElement();
85 
86  if(Attributes::getBool(itsAttr[SELECTED])) {
87  count = Edit::block->replaceMultiple(elem);
88  } else {
89  const PlaceRep pos = Attributes::getPlace(itsAttr[CLASS]);
90  count = Edit::block->replaceSingle(pos, elem);
91  }
92 
93  if(Options::info) {
94  if(count == 0) {
95  std::cerr << "\nNo elements";
96  } else if(count == 1) {
97  std::cerr << "\n1 element";
98  } else {
99  std::cerr << '\n' << count << " elements";
100  }
101 
102  std::cerr << " replaced.\n" << std::endl;
103  }
104 }
The sequence editor REPLACE command.
Definition: EditReplace.h:28
virtual EditReplace * clone(const std::string &name)
Make clone.
Definition: EditReplace.cpp:67
Interface for basic beam line object.
Definition: ElementBase.h:128
The base class for all OPAL exceptions.
Definition: OpalException.h:28
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
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
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 ~EditReplace()
Definition: EditReplace.cpp:63
int replaceSingle(const PlaceRep &, ElementBase *elem)
Replace single element.
Definition: Edit.cpp:309
EditReplace()
Exemplar constructor.
Definition: EditReplace.cpp:44
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
static Edit * block
Pointer to the edit data.
Definition: Edit.h:134
ElementBase * getElement() const
Return the embedded CLASSIC element.
Definition: Element.h:128
static Element * find(const std::string &name)
Find named Element.
Definition: Element.cpp:37
Attribute makePlace(const std::string &name, const std::string &help)
Create a place attribute.
Definition: Attributes.cpp:137
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
const std::string name
int replaceMultiple(ElementBase *elem)
Replace multiple elements.
Definition: Edit.cpp:304
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307
virtual void execute()
Execute the command.
Definition: EditReplace.cpp:72