OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
AttList.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: AttList.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: AttList
10 //
11 // ------------------------------------------------------------------------
12 //
13 // $Date: 2001/08/13 15:25:21 $
14 // $Author: jowett $
15 //
16 // ------------------------------------------------------------------------
17 
18 #include "Tables/AttList.h"
20 #include "AbstractObjects/Table.h"
21 #include "Attributes/Attributes.h"
22 #include "Beamlines/Beamline.h"
23 #include "Elements/AttCell.h"
24 #include "Elements/OpalElement.h"
25 #include "Tables/AttWriter.h"
28 #include <fstream>
29 #include <iostream>
30 #include <vector>
31 
32 using std::vector;
33 
34 
35 // Class AttList
36 // ------------------------------------------------------------------------
37 
38 namespace {
39 
40  // The attributes of class AttList.
41  enum {
42  LINE, // The name of the line to be listed.
43  FNAME, // The name of the file to be written.
44  ALL, // If true, list all columns.
45  VALUE, // Which value is desired: "ACTUAL", "IDEAL", "ERROR".
46  COLUMN, // The columns to be written.
47  SIZE
48  };
49 }
50 
51 
53  Action(SIZE, "ATTLIST",
54  "The \"ATTLIST\" statement lists the element strengths "
55  "in a beam line or a sequence.") {
57  ("LINE", "Name of line to be listed");
59  ("FILE", "Name of file to receive output", "ATTLIST");
61  ("ALL", "Are all columns desired?");
63  ("VALUE", "Which value is desired: ACTUAL, IDEAL, or ERROR.", "ACTUAL");
65  ("COLUMN", "The columns to be written");
66 
68 }
69 
70 
71 AttList::AttList(const std::string &name, AttList *parent):
72  Action(name, parent)
73 {}
74 
75 
77 {}
78 
79 
80 AttList *AttList::clone(const std::string &name) {
81  return new AttList(name, this);
82 }
83 
84 
86  // Find beam sequence or table definition.
87  const std::string name = Attributes::getString(itsAttr[LINE]);
88  const Beamline *line = 0;
89 
90  if(Object *obj = OpalData::getInstance()->find(name)) {
91  if(BeamSequence *beamLine = dynamic_cast<BeamSequence *>(obj)) {
92  line = beamLine->fetchLine();
93  } else if(Table *table = dynamic_cast<Table *>(obj)) {
94  line = table->getLine();
95  } else {
96  throw OpalException("Select::execute()",
97  "You cannot do an \"ATTLIST\" on \"" + name +
98  "\", it is neither a line nor a table.");
99  }
100  } else {
101  throw OpalException("Select::execute()",
102  "Object \"" + name + "\" not found.");
103  }
104 
105  // Select the file to be used.
106  const std::string &fileName = Attributes::getString(itsAttr[FNAME]);
107  if(fileName == "TERM") {
108  writeTable(*line, std::cout);
109  } else {
110  std::ofstream os(fileName.c_str());
111 
112  if(os.good()) {
113  writeTable(*line, os);
114  } else {
115  throw OpalException("AttList::execute()",
116  "Unable to open output stream \"" +
117  fileName + "\".");
118  }
119  }
120 }
121 
122 
123 void AttList::writeTable(const Beamline &line, std::ostream &os) {
124  // Type of values desired.
125  const std::string &value = Attributes::getString(itsAttr[VALUE]);
127  if(value == "ACTUAL") {
129  } else if(value == "IDEAL") {
131  } else if(value == "ERROR") {
133  } else {
134  throw OpalException("AttList::writeTable()",
135  "Unknown \"VALUE\" type \"" + value + "\".");
136  }
137 
138  // Construct column access table.
139  // This may throw, if a column is unknown.
140  vector<std::string> header = Attributes::getStringArray(itsAttr[COLUMN]);
141  vector<std::string>::size_type n = header.size();
142  vector<AttCell *> buffer(n);
143  for(vector<std::string>::size_type i = 0; i < n; ++i) {
144  buffer[i] = OpalElement::findRegisteredAttribute(header[i]);
145  }
146 
147  // Write table descriptors.
148  OPALTimer::Timer timer;
149  os << "@ TYPE %s ATTRIBUTE\n"
150  << "@ LINE %s " << line.getName() << "\n"
151  << "@ DATE %s " << timer.date() << "\n"
152  << "@ TIME %s " << timer.time() << "\n"
153  << "@ ORIGIN %s OPAL_9.5/4\n"
154  << "@ COMMENT %s \""
155  << "@ VALUE %s " << value << "\n";
157  os << "\"\n";
158 
159  // Write column header names.
160  os << '*';
161  for(vector<std::string>::size_type i = 0; i < n; ++i) {
162  os << ' ' << header[i];
163  }
164  os << '\n';
165 
166  // Write column header formats.
167  os << '$';
168  for(vector<std::string>::size_type i = 0; i < n; ++i) {
169  os << ' ';
170  buffer[i]->printFormat(os);
171  buffer[i]->clearValue();
172  }
173  os << '\n';
174 
175  // List the table body.
176  AttWriter writer(line, os, flag, buffer);
177  writer.execute();
178 }
The ATTLIST command.
Definition: AttList.h:31
virtual AttList * clone(const std::string &name)
Make clone.
Definition: AttList.cpp:80
ValueFlag
Switch for value desired on ATTLIST command.
Definition: OpalElement.h:71
The base class for all OPAL actions.
Definition: Action.h:30
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual void execute()
Execute the command.
Definition: AttList.cpp:85
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
void writeTable(const Beamline &line, std::ostream &os)
Definition: AttList.cpp:123
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
virtual ~AttList()
Definition: AttList.cpp:76
std::string date() const
Return date.
Definition: Timer.cpp:35
void printTitle(std::ostream &)
Print the page title.
Definition: OpalData.cpp:705
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
Definition: Attributes.cpp:373
static OpalData * getInstance()
Definition: OpalData.cpp:209
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
Definition: Attributes.cpp:378
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
AttList()
Exemplar constructor.
Definition: AttList.cpp:52
The worker class for ATTLIST commands.
Definition: AttWriter.h:53
An abstract sequence of beam line components.
Definition: Beamline.h:37
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
virtual void execute()
Apply the algorithm to the top-level beamline.
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
std::string time() const
Return time.
Definition: Timer.cpp:42
The base class for all OPAL objects.
Definition: Object.h:48
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
const std::string name
static AttCell * findRegisteredAttribute(const std::string &name)
Find a registered attribute.
The base class for all OPAL tables.
Definition: Table.h:42
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307