gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
printermanager.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
22 
26 #include "gambit/Printers/printer_rollcall.hpp"
28 
29 // Switch for debugging output (manual at the moment)
30 //#define DEBUG_MODE
31 
32 #ifdef DEBUG_MODE
33  #define DBUG(x) x
34 #else
35  #define DBUG(x)
36 #endif
37 
38 namespace Gambit
39 {
40 
41  // Set/Get global reader object retrieval
43 
44  // Function to return the value of global_printer_manager
46  {
47  return(global_printer_manager);
48  }
49 
51  {
52  if(global_printer_manager==NULL)
53  {
54  std::ostringstream os;
55  os<<"Tried to retrieve postprocessor reader object, but no PrinterManager has been set! This should be set during scan setup.";
56  Printers::printer_error().raise(LOCAL_INFO,os.str());
57  }
58 
59  if(not global_printer_manager->reader_exists("old_points"))
60  {
61  std::ostringstream errmsg;
62  errmsg<<"No postprocessor reader object found! This probably means that you are not scanning using the postprocessor. To retrieve data from previous output you must scan with the 'postproccesor' scanner.";
63  Printers::printer_error().raise(LOCAL_INFO,errmsg.str());
64  }
65 
66  return *(global_printer_manager->get_full_reader("old_points"));
67  }
68 
70  {
71  global_printer_manager = pm;
72  }
73 
74 
75  namespace Printers
76  {
77 
79  PrinterManager::PrinterManager(const Options& printerNode, bool resume_mode)
80  : BasePrinterManager(resume_mode)
81  , tag(printerNode.getValue<std::string>("printer"))
82  , options(printerNode.getNode("options"))
83  , printerptr(NULL)
84  {
85  // Change printer pointer to actually point to the printer object
86  if( printer_creators.find(tag)!=printer_creators.end() )
87  {
88  // If "tag" names a valid printer, create it.
89  DBUG( std::cout << "PrinterManager: Creating Primary printer \"" << tag << "\"" << std::endl; )
90 
91  // pass on the "resume" flag to the printer
92  Options mod_options = options;
93  mod_options.setValue("resume",resume_mode);
94 
95  BasePrinter* null(NULL);
96  printerptr = printer_creators.at(tag)(mod_options,null);
97 
98  // Update resume flag (primary printer may have switched it off due to lack of existing output)
99  set_resume_mode(printerptr->get_resume());
100  }
101  else
102  {
103  // Otherwise throw an error
104  std::ostringstream os;
105  os << "Inifile entry 'printer:\""<<tag<<"\"' does not specify a valid printer! Please choose from one of the following:"<<std::endl;
106 
107  for (auto it = printer_creators.begin(); it != printer_creators.end(); ++it)
108  {
109  os << " " << it->first << std::endl;
110  }
111 
112  printer_error().raise(LOCAL_INFO,os.str());
113  }
114  }
115 
117  void PrinterManager::delete_stream(const std::string& name)
118  {
119  if(name=="")
120  {
121  std::ostringstream os;
122  os << "Tried to delete primary printer stream! Currently you are not permitted to do this. Why do you want to?";
123  printer_error().raise(LOCAL_INFO,os.str());
124  }
125  else
126  {
127  auto it = auxprinters.find(name);
128  if(it!=auxprinters.end())
129  {
130  delete it->second;
131  auxprinters.erase(it);
132  }
133  else
134  {
135  std::ostringstream os;
136  os << "Tried to delete printer stream '"<<name<<"', but could not find a print stream with that name! Perhaps it was never created, or was deleted already?";
137  printer_error().raise(LOCAL_INFO,os.str());
138  }
139  }
140  }
141 
142  void PrinterManager::delete_reader(const std::string& name)
143  {
144  auto it = readers.find(name);
145  if(it!=readers.end())
146  {
147  delete it->second;
148  readers.erase(it);
149  }
150  else
151  {
152  std::ostringstream os;
153  os << "Tried to delete reader object '"<<name<<"', but could not find a reader object with that name! Perhaps it was never created, or was deleted already?";
154  printer_error().raise(LOCAL_INFO,os.str());
155  }
156  }
157 
159  {
160  // Delete all the printer objects
161  DBUG( std::cout << "PrinterManager: Destructing printers and readers..." << std::endl; )
162  typedef std::map<std::string, BasePrinter*>::iterator it_type;
163  typedef std::map<std::string, BaseReader*>::iterator it2_type;
164  for(it_type it = auxprinters.begin(); it != auxprinters.end(); it++) {
165  delete it->second; // Delete the printer to which this pointer points.
166  }
167  for(it2_type it = readers.begin(); it != readers.end(); it++) {
168  delete it->second; // Delete the reader to which this pointer points.
169  }
170  delete printerptr; // Delete primary printer
171  }
172 
173  // Create new printer object (of the same type as the primary printer)
174  // and attach it to the provided name.
175  void PrinterManager::new_stream(const std::string& streamname, const Options& new_options)
176  {
177  //TODO need some way for the scanners to change the options
178  //for the auxiliary printers, e.g. so we can print to a different file
179  DBUG( std::cout << "PrinterManager: Creating Auxilliary printer \"" << tag << "\" with name \"" << streamname << "\"" << std::endl; )
180  Options mod_options = new_options;
181  mod_options.setValue("resume",this->resume_mode());
182  mod_options.setValue("auxilliary",true);
183  mod_options.setValue("name",streamname);
184  mod_options.setValue("default_output_path",options.getValue<str>("default_output_path"));
185  // To construct printer as an auxilliary printer, a pointer to the primary printer is supplied as well as the options.
186  auxprinters[streamname] = printer_creators.at(tag)(mod_options,printerptr);
187  // Some printers may requires two-step initiations so this virtual function is provided to allow that.
188  auxprinters.at(streamname)->auxilliary_init();
189  }
190 
191  // Create new printer reader object (of the same type as the primary printer)
192  // and attach it to the provided name.
193  void PrinterManager::new_reader(const std::string& readstreamname, const Options& options)
194  {
195  DBUG( std::cout << "PrinterManager: Creating printer read stream of type \"" << tag << "\" with name \"" << readstreamname << "\"" << std::endl; )
196  std::string whichreader = options.getValueOrDef<std::string>(tag,"type");
197  if(reader_creators.find(whichreader)==reader_creators.end())
198  {
199  std::ostringstream os;
200  os << "PrinterManager: Tried to construct reader with name '"<<readstreamname<<"' as reader-type '"<<whichreader<<"', but this is not a valid reader type! Please choose one of the following:"<<std::endl;
201  for (auto it = reader_creators.begin(); it != reader_creators.end(); ++it)
202  {
203  os << " " << it->first << std::endl;
204  }
205  printer_error().raise(LOCAL_INFO,os.str());
206  }
207  readers[readstreamname] = reader_creators.at(whichreader)(options);
208  }
209 
212  {
213  // The only difficulty here is to get the options needed to build
214  // the reader (filenames etc). This varies between different types
215  // of printers, so we have to ask the primary printer object to
216  // give us these options.
217  new_reader("resume", get_stream()->resume_reader_options());
218  }
219 
220  // Retrieve pointer to named printer object
221  BaseBasePrinter* PrinterManager::get_stream(const std::string& streamname)
222  {
223  DBUG( std::cout << "PrinterManager: Retrieving printer stream \"" << streamname << "\"" << std::endl; )
224  if(streamname=="")
225  {
226  if(printerptr==NULL)
227  {
228  printer_error().raise(LOCAL_INFO,"Error retrieving primary printer from PrinterManager! printerptr==NULL! Must be a bug in the PrinterManager initialisation.");
229  }
230  return printerptr;
231  }
232  else
233  {
234  // Note that this routine automatically converts the BasePrinter pointer into a BaseBasePrinter pointer
235  // (for a more minimal interface for use in ScannerBit)
236  typedef std::map<std::string, BasePrinter*>::iterator it_type;
237  it_type it = auxprinters.find(streamname);
238  if( it == auxprinters.end() )
239  {
240  std::ostringstream errmsg;
241  errmsg << "Error! PrinterManager failed to retrieve the requested auxilliary print stream with name '"<<streamname<<"'! The stream may not have been created in the first place. Please check that the scanner plugin you are using correctly creates a printer stream with this name.";
242  printer_error().raise(LOCAL_INFO, errmsg.str());
243  }
244  return it->second;
245  }
246  }
247 
249  BaseBaseReader* PrinterManager::get_reader(const std::string& readername)
250  {
251  // Note that this routine automatically converts the BaseReader pointer into a BaseBaseReader pointer
252  // (for a more minimal interface for use in ScannerBit)
253  return get_full_reader(readername);
254  }
255 
257  BaseReader* PrinterManager::get_full_reader(const std::string& readername)
258  {
259  DBUG( std::cout << "PrinterManager: Retrieving reader stream \"" << readername << "\"" << std::endl; )
260  // Note that this routine automatically converts the BaseReader pointer into a BaseBaseReader pointer
261  // (for a more minimal interface for use in ScannerBit)
262  typedef std::map<std::string, BaseReader*>::iterator it_type;
263  it_type it = readers.find(readername);
264  if( it == readers.end() )
265  {
266  std::ostringstream errmsg;
267  errmsg << "Error! PrinterManager failed to retrieve the requested reader stream with name '"<<readername<<"'! The reader may not have been created in the first place. Please check that the scanner plugin you are using correctly creates a reader stream with this name.";
268  printer_error().raise(LOCAL_INFO, errmsg.str());
269  }
270  return it->second;
271  }
272 
274  bool PrinterManager::reader_exists(const std::string& readername)
275  {
276  typedef std::map<std::string, BaseReader*>::iterator it_type;
277  it_type it = readers.find(readername);
278  return (it != readers.end());
279  }
280 
282  void PrinterManager::finalise(bool abnormal)
283  {
284  typedef std::map<std::string, BasePrinter*>::iterator it_type;
285  for(it_type it = auxprinters.begin(); it != auxprinters.end(); it++) {
286  it->second->finalise(abnormal);
287  }
288  printerptr->finalise(abnormal);
289  }
290 
291  }
292 }
293 
294 #undef DBUG
295 #undef DEBUG_MODE
Printers::PrinterManager * global_printer_manager
Manager class for creating printer objects.
EXPORT_SYMBOLS error & printer_error()
Printer errors.
void set_global_printer_manager(Printers::PrinterManager *pm)
std::map< std::string, BasePrinter * > auxprinters
Map containing pointers to auxiliary printer objects.
void new_reader(const std::string &, const Options &)
Create reader object.
BasePrinter * printerptr
Pointer to main printer object.
BaseReader * get_full_reader(const std::string &readername)
Retrieve non-basebase version of reader object (for use in module functions rather than ScannerBit) ...
TYPE getNode(const YAML::Node node)
Wrapper for reading the node for a given type.
reg_elem< create_printer_function > printer_creators
#define LOCAL_INFO
Definition: local_info.hpp:34
STL namespace.
BaseBaseReader * get_reader(const std::string &)
Getter for reader objects.
PrinterManager(const Options &, bool resume_mode)
Constructor.
Declarations for the YAML options class.
Manager class for printers.
Printers::BaseReader & get_pp_reader()
void finalise(bool abnormal=false)
Instruct printers that scan has finished and to perform cleanup.
std::string tag
Name specifying the printer type.
void set_resume_mode(bool rflag)
Setter for "resume" mode flag (printer may override user choice if no previous output exists) ...
TYPE getValue(const args &... keys) const
bool reader_exists(const std::string &)
Checker for existence of reader object.
BaseBasePrinter * get_stream(const std::string &="")
Getter for auxiliary printer objects.
std::string str
Definition: sqlitebase.hpp:52
void new_stream(const std::string &, const Options &)
Create auxiliary printer object.
bool resume_mode()
Getter for "resume" mode flag.
Declaration and definition of printer base class.
std::map< std::string, BaseReader * > readers
Map containing pointers to reader objects.
BASE PRINTER CLASS.
TYPE getValueOrDef(TYPE def, const args &... keys) const
reg_elem< create_reader_function > reader_creators
void delete_reader(const std::string &)
Printers::PrinterManager * get_global_printer_manager()
void delete_stream(const std::string &="")
Destruct printer/reader objects.
Manager class for creating printer objects.
This is a minimal (pure virtual) precursor to the printer base class, for use only in ScannerBit...
void setValue(const KEYTYPE &key, const VALTYPE &val)
Basic setter, for adding extra options.
#define DBUG(x)
Options options
Storage for printer options (needed for creating new streams)
TODO: see if we can use this one:
Definition: Analysis.hpp:33
A small wrapper object for &#39;options&#39; nodes.
void create_resume_reader()
Create for reader object for previous print output ("resume reader")