OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Save.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Save.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.3 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Save
10 // The base class for the OPAL SAVE command.
11 //
12 // Note by JMJ 6/4/2000:
13 // According to some old version of the manual this should have a PATTERNS option
14 // to allow only a certain set of parameters to be saved.
15 // Not so, it seems to just save everything. I opale changes to the print method
16 // of the ConcreteVar class to partly compensate for this deficiency: that allows
17 // you to get the variables used in matching in OPAL input syntax, at least on the
18 // main output file.
19 //
20 // ------------------------------------------------------------------------
21 //
22 // $Date: 2001/08/14 07:02:44 $
23 // $Author: jowett $
24 //
25 // ------------------------------------------------------------------------
26 
27 #include "BasicActions/Save.h"
34 #include "Attributes/Attributes.h"
36 #include "Utilities/Options.h"
37 #include "Utilities/Util.h"
38 #include <fstream>
39 #include <string>
40 #include "OPALconfig.h"
41 
42 extern Inform *gmsg;
43 
44 // Functors for flagging objects and saving special categories.
45 // ------------------------------------------------------------------------
46 
47 namespace SaveNS {
48 
49  // Functor for flagging an object.
50  // ----------------------------------------------------------------------
52  virtual void operator()(Object *) const;
53  };
54 
55  void ObjectFlagger::operator()(Object *object) const {
56  // Only output objects which have a parent, and which are not built-in.
57  object->setFlag(object->getParent() != 0 && ! object->isBuiltin());
58  }
59 
60  // Functor for saving an element.
61  // ----------------------------------------------------------------------
63  ElementWriter(std::ostream &ostr): os(ostr) { }
64  virtual void operator()(Object *) const;
65  private:
66  std::ostream &os;
67  };
68 
69  void ElementWriter::operator()(Object *object) const {
70  if(object->isFlagged() && dynamic_cast<Element *>(object) &&
71  ! dynamic_cast<BeamSequence *>(object)) {
72  if(object->getOpalName()[0] != '#') {
73  (*this)(object->getParent());
74  os << object;//->print(*gmsg);
75  object->setFlag(false);
76  }
77  }
78  }
79 
80  // Functor for saving a parameter.
81  // ----------------------------------------------------------------------
83  ParameterWriter(std::ostream &ostr): os(ostr) { }
84  virtual void operator()(Object *) const;
85  private:
86  std::ostream &os;
87  };
88 
89  void ParameterWriter::operator()(Object *object) const {
90  if(object->isFlagged() && dynamic_cast<ValueDefinition *>(object)) {
91  os << object;//->print(*gmsg);
92  object->setFlag(false);
93  }
94  }
95 
96  // Functor for saving a special definition.
97  // ----------------------------------------------------------------------
99  SpecialWriter(std::ostream &ostr): os(ostr) { }
100  virtual void operator()(Object *) const;
101  private:
102  std::ostream &os;
103  };
104 
105  void SpecialWriter::operator()(Object *object) const {
106  if(object->isFlagged() && dynamic_cast<Definition *>(object)) {
107  (*this)(object->getParent());
108  os << object;//->print(*gmsg);
109  object->setFlag(false);
110  }
111  }
112 }
113 
114 
115 using namespace SaveNS;
116 
117 
118 
119 // Class Save
120 // ------------------------------------------------------------------------
121 
123  Action(1, "SAVE",
124  "The \"SAVE\" statement prints a list of all definitions,\n"
125  "starting with constants, variables, and vectors,"
126  "followed by elements, and finally all sequences.") {
128  ("FILE", "Name of file to be written", "SAVE");
129 
131 }
132 
133 
134 Save::Save(const std::string &name, Save *parent):
135  Action(name, parent)
136 {}
137 
138 
140 {}
141 
142 
143 Save *Save::clone(const std::string &name) {
144  return new Save(name, this);
145 }
146 
147 
149  std::string file = Attributes::getString(itsAttr[0]);
150  std::ofstream os(file.c_str());
151 
152  if(os.bad()) {
153  throw OpalException("Save::execute()",
154  "Unable to open output stream \"" + file + "\".");
155  } else {
156  // Flag all objects to be saved.
158 
159 
160  // Now save all objects according to categories.
161  //JMJ adding some comment tags to saved output 25/10/2000
162  //JMJ more of those 18/12/2000
163 
164  std::string comchar = "// ";
165 
166  os << comchar << "<OPAL Version " << OPAL_PROJECT_VERSION << " GIT version "
167  << Util::getGitRevision() << " (c) PSI, http://amas.web.psi.ch"
168  << std::endl << ";" << std::endl ;
169 
170  os << comchar << "<Parameter definitions> ;" << std::endl ;
172  os << comchar << "</Parameter definitions> ;"
173  << std::endl << ";" << std::endl ;
174 
175  os << comchar << "<Element definitions> ;" << std::endl ;
177  os << comchar << "</Element definitions> ;"
178  << std::endl << ";" << std::endl ;
179 
180  os << comchar << "<Line (and split element) definitions> ;"
181  << std::endl ;
182 
183  os << comchar << "</Line (and split element) definitions> ;"
184  << std::endl << ";" << std::endl ;
185 
186  os << comchar << "<Special definitions> ;" << std::endl ;
188  os << comchar << "</Special definitions> ;"
189  << std::endl << ";" << std::endl ;
190 
191  os << comchar << "<OPAL Version " << OPAL_PROJECT_VERSION << " GIT version "
192  << Util::getGitRevision() << " (c) PSI, http://amas.web.psi.ch"
193  << std::endl << ";" << std::endl ;
194  }
195 }
196 
197 
198 void Save::parse(Statement &statement) {
199  parseShortcut(statement);
200 }
ParameterWriter(std::ostream &ostr)
Definition: Save.cpp:83
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition: Object.cpp:273
virtual void parseShortcut(Statement &)
Parser for single-attribute commands.
Definition: Object.cpp:132
std::ostream & os
Definition: Save.cpp:66
The base class for all OPAL definitions.
Definition: Definition.h:30
The SAVE command.
Definition: Save.h:28
The base class for all OPAL actions.
Definition: Action.h:30
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition: Object.cpp:242
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual void operator()(Object *) const
The function to be executed.
Definition: Save.cpp:69
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:289
Inform * gmsg
Definition: Main.cpp:21
void parse(Statement &)
Parse command (special for one-attribute command).
Definition: Save.cpp:198
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
virtual void operator()(Object *) const
The function to be executed.
Definition: Save.cpp:55
void apply(const ObjectFunction &)
Apply a function to all objects.
Definition: OpalData.cpp:516
virtual Save * clone(const std::string &name)
Make clone.
Definition: Save.cpp:143
virtual ~Save()
Definition: Save.cpp:139
static OpalData * getInstance()
Definition: OpalData.cpp:209
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
virtual void operator()(Object *) const
The function to be executed.
Definition: Save.cpp:89
The base class for all OPAL elements.
Definition: Element.h:46
Interface for statements.
Definition: Statement.h:38
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
#define OPAL_PROJECT_VERSION
Definition: OPALconfig.h:5
ElementWriter(std::ostream &ostr)
Definition: Save.cpp:63
The base class for all OPAL objects.
Definition: Object.h:48
Save()
Exemplar constructor.
Definition: Save.cpp:122
virtual void execute()
Execute the command.
Definition: Save.cpp:148
The base class for all OPAL value definitions.
const std::string name
std::string getGitRevision()
Definition: Util.cpp:15
std::ostream & os
Definition: Save.cpp:102
Abstract base class for functor objects whose argument is an Object.
std::ostream & os
Definition: Save.cpp:86
virtual void operator()(Object *) const
The function to be executed.
Definition: Save.cpp:105
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:268
SpecialWriter(std::ostream &ostr)
Definition: Save.cpp:99
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307