STIR  6.2.0
KeyParser.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000 PARAPET partners
3  Copyright (C) 2000 - 2007-10-08, Hammersmith Imanet Ltd
4  Copyright (C) 2013, 2020, 2021, 2023, 2024 University College London
5  This file is part of STIR.
6 
7  SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
8 
9  See STIR/LICENSE.txt for details
10 */
22 #ifndef __stir_KEYPARSER_H__
23 #define __stir_KEYPARSER_H__
24 
25 #include "stir/shared_ptr.h"
26 #include "stir/Array.h"
27 #include "stir/warning.h"
28 #include "boost/any.hpp"
29 
30 #include <map>
31 #include <list>
32 #include <utility>
33 #include <string>
34 #include <iostream>
35 #include <vector>
36 
37 START_NAMESPACE_STIR
38 
39 typedef std::vector<std::string> ASCIIlist_type;
40 
41 class RegisteredObjectBase;
42 class Succeeded;
43 
44 class KeyParser;
45 
56 {
57 public:
58  enum type
59  {
60  NONE,
61  ASCII,
62  LIST_OF_ASCII,
63  ASCIIlist,
64  UINT,
65  ULONG,
66  LONG,
67  INT,
68  LIST_OF_INTS,
69  DOUBLE,
70  LIST_OF_DOUBLES,
71  listASCIIlist,
72  ARRAY2D_OF_FLOATS,
73  ARRAY3D_OF_FLOATS,
74  BASICCOORDINATE3D_OF_FLOATS,
75  BASICCOORDINATE3D_OF_ARRAY3D_OF_FLOATS,
76  PARSINGOBJECT,
77  SHARED_PARSINGOBJECT,
78  FLOAT,
79  BOOL
80  };
81 };
82 
91 {
92 public:
93  KeyArgument::type type;
94  void (KeyParser::*p_object_member)(); // pointer to a member function
95  // TODO void (*p_object_member)();
96  void* p_object_variable; // pointer to a variable
97  int vectorised_key_level;
98  const ASCIIlist_type* p_object_list_of_values; // only used by ASCIIlist
99  // TODO should really not be here, but it works for now
100  typedef RegisteredObjectBase*(Parser)(std::istream*, const std::string&);
101  Parser* parser;
102 
103  map_element();
104 
105  // map_element(KeyArgument::type t, void (KeyParser::*object_member_ptr)());
106 
107  map_element(KeyArgument::type t,
108  void (KeyParser::*object_member_ptr)(),
109  void* variable_ptr,
110  const int vectorised_key_level,
111  const ASCIIlist_type* list_of_valid_keywords = 0);
112  map_element(void (KeyParser::*pom)(), RegisteredObjectBase** pov, Parser*);
113  map_element(void (KeyParser::*pom)(), shared_ptr<RegisteredObjectBase>* pov, Parser*);
114  ~map_element();
115 
116  map_element& operator=(const map_element& me);
117 };
118 
119 // note: next text contains double \\ for correct parsing by doxygen
120 // you should read them as a single backslash
162 {
163  // historical typedefs
164  typedef std::vector<int> IntVect;
165  typedef std::vector<double> DoubleVect;
166 
167 public:
168  KeyParser();
169  virtual ~KeyParser();
170 
172 
173  bool parse(std::istream& f, const bool write_warnings = true);
175 
176  bool parse(const char* const filename, const bool write_warnings = true);
178  bool parse(const std::string&, const bool write_warnings = true);
179 
181 
182  // KT 07/02/2001 all these are new (up to add_parsing_key)
183 
185  void add_key(const std::string& keyword, float* variable_ptr);
187  void add_vectorised_key(const std::string& keyword, std::vector<float>* variable_ptr);
189  void add_key(const std::string& keyword, double* variable_ptr);
191  void add_vectorised_key(const std::string& keyword, std::vector<double>* variable_ptr);
193  void add_vectorised_key(const std::string& keyword, std::vector<std::vector<double>>* variable_ptr);
194 
196  void add_key(const std::string& keyword, int* variable_ptr);
198  void add_key(const std::string& keyword, std::vector<int>* variable_ptr);
200  void add_vectorised_key(const std::string& keyword, std::vector<int>* variable_ptr);
202  void add_vectorised_key(const std::string& keyword, std::vector<std::vector<int>>* variable_ptr);
203 
205  void add_key(const std::string& keyword, long int* variable_ptr);
206 
208  void add_key(const std::string& keyword, unsigned int* variable_ptr);
210  void add_vectorised_key(const std::string& keyword, std::vector<unsigned int>* variable);
212  void add_key(const std::string& keyword, unsigned long* variable_ptr);
214  void add_vectorised_key(const std::string& keyword, std::vector<unsigned long>* variable_ptr);
215 
217 
218  void add_key(const std::string& keyword, bool* variable_ptr);
219 
221  void add_key(const std::string& keyword, std::vector<double>* variable_ptr);
222 
224  void add_key(const std::string& keyword, std::vector<std::string>* variable_ptr);
225 
227  void add_key(const std::string& keyword, Array<2, float>* variable_ptr);
228 
230  void add_key(const std::string& keyword, Array<3, float>* variable_ptr);
231 
233  void add_key(const std::string& keyword, BasicCoordinate<3, float>* variable_ptr);
234 
237  void add_key(const std::string& keyword, BasicCoordinate<3, Array<3, float>>* variable_ptr);
238 
240  void add_key(const std::string& keyword, std::string* variable_ptr);
242 
243  void add_vectorised_key(const std::string& keyword, std::vector<std::string>* variable_ptr);
252  void add_key(const std::string& keyword, int* variable_ptr, const ASCIIlist_type* const list_of_values);
253 
255  void ignore_key(const std::string& keyword);
257 
258  void add_start_key(const std::string& keyword);
260  void add_stop_key(const std::string& keyword);
261 
263 
301  // definition of next function has to be here to get it to compile with VC (it uses member templates)
302  template <typename ParsingClass>
303  void add_parsing_key(const std::string& keyword, ParsingClass** parsed_object_ptr_ptr)
304  {
305  add_in_keymap(keyword,
306  map_element(&KeyParser::set_parsing_object,
307  reinterpret_cast<RegisteredObjectBase**>(parsed_object_ptr_ptr),
308  (map_element::Parser*)(&ParsingClass::read_registered_object)));
309  }
310 
312 
313  template <typename ParsingClass>
314  void add_parsing_key(const std::string& keyword, shared_ptr<ParsingClass>* parsed_object_ptr_ptr)
315  {
316  add_in_keymap(keyword,
317  map_element(&KeyParser::set_shared_parsing_object,
318  reinterpret_cast<shared_ptr<RegisteredObjectBase>*>(parsed_object_ptr_ptr),
319  (map_element::Parser*)(&ParsingClass::read_registered_object)));
320  }
321 
323 
324  bool remove_key(const std::string& keyword);
325 
327 
331  void add_alias_key(const std::string& keyword, const std::string& alias, bool deprecated_key = true);
333  void print_keywords_to_stream(std::ostream&) const;
334 
335  // KT 07/02/2001 new
337 
340  virtual std::string parameter_info() const;
341 
342  // KT 07/02/2001 new
344 
352  virtual void ask_parameters();
353 
355 
358  int find_in_ASCIIlist(const std::string&, const ASCIIlist_type& list_of_values);
359 
360 protected:
362 
364 
368  virtual bool post_processing() { return false; }
369 
371 
373  virtual std::string standardise_keyword(const std::string& keyword) const;
374 
376 
378  virtual std::string get_keyword(const std::string&) const;
379 
380  typedef void (KeyParser::*KeywordProcessor)();
381  // TODO typedef void (*KeywordProcessor)();
382 
384 
388  void add_key(const std::string& keyword,
389  KeyArgument::type t,
390  KeywordProcessor function,
391  void* variable = 0,
392  const ASCIIlist_type* const list = 0);
394 
398  void add_key(const std::string& keyword,
399  KeyArgument::type t,
400  KeywordProcessor function,
401  void* variable,
402  const int vectorised_key_level,
403  const ASCIIlist_type* const list = 0);
404 
406 
407  void add_key(const std::string& keyword, KeyArgument::type t, void* variable, const ASCIIlist_type* const list = 0);
409 
410  void add_key(const std::string& keyword,
411  KeyArgument::type t,
412  void* variable,
413  const int vectorised_key_level,
414  const ASCIIlist_type* const list = 0);
415 
416 public:
419  void start_parsing();
421  void stop_parsing();
423  void do_nothing(){};
425 
426  void set_variable();
427 
430 
431  void set_parsing_object();
434 
435  void set_shared_parsing_object();
436 
437 private:
438  friend class map_element;
440 
441  enum parse_status
442  {
443  end_parsing,
444  parsing
445  };
446  parse_status status;
447 
448  // KT 01/05/2001 changed to a list, to preserve order of keywords
449  // typedef std::map<std::string,map_element> Keymap;
450  typedef std::list<std::pair<std::string, map_element>> Keymap;
451 
452  Keymap kmap;
453 
455  typedef std::map<std::string, std::string> AliasMap;
456 
457  AliasMap alias_map;
458  AliasMap deprecated_alias_map;
459 
460  // KT 01/05/2001 new functions to allow a list type
461  map_element* find_in_keymap(const std::string& keyword);
462  void add_in_keymap(const std::string& keyword, const map_element& new_element);
463 
465  std::string resolve_alias(const std::string& keyword) const;
466 
467  std::istream* input;
468  map_element* current;
469  int current_index;
470  std::string keyword;
471 
472  // next will be false when there's only a keyword on the line
473  // maybe should be protected (or even public?). At the moment, this is only used by set_variable().
474  // That should cover most cases.
475  bool keyword_has_a_value;
476  boost::any parameter;
477 
479 
481  static void value_to_stream(std::ostream& s, const map_element&);
483  static void vectorised_value_to_stream(std::ostream& s, const std::string& keyword, const map_element& element);
484 
486  Succeeded parse_header(const bool write_warnings);
487 
489  Succeeded read_and_parse_line(const bool write_warning);
490 
492  // see if current keyword is in the keymap using map_keyword
493  // if so, call its call_back function and return Succeeded::yes, else
494  // conditionally write a warning and return Succeeded::no
495  Succeeded parse_value_in_line(const std::string& line, const bool write_warning);
496 
498  // return Succeeded::yes if valid keyword, Succeeded::no otherwise
499  Succeeded map_keyword(const std::string& keyword);
500 
502  void process_key();
503 };
504 
505 END_NAMESPACE_STIR
506 
507 #endif
void add_parsing_key(const std::string &keyword, ParsingClass **parsed_object_ptr_ptr)
add keyword corresponding to an object that will parse the next keys itself
Definition: KeyParser.h:303
A class to parse Interfile headers.
Definition: KeyParser.h:161
Class to store the Interfile keywords and their actions.
Definition: KeyParser.h:90
virtual bool post_processing()
This will be called at the end of the parsing.
Definition: KeyParser.h:368
Import of std::shared_ptr, std::dynamic_pointer_cast and std::static_pointer_cast (or corresponding b...
defines the Array class for multi-dimensional (numeric) arrays
A class that enumerates the possible types that can be used to store parsed values. Used (only) by KeyParser.
Definition: KeyParser.h:55
void do_nothing()
callback function for keys which do not do anything
Definition: KeyParser.h:423
Base class for all classes that can parse .par files (and more?)The only reason that this class exist...
Definition: RegisteredObjectBase.h:40
void add_parsing_key(const std::string &keyword, shared_ptr< ParsingClass > *parsed_object_ptr_ptr)
add keyword corresponding to an object that will parse the next keys itself
Definition: KeyParser.h:314
class BasicCoordinate<int num_dimensions, typename coordT> defines num_dimensions -dimensional coordi...
Definition: BasicCoordinate.h:53
Declaration of stir::warning()
a class containing an enumeration type that can be used by functions to signal successful operation o...
Definition: Succeeded.h:43