Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JProperties.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JPROPERTIES__
2 #define __JEEP__JPROPERTIES__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <fstream>
8 #include <sstream>
9 #include <map>
10 #include <vector>
11 
12 #include "JLang/JAbstractIO.hh"
13 #include "JLang/JException.hh"
14 #include "JLang/JSharedPointer.hh"
16 #include "JLang/JEquationFacet.hh"
17 #include "JLang/JEquation.hh"
19 #include "JLang/JCategory.hh"
20 #include "JLang/JClass.hh"
21 #include "Jeep/JStreamToolkit.hh"
23 #include "Jeep/JMessage.hh"
24 
25 
26 /**
27  * \file
28  * Utility class to parse parameter values.
29  * \author mdejong
30  */
31 namespace JEEP {}
32 namespace JPP { using namespace JEEP; }
33 
34 namespace JEEP {
35 
36  using JLANG::JStreamInput;
44  using JLANG::JEquation;
45  using JLANG::JComparisonAvailable;
47 
48 
49  /**
50  * Check for stream state.
51  *
52  * Note that end-of-file is not defined as an error so to normally process e.g.\ std::string and std::vector.
53  *
54  * \param in input stream
55  * \return true if failure; else false
56  */
57  inline bool fail(std::istream& in)
58  {
59  return in.bad() || (in.fail() && !in.eof());
60  }
61 
62 
63  /**
64  * Interface for I/O of properties element.
65  */
67  public JStreamInput,
68  public JStreamOutput,
69  public JStreamSuffixOutput
70  {
71  public:
72 
75 
76 
77  /**
78  * Get properties type.
79  *
80  * \return false
81  */
82  virtual bool is_properties() const
83  {
84  return false;
85  }
86 
87 
88  /**
89  * Equality between property element interfaces.
90  *
91  * \param element properties element interface
92  * \return false
93  */
94  virtual bool equals(const JPropertiesElementInterface& element) const
95  {
96  return false;
97  }
98  };
99 
100 
101  class JPropertiesElement; // Forward declaration.
102 
103 
104  /**
105  * Template class for I/O of properties element.
106  *
107  * This class implements the JPropertiesElementInterface interface.
108  */
109  template<class T>
112  {
113 
114  friend class JPropertiesElement;
115 
116  public:
117  /**
118  * Constructor.
119  *
120  * \param value reference of template object
121  */
124  object(value)
125  {}
126 
127 
128  /**
129  * Stream input.
130  *
131  * \param in input stream
132  * \return input stream
133  */
134  virtual std::istream& read(std::istream& in) override
135  {
136  using namespace std;
137 
138  readObject(in, object);
139 
140  string buffer;
141 
142  in >> buffer;
143 
144  if (!buffer.empty()) {
145  THROW(JPropertiesException, "JProperties: pending data <" << buffer << (in.peek() != EOF ? "..." : "") << ">");
146  }
147 
148  return in;
149  }
150 
151 
152  /**
153  * Stream output.
154  *
155  * \param out output stream
156  * \param prefix prefix
157  * \param postfix postfix
158  * \return output stream
159  */
160  virtual std::ostream& write(std::ostream& out,
161  const char* prefix,
162  const char postfix) const override
163  {
164  return writeObject(out, prefix, object, postfix);
165  }
166 
167 
168  /**
169  * Stream output.
170  *
171  * \param out output stream
172  * \return output stream
173  */
174  virtual std::ostream& write(std::ostream& out) const override
175  {
176  return writeObject(out, object);
177  }
178 
179 
180  /**
181  * Equality between property element interfaces.
182  *
183  * \param element properties element interface
184  * \return true if equal; else false
185  */
186  virtual bool equals(const JPropertiesElementInterface& element) const override
187  {
188  const JPropertiesTemplateElement<T>* p = dynamic_cast<const JPropertiesTemplateElement<T>*>(&element);
189 
190  if (p != NULL)
191  return compareObjects(object, p->object);
192  else
193  return false;
194  }
195 
196 
197  protected:
199  };
200 
201 
202  /**
203  * Template specialisation of JPropertiesTemplateElement for const data type.
204  *
205  * This class implements the JPropertiesElementInterface interface.
206  */
207  template<class T>
210  {
211 
212  friend class JPropertiesElement;
213 
214  public:
215  /**
216  * Constructor.
217  *
218  * \param value reference of template object
219  */
220  JPropertiesTemplateElement(const T& value) :
222  object(value)
223  {}
224 
225 
226  /**
227  * Stream input.
228  *
229  * \param in input stream
230  * \return input stream
231  */
232  virtual std::istream& read(std::istream& in) override
233  {
234  THROW(JPropertiesException, "JPropertiesTemplateElement<>::read() reading of const data type.");
235  }
236 
237 
238  /**
239  * Stream output.
240  *
241  * \param out output stream
242  * \return output stream
243  */
244  virtual std::ostream& write(std::ostream& out) const override
245  {
246  return writeObject(out, object);
247  }
248 
249 
250  /**
251  * Stream output.
252  *
253  * \param out output stream
254  * \param prefix prefix
255  * \param postfix postfix
256  * \return output stream
257  */
258  virtual std::ostream& write(std::ostream& out,
259  const char* prefix,
260  const char postfix) const override
261  {
262  return writeObject(out, prefix, object, postfix);
263  }
264 
265 
266  /**
267  * Equality between property element interfaces.
268  *
269  * \param element properties element interface
270  * \return true if equal; else false
271  */
272  virtual bool equals(const JPropertiesElementInterface& element) const override
273  {
274  const JPropertiesTemplateElement<const T>* p = dynamic_cast<const JPropertiesTemplateElement<const T>*>(&element);
275 
276  if (p != NULL)
277  return compareObjects(object, p->object);
278  else
279  return false;
280  }
281 
282 
283  protected:
284  const T& object;
285  };
286 
287 
288  /**
289  * The property value class.
290  * This class consists of a pointer to the JPropertiesElementInterface.
291  * This class implements the assignment and type conversion operators.
292  */
294  public JSharedPointer<JPropertiesElementInterface>
295  {
296  public:
297  /**
298  * Default constructor.
299  */
301  end_marker(false)
302  {}
303 
304 
305  /**
306  * Constructor.
307  *
308  * \param value reference to template object
309  */
310  template<class T>
312  end_marker(false)
313  {
315  }
316 
317 
318  /**
319  * Equality between property elements.
320  *
321  * \param element properties element
322  * \return true if equal; else false
323  */
324  bool equals(const JPropertiesElement& element) const
325  {
326  return get()->equals(*element.get());
327  }
328 
329 
330  /**
331  * Assignment operator.
332  *
333  * \param value reference to template object
334  * \return this JPropertiesElement
335  */
336  template<class T>
338  {
340 
341  return *this;
342  }
343 
344 
345  /**
346  * Get value.
347  *
348  * \return value of this JPropertiesElement
349  */
350  template<class T>
351  const T& getValue() const
352  {
353  const JPropertiesTemplateElement<T>* p = dynamic_cast<const JPropertiesTemplateElement<T>*>(this->get());
354 
355  if (p != NULL)
356  return p->object;
357  else
358  THROW(JPropertiesException, "Inconsistent data type.");
359  }
360 
361 
362  /**
363  * Get value.
364  *
365  * \return value of this JPropertiesElement
366  */
367  template<class T>
369  {
370  JPropertiesTemplateElement<T>* p = dynamic_cast<JPropertiesTemplateElement<T>*>(this->get());
371 
372  if (p != NULL)
373  return p->object;
374  else
375  THROW(JPropertiesException, "Inconsistent data type.");
376  }
377 
378 
379 
380  /**
381  * Set value of this JPropertiesElement.
382  *
383  * \param value value
384  */
385  template<class T>
386  void setValue(const T& value)
387  {
388  JPropertiesTemplateElement<T>* p = dynamic_cast<JPropertiesTemplateElement<T>*>(this->get());
389 
390  if (p != NULL)
391  p->object = value;
392  else
393  THROW(JPropertiesException, "Inconsistent data type.");
394  }
395 
396 
397  /**
398  * Convert to string.
399  *
400  * \return value of this JPropertiesElement
401  */
403  {
404  std::ostringstream os;
405 
406  get()->write(os, "", '\0');
407 
408  return os.str();
409  }
410 
411 
412  /**
413  * Convert to template type.
414  *
415  * \return value of this JPropertiesElement
416  */
417  template<class T>
418  T toValue() const
419  {
420  T value;
421 
422  std::istringstream in(this->toString());
423 
424  in >> value;
425 
426  return value;
427  }
428 
429 
430  /**
431  * Type conversion operator.
432  *
433  * \return value of this JPropertiesElement
434  */
435  template<class T>
436  operator const T&() const
437  {
438  return getValue<T>();
439  }
440 
441 
442  /**
443  * Get end marker.
444  *
445  * \return end marker
446  */
447  bool getEndMarker() const
448  {
449  return end_marker;
450  }
451 
452 
453  /**
454  * Set end marker.
455  *
456  * \param marker if true stop reading after this properties element, else continue
457  */
458  void setEndMarker(const bool marker)
459  {
460  end_marker = marker;
461  }
462 
463 
464  private:
467  };
468 
469 
470  /**
471  * Utility class to parse parameter values.
472  *
473  * The mapping between a parameter (of any type) and a value
474  * has to be defined in the user's program, e.g.
475  *
476  *\code{.cpp}
477 
478  #include "Jeep/JProperties.hh"
479 
480  ifstream in(filename.c_str());
481 
482  JProperties zap;
483 
484  int integer_value;
485  double double_value;
486  string string_value;
487 
488  zap.insert(make_property(integer_value)); // key == parameter name
489  zap.insert(make_property(double_value)); // key == parameter name
490 
491  zap["mies"] = string_value;
492 
493  zap.read(in);
494  zap.write(cout);
495  \endcode
496  */
497  class JProperties :
498  public std::map<std::string, JPropertiesElement>,
499  public JEquationParameters,
500  public JMessage<JProperties>
501  {
502  public:
503 
505 
507 
508 
509  /**
510  * Utility method to strip off all leading characters from a string until specified character(s).
511  *
512  * \param buffer input string
513  * \param sep last character(s) to strip
514  * \return modified string
515  */
516  static inline std::string getKey(const std::string& buffer, const std::string& sep)
517  {
518  using namespace std;
519 
520  const size_type pos = buffer.find_last_of(sep);
521 
522  if (pos != string::npos)
523  return buffer.substr(pos + 1);
524  else
525  return buffer;
526  }
527 
528 
529  /**
530  * Auxiliary class to handle input from file.
531  *
532  * The assignment of a value will cause the file with the corresponding name to be opened and read.
533  * The contents of the file are processed in the same way as any input to the associated JProperties object.
534  */
535  class JFileReader {
536  public:
537  JFileReader(JProperties& __properties) :
538  properties(__properties)
539  {}
540 
541 
542  /**
543  * Assignment operator.
544  *
545  * \param file_name file name
546  * \return this JFileReader
547  */
548  JFileReader& operator=(const std::string& file_name)
549  {
550  read(file_name);
551 
552  return *this;
553  }
554 
555 
556  /**
557  * Stream input.
558  *
559  * \param in input stream
560  * \param fileReader file reader object
561  * \return input stream
562  */
563  friend inline std::istream& operator>>(std::istream& in, JProperties::JFileReader& fileReader)
564  {
565  std::string file_name;
566 
567  in >> file_name;
568 
569  fileReader.read(file_name);
570 
571  return in;
572  }
573 
574 
575  /**
576  * Stream output.
577  *
578  * \param out output stream
579  * \param fileReader file reader
580  * \return output stream
581  */
582  friend inline std::ostream& operator<<(std::ostream& out, const JProperties::JFileReader& fileReader)
583  {
584  return out;
585  }
586 
587 
588  private:
589  /**
590  * Read properties from file.
591  *
592  * \param file_name file name
593  */
594  void read(const std::string& file_name)
595  {
596  std::ifstream in(file_name.c_str());
597 
598  if (!in) {
599  THROW(JFileOpenException, "JFileReader: error opening file " << file_name);
600  }
601 
602  properties.read(in);
603 
604  if (in.bad()) {
605  THROW(JFileReadException, "JFileReader: error reading file " << file_name);
606  }
607 
608  in.close();
609  }
610 
611 
613  };
614 
615 
616  /**
617  * Constructor.
618  *
619  * \param debug debug level
620  */
621  JProperties(const int debug = 0) :
622  JMap_t(),
624  {
625  this->debug = debug;
626  }
627 
628 
629  /**
630  * Constructor.
631  *
632  * \param parameters equation parameters
633  * \param debug debug level
634  */
636  const int debug = 0) :
637  JMap_t(),
638  JEquationParameters(parameters)
639  {
640  this->debug = debug;
641  }
642 
643 
644  /**
645  * Put object at given key.
646  *
647  * \param key key
648  * \param object object
649  */
650  template<class T>
651  void put(const std::string& key, T& object)
652  {
653  this->insert(value_type(key, JPropertiesElement(object)));
654  }
655 
656 
657  /**
658  * Join properties objects.
659  *
660  * \param properties properties
661  */
662  JProperties& join(const JProperties& properties)
663  {
664  JEquationParameters::join(properties);
665 
666  insert(properties.begin(), properties.end());
667 
668  return *this;
669  }
670 
671 
672  /**
673  * Read equation.
674  *
675  * \param equation equation
676  * \return status
677  */
678  bool read(const JEquation& equation)
679  {
680  using namespace std;
681 
682  iterator p = find(equation.getKey());
683 
684  DEBUG("Processing key: " << equation.getKey() << ' ' << (p != end()) << endl);
685 
686  if (p != end()) {
687 
688  istringstream is(equation.getValue());
689 
690  if (isDivision(equation.getSeparator())) {
691 
692  if (p->second->is_properties()) {
693 
694  p->second->read(is);
695 
696  } else {
697 
698  ERROR("JProperties::read(): no properties object after division <" << equation.getKey() << ">" << endl);
699  }
700 
701  } else if (isSeparator(equation.getSeparator())) {
702 
703  try {
704  p->second->read(is);
705  }
706  catch(const exception& error) {
707  ERROR("JProperties::read(): read error at key <" << equation.getKey() << "> " << error.what() << endl);
708  }
709 
710  } else {
711 
712  ERROR("JProperties::read(): illegal character following key <" << equation.getKey() << "> " << equation.getSeparator() << endl);
713  }
714 
715  if (p->second.getEndMarker()) {
716  return false;
717  }
718 
719  if (fail(is)) {
720 
721  ERROR("JProperties::read(): error reading data for key <" << equation.getKey() << "> " << equation.getValue() << endl);
722  }
723 
724  } else {
725 
726  WARNING("JProperties::read(): unknown key <" << equation.getKey() << ">" << endl);
727  }
728 
729  return true;
730  }
731 
732 
733  /**
734  * Read from input string.
735  *
736  * \param buffer input string
737  * \return read status
738  */
739  bool read(const std::string& buffer)
740  {
741  std::istringstream in(buffer);
742 
743  return !fail(read(in));
744  }
745 
746 
747  /**
748  * Read from input stream.
749  *
750  * The input format is:
751  * <pre>
752  * [<key><sub>]<key><sep><value><eol>
753  * [<key><sub>]<key><sep><value><eol>
754  * </pre>
755  * In this, white spaces are ignored.
756  * The reading of key and value pairs is controlled by the JLANG::JEquationFacet class.
757  *
758  * \param in input stream
759  * \return input stream
760  */
761  std::istream& read(std::istream& in)
762  {
763  using namespace std;
764 
765  in.imbue(locale(in.getloc(), new JEquationFacet(*this)));
766 
767  for (JEquation equation; in >> equation && read(equation); ) {}
768 
769  return in;
770  }
771 
772 
773  /**
774  * Read from input stream according given format.
775  *
776  * For each key in the format specification,
777  * a corresponding value will be read from the input stream.
778  *
779  * \param in input stream
780  * \param format format
781  * \return input stream
782  */
783  std::istream& read(std::istream& in, const std::string& format)
784  {
785  using namespace std;
786 
787  istringstream is(format);
788 
789  vector<string> buffer;
790 
791  for (string key; is >> key; ) {
792  buffer.push_back(key);
793  }
794 
795  return read(in, buffer.begin(), buffer.end());
796  }
797 
798 
799  /**
800  * Read from input stream according given format.
801  *
802  * For each key in the format specification,
803  * a corresponding value will be read from the input stream.
804  *
805  * \param in input stream
806  * \param __begin begin of format
807  * \param __end end of format
808  * \return input stream
809  */
810  template<class T>
811  std::istream& read(std::istream& in, T __begin, T __end)
812  {
813  using namespace std;
814 
815  for (T i = __begin; i != __end; ++i) {
816 
817  iterator p = find(*i);
818 
819  if (p != end()) {
820 
821  p->second->read(in);
822 
823  } else {
824 
825  WARNING("JProperties::read(): unknown key <" << *i << ">" << endl);
826  }
827  }
828 
829  return in;
830  }
831 
832 
833  /**
834  * Write the current parameter values.
835  *
836  * The output format is
837  *
838  * [<key><sub>]<key><sep><value><eol>
839  * [<key><sub>]<key><sep><value><eol>
840  *
841  * in this, white spaces are omitted.
842  *
843  * \param out output stream
844  * \return output stream
845  */
846  std::ostream& write(std::ostream& out) const
847  {
848  using namespace std;
849 
850  for (const_iterator i = begin(); i != end(); ++i) {
851 
852  char c = ' ';
853 
854  if (i->second->is_properties()) {
855  c = getDefaultDivision ();
856  } else {
857  c = getDefaultSeparator();
858  }
859 
860  i->second->write(out, (i->first + c).c_str(), getDefaultEndOfLine());
861  }
862 
863  out << flush;
864 
865  return out;
866  }
867 
868 
869  /**
870  * Write to output stream according given format.
871  *
872  * For each key in the format specification,
873  * a corresponding value will be written to the output stream.
874  *
875  * \param out output stream
876  * \param format format
877  * \return output stream
878  */
879  std::ostream& write(std::ostream& out, const std::string& format)
880  {
881  using namespace std;
882 
883  istringstream is(format);
884 
885  vector<string> buffer;
886 
887  for (string key; is >> key; ) {
888  buffer.push_back(key);
889  }
890 
891  return write(out, buffer.begin(), buffer.end());
892  }
893 
894 
895  /**
896  * Write to output stream according given format.
897  *
898  * For each key in the format specification,
899  * a corresponding value will be written to the output stream.
900  *
901  * \param out output stream
902  * \param __begin begin of format
903  * \param __end end of format
904  * \return output stream
905  */
906  template<class T>
907  std::ostream& write(std::ostream& out, T __begin, T __end)
908  {
909  using namespace std;
910 
911  for (T i = __begin; i != __end; ++i) {
912 
913  iterator p = find(*i);
914 
915  if (p != end()) {
916 
917  out << getDefaultWhiteSpace();
918 
919  p->second->write(out);
920 
921  } else {
922 
923  WARNING("JProperties::write(): unknown key <" << *i << ">" << endl);
924  }
925  }
926 
927  return out;
928  }
929 
930 
931  /**
932  * Stream editing of input format.
933  *
934  * For each key in the format specification,
935  * a corresponding value will be written to the output stream.
936  *
937  * \param format format
938  * \param prefix prefix key word
939  * \param postfix postfix key word
940  * \return output stream
941  */
942  std::string sed(const std::string& format,
943  const std::string& prefix = "",
944  const std::string& postfix = "")
945  {
946  using namespace std;
947 
948  string buffer = format;
949 
950  for (iterator i = begin(); i != end(); ++i) {
951 
952  string::size_type ipos = 0;
953 
954  while ((ipos = buffer.find(prefix + i->first + postfix, ipos)) != string::npos) {
955 
956  ostringstream out;
957 
958  i->second->write(out);
959 
960  buffer.replace(ipos, prefix.length() + i->first.length() + postfix.length(), out.str());
961  }
962  }
963 
964  return buffer;
965  }
966 
967 
968  /**
969  * Get value.
970  *
971  * \param key key
972  * \return value of this JPropertiesElement
973  */
974  template<class T>
975  const T& getValue(const std::string& key) const
976  {
977  const_iterator i = find(key);
978 
979  if (i != end())
980  return i->second.getValue<T>();
981  else
982  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getValue()");
983  }
984 
985 
986  /**
987  * Get value.
988  *
989  * \param key key
990  * \return value of this JPropertiesElement
991  */
992  template<class T>
993  T& getValue(const std::string& key)
994  {
995  iterator i = find(key);
996 
997  if (i != end())
998  return i->second.getValue<T>();
999  else
1000  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getValue()");
1001  }
1002 
1003 
1004  /**
1005  * Set value.
1006  *
1007  * \param key key
1008  * \param value value
1009  */
1010  template<class T>
1011  void setValue(const std::string& key, const T& value)
1012  {
1013  iterator i = find(key);
1014 
1015  if (i != end())
1016  return i->second.setValue<T>(value);
1017  else
1018  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::setValue()");
1019  }
1020 
1021 
1022  /**
1023  * Get string value.
1024  *
1025  * \param key key
1026  * \return value
1027  */
1029  {
1030  const_iterator i = find(key);
1031 
1032  if (i != end())
1033  return i->second.toString();
1034  else
1035  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getString()");
1036  }
1037 
1038 
1039  /**
1040  * Print the current parameter values.
1041  *
1042  * \param out output stream
1043  * \return output stream
1044  */
1045  std::ostream& print(std::ostream& out) const
1046  {
1047  write(out);
1048 
1049  return out;
1050  }
1051 
1052 
1053  /**
1054  * Stream input.
1055  *
1056  * \param in input stream
1057  * \param properties properties
1058  * \return input stream
1059  */
1060  friend inline std::istream& operator>>(std::istream& in, JProperties& properties)
1061  {
1062  return properties.read(in);
1063  }
1064 
1065 
1066  /**
1067  * Stream output.
1068  *
1069  * \param out output stream
1070  * \param properties properties
1071  * \return output stream
1072  */
1073  friend inline std::ostream& operator<<(std::ostream& out, const JProperties& properties)
1074  {
1075  return properties.write(out);
1076  }
1077  };
1078 
1079 
1080  /**
1081  * Template specialisation for JFileReader.
1082  * This class will not produce ASCII output.
1083  */
1084  template<>
1087  {
1088  public:
1089  /**
1090  * Constructor.
1091  *
1092  * \param value reference of template bject
1093  */
1096  object(value)
1097  {}
1098 
1099 
1100  /**
1101  * Stream input.
1102  *
1103  * \param in input stream
1104  * \return input stream
1105  */
1106  virtual std::istream& read(std::istream& in) override
1107  {
1108  return in >> object;
1109  }
1110 
1111 
1112  /**
1113  * Stream output.
1114  *
1115  * \param out output stream
1116  * \param prefix prefix
1117  * \param postfix postfix
1118  * \return output stream
1119  */
1120  virtual std::ostream& write(std::ostream& out,
1121  const char* prefix,
1122  const char postfix) const override
1123  {
1124  return out;
1125  }
1126 
1127 
1128  /**
1129  * Stream output.
1130  *
1131  * \param out output stream
1132  * \return output stream
1133  */
1134  virtual std::ostream& write(std::ostream& out) const override
1135  {
1136  return out;
1137  }
1138 
1139 
1140  /**
1141  * Equality between property element interfaces.
1142  *
1143  * \param element properties element interface
1144  * \return true if equal; else false
1145  */
1146  virtual bool equals(const JPropertiesElementInterface& element) const override
1147  {
1148  return false;
1149  }
1150 
1151  private:
1153  };
1154 
1155 
1156  /**
1157  * Template specialisation for JProperties.
1158  */
1159  template<>
1162  {
1163  public:
1164  /**
1165  * Constructor.
1166  *
1167  * \param value reference of template bject
1168  */
1171  object(value)
1172  {}
1173 
1174 
1175  /**
1176  * Stream input.
1177  *
1178  * \param in input stream
1179  * \return input stream
1180  */
1181  virtual std::istream& read(std::istream& in) override
1182  {
1183  object.read(in);
1184 
1185  return in;
1186  }
1187 
1188 
1189  /**
1190  * Stream output.
1191  *
1192  * \param out output stream
1193  * \param prefix prefix
1194  * \param postfix postfix
1195  * \return output stream
1196  */
1197  virtual std::ostream& write(std::ostream& out,
1198  const char* prefix,
1199  const char postfix) const override
1200  {
1201  using namespace std;
1202 
1203  for (JProperties::const_iterator i = object.begin(); i != object.end(); ++i) {
1204 
1205  char c = ' ';
1206 
1207  if (i->second->is_properties()) {
1208  c = object.getDefaultDivision ();
1209  } else {
1210  c = object.getDefaultSeparator();
1211  }
1212 
1213  i->second->write(out, (prefix + i->first + c).c_str(), postfix);
1214  }
1215 
1216  out << flush;
1217 
1218  return out;
1219  }
1220 
1221 
1222  /**
1223  * Stream output.
1224  *
1225  * \param out output stream
1226  * \return output stream
1227  */
1228  virtual std::ostream& write(std::ostream& out) const override
1229  {
1230  return writeObject(out, object);
1231  }
1232 
1233 
1234  /**
1235  * Get properties type.
1236  *
1237  * \return true
1238  */
1239  virtual bool is_properties() const override
1240  {
1241  return true;
1242  }
1243 
1244 
1245  private:
1247  };
1248 
1249 
1250  /**
1251  * Get properties of a given object.
1252  *
1253  * This method transfers the making of the property object to the corresponding class
1254  * whilst preserving the constness of the argument.\n
1255  * The corresponding class should implement the method:
1256  * <pre>
1257  * template<bool is_constant>
1258  * static JProperties getProperties(typename JCategory<T, is_constant>::reference_type object,
1259  * const JEquationParameters& equation,
1260  * const int debug)
1261  * </pre>
1262  *
1263  * \param object object
1264  * \param parameters equation parameters
1265  * \param debug debug level
1266  */
1267  template<class T>
1268  inline JProperties& getProperties(T& object,
1270  const int debug = 1)
1271  {
1272  using namespace JPP;
1273 
1274  static JProperties properties;
1275 
1276  properties = T::template getProperties<JClass<T>::is_constant>(object, parameters, debug);
1277 
1278  return properties;
1279  }
1280 }
1281 
1282 
1284 using JLANG::JCategory;
1285 using JEEP::JProperties;
1286 using JEEP::getProperties;
1287 
1288 
1289 /**
1290  * macros to convert (template) parameter to JPropertiesElement object
1291  */
1292 #define gmake_property(A) JProperties::value_type(JProperties::getKey(#A,".>/:"), JEEP::JPropertiesElement(A))
1293 #define zmake_property(A) JProperties::value_type(#A, JEEP::JPropertiesElement(A))
1294 
1295 #endif
Exception for opening of file.
Definition: JException.hh:358
virtual std::ostream & write(std::ostream &out, const char *prefix, const char postfix) const override
Stream output.
const std::string & getKey() const
Get key.
Definition: JEquation.hh:163
JProperties(const JEquationParameters &parameters, const int debug=0)
Constructor.
Definition: JProperties.hh:635
Exception for reading of file.
Definition: JException.hh:394
bool read(const JEquation &equation)
Read equation.
Definition: JProperties.hh:678
#define WARNING(A)
Definition: JMessage.hh:65
Exceptions.
std::istream & read(std::istream &in, const std::string &format)
Read from input stream according given format.
Definition: JProperties.hh:783
const std::string & getValue() const
Get value.
Definition: JEquation.hh:185
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
virtual std::ostream & write(std::ostream &out, const char *prefix, const char postfix) const override
Stream output.
Definition: JProperties.hh:258
JEquationParameters & join(const JEquationParameters &value)
Join equation parameters.
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition: JProperties.hh:174
void put(const std::string &key, T &object)
Put object at given key.
Definition: JProperties.hh:651
bool equals(const JPropertiesElement &element) const
Equality between property elements.
Definition: JProperties.hh:324
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JProperties.hh:134
Interface for ASCII input using standard streams.
Definition: JAbstractIO.hh:21
virtual std::istream & read(std::istream &in) override
Stream input.
JProperties & getProperties(T &object, const JEquationParameters &parameters=JEquationParameters(), const int debug=1)
Get properties of a given object.
std::map< std::string, JPropertiesElement > JMap_t
Definition: JProperties.hh:504
Exception when parsing a value.
Definition: JException.hh:574
Template specialisation of JPropertiesTemplateElement for const data type.
Definition: JProperties.hh:208
friend std::istream & operator>>(std::istream &in, JProperties::JFileReader &fileReader)
Stream input.
Definition: JProperties.hh:563
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
virtual std::ostream & write(std::ostream &out) const override
Stream output.
virtual std::ostream & write(std::ostream &out, const char *prefix, const char postfix) const override
Stream output.
Definition: JProperties.hh:160
virtual bool equals(const JPropertiesElementInterface &element) const
Equality between property element interfaces.
Definition: JProperties.hh:94
Utility class to parse parameter values.
Definition: JProperties.hh:497
friend std::istream & operator>>(std::istream &in, JProperties &properties)
Stream input.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
virtual std::istream & read(std::istream &in) override
Stream input.
void setEndMarker(const bool marker)
Set end marker.
Definition: JProperties.hh:458
Simple data structure to support I/O of equations (see class JLANG::JEquation).
virtual std::istream & read(std::istream &in) override
Stream input.
Definition: JProperties.hh:232
void setValue(const T &value)
Set value of this JPropertiesElement.
Definition: JProperties.hh:386
Auxiliary class to handle input from file.
Definition: JProperties.hh:535
is
Definition: JDAQCHSM.chsm:167
const T & getValue() const
Get value.
Definition: JProperties.hh:351
JPropertiesTemplateElement(const JProperties &value)
Constructor.
virtual std::ostream & write(std::ostream &out) const override
Stream output.
Definition: JProperties.hh:244
std::istream & readObject(std::istream &in, T &object)
Stream input of object.
bool read(const std::string &buffer)
Read from input string.
Definition: JProperties.hh:739
JPropertiesTemplateElement(const T &value)
Constructor.
Definition: JProperties.hh:220
JPropertiesTemplateElement(T &value)
Constructor.
Definition: JProperties.hh:122
General purpose equation class.
Definition: JEquation.hh:47
virtual std::ostream & write(std::ostream &out, const char *prefix, const char postfix) const override
Stream output.
const T & getValue(const std::string &key) const
Get value.
Definition: JProperties.hh:975
std::string sed(const std::string &format, const std::string &prefix="", const std::string &postfix="")
Stream editing of input format.
Definition: JProperties.hh:942
std::ostream & print(std::ostream &out) const
Print the current parameter values.
The template JSharedPointer class can be used to share a pointer to an object.
JFileReader(JProperties &__properties)
Definition: JProperties.hh:537
std::ostream & write(std::ostream &out) const
Write the current parameter values.
Definition: JProperties.hh:846
std::string getString(const std::string &key) const
Get string value.
JProperties(const int debug=0)
Constructor.
Definition: JProperties.hh:621
bool getEndMarker() const
Get end marker.
Definition: JProperties.hh:447
virtual bool is_properties() const
Get properties type.
Definition: JProperties.hh:82
JPropertiesElement(T &value)
Constructor.
Definition: JProperties.hh:311
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JPropertiesElement & operator=(T &value)
Assignment operator.
Definition: JProperties.hh:337
virtual bool equals(const JPropertiesElementInterface &element) const override
Equality between property element interfaces.
Definition: JProperties.hh:272
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
JProperties & join(const JProperties &properties)
Join properties objects.
Definition: JProperties.hh:662
Template class for I/O of properties element.
Definition: JProperties.hh:110
std::istream & read(std::istream &in)
Read from input stream.
Definition: JProperties.hh:761
virtual bool equals(const JPropertiesElementInterface &element) const override
Equality between property element interfaces.
Definition: JProperties.hh:186
JFileReader & operator=(const std::string &file_name)
Assignment operator.
Definition: JProperties.hh:548
virtual bool is_properties() const override
Get properties type.
bool compareObjects(const T &first, const T &second, JBool< true >)
Comparison of comparable objects.
General purpose messaging.
T & getValue(const std::string &key)
Get value.
Definition: JProperties.hh:993
void setValue(const std::string &key, const T &value)
Set value.
bool fail(std::istream &in)
Check for stream state.
Definition: JParser.hh:113
virtual std::ostream & write(std::ostream &out) const =0
Stream output.
friend std::ostream & operator<<(std::ostream &out, const JProperties::JFileReader &fileReader)
Stream output.
Definition: JProperties.hh:582
std::string toString() const
Convert to string.
Definition: JProperties.hh:402
$WORKDIR ev_configure_dqsimulator txt echo process $DQ_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DQ_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
void reset(T &value)
Reset value.
The property value class.
Definition: JProperties.hh:293
Interface for ASCII output using standard streams.
Definition: JAbstractIO.hh:56
JPropertiesTemplateElement(JProperties::JFileReader &value)
Constructor.
friend std::ostream & operator<<(std::ostream &out, const JProperties &properties)
Stream output.
T & getValue()
Get value.
Definition: JProperties.hh:368
std::ostream & write(std::ostream &out, const std::string &format)
Write to output stream according given format.
Definition: JProperties.hh:879
T toValue() const
Convert to template type.
Definition: JProperties.hh:418
virtual std::ostream & write(std::ostream &out, const char *prefix, const char postfix) const =0
Stream output.
std::ostream & write(std::ostream &out, T __begin, T __end)
Write to output stream according given format.
Definition: JProperties.hh:907
const char getSeparator() const
Get separator.
Definition: JEquation.hh:174
static std::string getKey(const std::string &buffer, const std::string &sep)
Utility method to strip off all leading characters from a string until specified character(s).
Definition: JProperties.hh:516
JPropertiesElement()
Default constructor.
Definition: JProperties.hh:300
virtual std::ostream & write(std::ostream &out) const override
Stream output.
std::istream & read(std::istream &in, T __begin, T __end)
Read from input stream according given format.
Definition: JProperties.hh:811
void read(const std::string &file_name)
Read properties from file.
Definition: JProperties.hh:594
Interface for I/O of properties element.
Definition: JProperties.hh:66
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Interface for ASCII output with prefix and postfix using standard streams.
Definition: JAbstractIO.hh:91
virtual JClass_t * get() const override
Get pointer.
Definition: JPointer.hh:64
Auxiliary class to define value, reference and pointer types for given data type and category...
Definition: JCategory.hh:18
virtual bool equals(const JPropertiesElementInterface &element) const override
Equality between property element interfaces.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Auxiliary class for handling debug parameter within a class.
Definition: JMessage.hh:44
std::ostream & writeObject(std::ostream &out, const T &object)
Stream output of object.