TECA
The Toolkit for Extreme Climate Analysis
teca_program_options.h
Go to the documentation of this file.
1 #ifndef teca_program_options_h
2 #define teca_program_options_h
3 
4 /// @file
5 
6 #if defined(TECA_HAS_BOOST) && !defined(SWIG)
7 #include "teca_config.h"
8 #include "teca_common.h"
9 #include "teca_mpi_util.h"
10 
11 #include <iostream>
12 #include <boost/array.hpp>
13 
14 namespace boost
15 {
16  namespace program_options
17  {
18  class options_description;
19  class variables_map;
20  }
21 };
22 
23 using options_description = boost::program_options::options_description;
24 using variables_map = boost::program_options::variables_map;
25 
26 // the following allows std::array to work with boost::program_options
27 namespace std
28 {
29 template <typename T, size_t N>
30 std::istream& operator>>(std::istream& str, std::array<T,N>& arr)
31 {
32  for (size_t i = 0; i < N; ++i)
33  str >> std::skipws >> arr[i];
34  return str;
35 }
36 }
37 namespace boost
38 {
39 template <typename T, size_t N>
40 std::istream& operator>>(std::istream& str, boost::array<T,N>& arr)
41 {
42  for (size_t i = 0; i < N; ++i)
43  str >> std::skipws >> arr[i];
44  return str;
45 }
46 template <typename T, size_t N>
47 std::ostream& operator<<(std::ostream& str, const boost::array<T,N>& arr)
48 {
49  str << arr[0];
50  for (size_t i = 1; i < N; ++i)
51  str << " " << arr[i];
52  return str;
53 }
54 }
55 
56 /// initialize the given options description with algorithm's properties
57 #define TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION() \
58  \
59  /** Adds the class algorithm properties to the description object */ \
60  void get_properties_description(const std::string &prefix, \
61  boost::program_options::options_description &opts) override; \
62 
63 /// initialize the algorithm from the given options variable map.
64 #define TECA_SET_ALGORITHM_PROPERTIES() \
65  /** Sets the class algorithm properties from the map object */ \
66  void set_properties(const std::string &prefix, \
67  boost::program_options::variables_map &opts) override; \
68 
69 // helpers for implementation dealing with Boost
70 // program options. NOTE: because the above declarations
71 // are intented to be included in class header files
72 // we are intentionally not including <string> and
73 // <boost/program_options.hpp>. These need to be
74 // included in your cxx files.
75 //
76 #define TECA_POPTS_GET(_type, _prefix, _name, _desc) \
77  (((_prefix.empty()?"":_prefix+"::") + #_name).c_str(), \
78  boost::program_options::value<_type>()->default_value \
79  (this->get_ ## _name()), "\n" _desc "\n")
80 
81 #define TECA_POPTS_MULTI_GET(_type, _prefix, _name, _desc) \
82  (((_prefix.empty()?"":_prefix+"::") + #_name).c_str(), \
83  boost::program_options::value<_type>()->multitoken \
84  ()->default_value(this->get_ ## _name()), \
85  "\n" _desc "\n")
86 
87 #define TECA_POPTS_SET(_opts, _type, _prefix, _name) \
88  {std::string opt_name = \
89  (_prefix.empty()?"":_prefix+"::") + #_name; \
90  bool defd = _opts[opt_name].defaulted(); \
91  if (!defd) \
92  { \
93  _type val = _opts[opt_name].as<_type>(); \
94  if (this->verbose && \
95  teca_mpi_util::mpi_rank_0(this->get_communicator())) \
96  { \
97  TECA_STATUS("Setting " << opt_name << " = " << val) \
98  } \
99  this->set_##_name(val); \
100  }}
101 
102 #else
103 #define TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
104 #define TECA_SET_ALGORITHM_PROPERTIES()
105 #endif
106 #endif
TECA_EXPORT std::ostream & operator<<(std::ostream &os, const teca_calendar_util::time_point &tpt)
send the time_point to a stream in humnan readable form