Jpp  18.5.2
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  * \return properties
662  */
663  JProperties& join(const JProperties& properties)
664  {
665  JEquationParameters::join(properties);
666 
667  insert(properties.begin(), properties.end());
668 
669  return *this;
670  }
671 
672 
673  /**
674  * Read equation.
675  *
676  * \param equation equation
677  * \return status
678  */
679  bool read(const JEquation& equation)
680  {
681  using namespace std;
682 
683  iterator p = find(equation.getKey());
684 
685  DEBUG("Processing key: " << equation.getKey() << ' ' << (p != end()) << endl);
686 
687  if (p != end()) {
688 
689  istringstream is(equation.getValue());
690 
691  if (isDivision(equation.getSeparator())) {
692 
693  if (p->second->is_properties()) {
694 
695  p->second->read(is);
696 
697  } else {
698 
699  ERROR("JProperties::read(): no properties object after division <" << equation.getKey() << ">" << endl);
700  }
701 
702  } else if (isSeparator(equation.getSeparator())) {
703 
704  try {
705  p->second->read(is);
706  }
707  catch(const exception& error) {
708  ERROR("JProperties::read(): read error at key <" << equation.getKey() << "> " << error.what() << endl);
709  }
710 
711  } else {
712 
713  ERROR("JProperties::read(): illegal character following key <" << equation.getKey() << "> " << equation.getSeparator() << endl);
714  }
715 
716  if (p->second.getEndMarker()) {
717  return false;
718  }
719 
720  if (fail(is)) {
721 
722  ERROR("JProperties::read(): error reading data for key <" << equation.getKey() << "> " << equation.getValue() << endl);
723  }
724 
725  } else {
726 
727  WARNING("JProperties::read(): unknown key <" << equation.getKey() << ">" << endl);
728  }
729 
730  return true;
731  }
732 
733 
734  /**
735  * Read from input string.
736  *
737  * \param buffer input string
738  * \return read status
739  */
740  bool read(const std::string& buffer)
741  {
742  std::istringstream in(buffer);
743 
744  return !fail(read(in));
745  }
746 
747 
748  /**
749  * Read from input stream.
750  *
751  * The input format is:
752  * <pre>
753  * [<key><sub>]<key><sep><value><eol>
754  * [<key><sub>]<key><sep><value><eol>
755  * </pre>
756  * In this, white spaces are ignored.
757  * The reading of key and value pairs is controlled by the JLANG::JEquationFacet class.
758  *
759  * \param in input stream
760  * \return input stream
761  */
762  std::istream& read(std::istream& in)
763  {
764  using namespace std;
765 
766  in.imbue(locale(in.getloc(), new JEquationFacet(*this)));
767 
768  for (JEquation equation; in >> equation && read(equation); ) {}
769 
770  return in;
771  }
772 
773 
774  /**
775  * Read from input stream according given format.
776  *
777  * For each key in the format specification,
778  * a corresponding value will be read from the input stream.
779  *
780  * \param in input stream
781  * \param format format
782  * \return input stream
783  */
784  std::istream& read(std::istream& in, const std::string& format)
785  {
786  using namespace std;
787 
788  istringstream is(format);
789 
790  vector<string> buffer;
791 
792  for (string key; is >> key; ) {
793  buffer.push_back(key);
794  }
795 
796  return read(in, buffer.begin(), buffer.end());
797  }
798 
799 
800  /**
801  * Read from input stream according given format.
802  *
803  * For each key in the format specification,
804  * a corresponding value will be read from the input stream.
805  *
806  * \param in input stream
807  * \param __begin begin of format
808  * \param __end end of format
809  * \return input stream
810  */
811  template<class T>
812  std::istream& read(std::istream& in, T __begin, T __end)
813  {
814  using namespace std;
815 
816  for (T i = __begin; i != __end; ++i) {
817 
818  iterator p = find(*i);
819 
820  if (p != end()) {
821 
822  p->second->read(in);
823 
824  } else {
825 
826  WARNING("JProperties::read(): unknown key <" << *i << ">" << endl);
827  }
828  }
829 
830  return in;
831  }
832 
833 
834  /**
835  * Write the current parameter values.
836  *
837  * The output format is
838  *
839  * [<key><sub>]<key><sep><value><eol>
840  * [<key><sub>]<key><sep><value><eol>
841  *
842  * in this, white spaces are omitted.
843  *
844  * \param out output stream
845  * \return output stream
846  */
847  std::ostream& write(std::ostream& out) const
848  {
849  using namespace std;
850 
851  for (const_iterator i = begin(); i != end(); ++i) {
852 
853  char c = ' ';
854 
855  if (i->second->is_properties()) {
856  c = getDefaultDivision ();
857  } else {
858  c = getDefaultSeparator();
859  }
860 
861  i->second->write(out, (i->first + c).c_str(), getDefaultEndOfLine());
862  }
863 
864  out << flush;
865 
866  return out;
867  }
868 
869 
870  /**
871  * Write to output stream according given format.
872  *
873  * For each key in the format specification,
874  * a corresponding value will be written to the output stream.
875  *
876  * \param out output stream
877  * \param format format
878  * \return output stream
879  */
880  std::ostream& write(std::ostream& out, const std::string& format)
881  {
882  using namespace std;
883 
884  istringstream is(format);
885 
886  vector<string> buffer;
887 
888  for (string key; is >> key; ) {
889  buffer.push_back(key);
890  }
891 
892  return write(out, buffer.begin(), buffer.end());
893  }
894 
895 
896  /**
897  * Write to output stream according given format.
898  *
899  * For each key in the format specification,
900  * a corresponding value will be written to the output stream.
901  *
902  * \param out output stream
903  * \param __begin begin of format
904  * \param __end end of format
905  * \return output stream
906  */
907  template<class T>
908  std::ostream& write(std::ostream& out, T __begin, T __end)
909  {
910  using namespace std;
911 
912  for (T i = __begin; i != __end; ++i) {
913 
914  iterator p = find(*i);
915 
916  if (p != end()) {
917 
918  out << getDefaultWhiteSpace();
919 
920  p->second->write(out);
921 
922  } else {
923 
924  WARNING("JProperties::write(): unknown key <" << *i << ">" << endl);
925  }
926  }
927 
928  return out;
929  }
930 
931 
932  /**
933  * Stream editing of input format.
934  *
935  * For each key in the format specification,
936  * a corresponding value will be written to the output stream.
937  *
938  * \param format format
939  * \param prefix prefix key word
940  * \param postfix postfix key word
941  * \return output stream
942  */
943  std::string sed(const std::string& format,
944  const std::string& prefix = "",
945  const std::string& postfix = "")
946  {
947  using namespace std;
948 
949  string buffer = format;
950 
951  for (iterator i = begin(); i != end(); ++i) {
952 
953  string::size_type ipos = 0;
954 
955  while ((ipos = buffer.find(prefix + i->first + postfix, ipos)) != string::npos) {
956 
957  ostringstream out;
958 
959  i->second->write(out);
960 
961  buffer.replace(ipos, prefix.length() + i->first.length() + postfix.length(), out.str());
962  }
963  }
964 
965  return buffer;
966  }
967 
968 
969  /**
970  * Get value.
971  *
972  * \param key key
973  * \return value of this JPropertiesElement
974  */
975  template<class T>
976  const T& getValue(const std::string& key) const
977  {
978  const_iterator i = find(key);
979 
980  if (i != end())
981  return i->second.getValue<T>();
982  else
983  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getValue()");
984  }
985 
986 
987  /**
988  * Get value.
989  *
990  * \param key key
991  * \return value of this JPropertiesElement
992  */
993  template<class T>
994  T& getValue(const std::string& key)
995  {
996  iterator i = find(key);
997 
998  if (i != end())
999  return i->second.getValue<T>();
1000  else
1001  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getValue()");
1002  }
1003 
1004 
1005  /**
1006  * Set value.
1007  *
1008  * \param key key
1009  * \param value value
1010  */
1011  template<class T>
1012  void setValue(const std::string& key, const T& value)
1013  {
1014  iterator i = find(key);
1015 
1016  if (i != end())
1017  return i->second.setValue<T>(value);
1018  else
1019  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::setValue()");
1020  }
1021 
1022 
1023  /**
1024  * Get string value.
1025  *
1026  * \param key key
1027  * \return value
1028  */
1030  {
1031  const_iterator i = find(key);
1032 
1033  if (i != end())
1034  return i->second.toString();
1035  else
1036  THROW(JPropertiesException, "Key <" << key << "> not found at JPropertiesElement::getString()");
1037  }
1038 
1039 
1040  /**
1041  * Print the current parameter values.
1042  *
1043  * \param out output stream
1044  * \return output stream
1045  */
1046  std::ostream& print(std::ostream& out) const
1047  {
1048  write(out);
1049 
1050  return out;
1051  }
1052 
1053 
1054  /**
1055  * Stream input.
1056  *
1057  * \param in input stream
1058  * \param properties properties
1059  * \return input stream
1060  */
1061  friend inline std::istream& operator>>(std::istream& in, JProperties& properties)
1062  {
1063  return properties.read(in);
1064  }
1065 
1066 
1067  /**
1068  * Stream output.
1069  *
1070  * \param out output stream
1071  * \param properties properties
1072  * \return output stream
1073  */
1074  friend inline std::ostream& operator<<(std::ostream& out, const JProperties& properties)
1075  {
1076  return properties.write(out);
1077  }
1078  };
1079 
1080 
1081  /**
1082  * Template specialisation for JFileReader.
1083  * This class will not produce ASCII output.
1084  */
1085  template<>
1088  {
1089  public:
1090  /**
1091  * Constructor.
1092  *
1093  * \param value reference of template bject
1094  */
1097  object(value)
1098  {}
1099 
1100 
1101  /**
1102  * Stream input.
1103  *
1104  * \param in input stream
1105  * \return input stream
1106  */
1107  virtual std::istream& read(std::istream& in) override
1108  {
1109  return in >> object;
1110  }
1111 
1112 
1113  /**
1114  * Stream output.
1115  *
1116  * \param out output stream
1117  * \param prefix prefix
1118  * \param postfix postfix
1119  * \return output stream
1120  */
1121  virtual std::ostream& write(std::ostream& out,
1122  const char* prefix,
1123  const char postfix) const override
1124  {
1125  return out;
1126  }
1127 
1128 
1129  /**
1130  * Stream output.
1131  *
1132  * \param out output stream
1133  * \return output stream
1134  */
1135  virtual std::ostream& write(std::ostream& out) const override
1136  {
1137  return out;
1138  }
1139 
1140 
1141  /**
1142  * Equality between property element interfaces.
1143  *
1144  * \param element properties element interface
1145  * \return true if equal; else false
1146  */
1147  virtual bool equals(const JPropertiesElementInterface& element) const override
1148  {
1149  return false;
1150  }
1151 
1152  private:
1154  };
1155 
1156 
1157  /**
1158  * Template specialisation for JProperties.
1159  */
1160  template<>
1163  {
1164  public:
1165  /**
1166  * Constructor.
1167  *
1168  * \param value reference of template bject
1169  */
1172  object(value)
1173  {}
1174 
1175 
1176  /**
1177  * Stream input.
1178  *
1179  * \param in input stream
1180  * \return input stream
1181  */
1182  virtual std::istream& read(std::istream& in) override
1183  {
1184  object.read(in);
1185 
1186  return in;
1187  }
1188 
1189 
1190  /**
1191  * Stream output.
1192  *
1193  * \param out output stream
1194  * \param prefix prefix
1195  * \param postfix postfix
1196  * \return output stream
1197  */
1198  virtual std::ostream& write(std::ostream& out,
1199  const char* prefix,
1200  const char postfix) const override
1201  {
1202  using namespace std;
1203 
1204  for (JProperties::const_iterator i = object.begin(); i != object.end(); ++i) {
1205 
1206  char c = ' ';
1207 
1208  if (i->second->is_properties()) {
1209  c = object.getDefaultDivision ();
1210  } else {
1211  c = object.getDefaultSeparator();
1212  }
1213 
1214  i->second->write(out, (prefix + i->first + c).c_str(), postfix);
1215  }
1216 
1217  out << flush;
1218 
1219  return out;
1220  }
1221 
1222 
1223  /**
1224  * Stream output.
1225  *
1226  * \param out output stream
1227  * \return output stream
1228  */
1229  virtual std::ostream& write(std::ostream& out) const override
1230  {
1231  return writeObject(out, object);
1232  }
1233 
1234 
1235  /**
1236  * Get properties type.
1237  *
1238  * \return true
1239  */
1240  virtual bool is_properties() const override
1241  {
1242  return true;
1243  }
1244 
1245 
1246  private:
1248  };
1249 
1250 
1251  /**
1252  * Get properties of a given object.
1253  *
1254  * This method transfers the making of the property object to the corresponding class
1255  * whilst preserving the constness of the argument.\n
1256  * The corresponding class should implement the method:
1257  * <pre>
1258  * template<bool is_constant>
1259  * static JProperties getProperties(typename JCategory<T, is_constant>::reference_type object,
1260  * const JEquationParameters& equation,
1261  * const int debug)
1262  * </pre>
1263  *
1264  * \param object object
1265  * \param parameters equation parameters
1266  * \param debug debug level
1267  */
1268  template<class T>
1269  inline JProperties& getProperties(T& object,
1271  const int debug = 1)
1272  {
1273  using namespace JPP;
1274 
1275  static JProperties properties;
1276 
1277  properties = T::template getProperties<JClass<T>::is_constant>(object, parameters, debug);
1278 
1279  return properties;
1280  }
1281 }
1282 
1283 
1285 using JLANG::JCategory;
1286 using JEEP::JProperties;
1287 using JEEP::getProperties;
1288 
1289 
1290 /**
1291  * macros to convert (template) parameter to JPropertiesElement object
1292  */
1293 #define gmake_property(A) JProperties::value_type(JProperties::getKey(#A,".>/:"), JEEP::JPropertiesElement(A))
1294 #define zmake_property(A) JProperties::value_type(#A, JEEP::JPropertiesElement(A))
1295 
1296 #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:679
#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:784
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:740
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:976
std::string sed(const std::string &format, const std::string &prefix="", const std::string &postfix="")
Stream editing of input format.
Definition: JProperties.hh:943
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:847
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:663
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:762
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:994
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:880
T toValue() const
Convert to template type.
Definition: JProperties.hh:418
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:48
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:908
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:812
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
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.