extended_value.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_extended_value_h
2 #define fhiclcpp_extended_value_h
3 
4 // ======================================================================
5 //
6 // extended_value
7 //
8 // ======================================================================
9 
10 #include "fhiclcpp/Protection.h"
11 #include "fhiclcpp/fwd.h"
12 #include "fhiclcpp/stdmap_shims.h"
13 
14 #include <any>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 // ----------------------------------------------------------------------
20 
21 namespace fhicl {
22  enum value_tag {
24  NIL,
32  };
33 }
34 
35 // ----------------------------------------------------------------------
36 
38 public:
40  using complex_t = std::pair<std::string, std::string>;
41  using sequence_t = std::vector<extended_value>;
43 
44  extended_value() = default;
45 
47  value_tag const tag,
48  std::any const value,
49  Protection const protection,
50  std::string src = {})
51  : in_prolog{in_prolog}
52  , tag{tag}
53  , value{value}
54  , src_info{move(src)}
55  , protection{protection}
56  {}
57 
58  extended_value(bool const in_prolog,
59  value_tag const tag,
60  std::any const value,
61  std::string src = {})
62  : in_prolog{in_prolog}, tag{tag}, value{value}, src_info{move(src)}
63  {}
64 
65  bool
66  is_a(value_tag const t) const noexcept
67  {
68  return t == tag;
69  }
70 
71  std::string to_string() const;
72 
73  void set_prolog(bool new_prolog_state);
74 
75  void
77  {
78  src_info = src;
79  }
80 
81  void
83  {
84  // See notes below.
85  protection = Protection::NONE;
86  }
87 
89 
90  operator atom_t() const { return std::any_cast<atom_t>(value); }
91  operator complex_t() const { return std::any_cast<complex_t>(value); }
92  operator sequence_t() const { return std::any_cast<sequence_t>(value); }
93  operator table_t() const { return std::any_cast<table_t>(value); }
94 
95  bool in_prolog{false};
97  std::any value{};
99 
100  // Protection corresponds to the binding of a name to a value, and
101  // not the value per se. The protection data member is thus
102  // separate from the other data of this class but retained here for
103  // navigational convenience. Care must therefore be taken when
104  // implementing '@local::' (e.g.):
105  //
106  // x @protect_ignore: 14 // -> protection for x is PROTECT_IGNORE
107  // y: @local::x // -> protection for y should be NONE
108  //
109  // The reset_protection() member function is called while parsing y
110  // to ensure that x's protection does not propagate to y.
112 
113 }; // extended_value
114 
115 // ======================================================================
116 
117 #endif /* fhiclcpp_extended_value_h */
118 
119 // Local Variables:
120 // mode: c++
121 // End:
bool is_a(value_tag const t) const noexcept
std::string string
Definition: nybbler.cc:12
extended_value(bool const in_prolog, value_tag const tag, std::any const value, Protection const protection, std::string src={})
std::pair< std::string, std::string > complex_t
Protection
Definition: Protection.h:7
void set_src_info(std::string const &src)
def move(depos, offset)
Definition: depos.py:107
std::vector< extended_value > sequence_t
shims::map< std::string, extended_value > table_t
extended_value(bool const in_prolog, value_tag const tag, std::any const value, std::string src={})
std::string to_string() const
std::string pretty_src_info() const
void set_prolog(bool new_prolog_state)