Spicy
parsed-unit.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <cassert>
6 #include <string>
7 
8 #include <hilti/rt/type-info.h>
9 #include <hilti/rt/types/reference.h>
10 
11 namespace spicy::rt {
12 
21 public:
23  template<typename T>
24  const T& get() const {
25  if ( auto p = _unit.as<T>() )
26  return *p;
27  else
28  throw hilti::rt::NullReference("parsed unit not set");
29  }
30 
36  if ( ! _ptr )
37  throw hilti::rt::NullReference("parsed unit not set");
38 
39  assert(_ti);
40  return {_ptr, _ti, *this};
41  }
42 
44  void reset() {
45  _unit.reset();
46  _ptr = nullptr;
47  _ti = nullptr;
48  }
49 
59  template<typename T>
61  assert(ti);
62 
63  u._unit = hilti::rt::StrongReference(t);
64  u._ptr = t.get();
65  u._ti = ti;
66  u.tie(u._unit);
67  }
68 
69 private:
71  const hilti::rt::TypeInfo* _ti = nullptr;
72  const void* _ptr = nullptr;
73 };
74 
75 } // namespace spicy::rt
76 
77 namespace hilti::rt::detail::adl {
78 inline std::string to_string(const ::spicy::rt::ParsedUnit& u, adl::tag /*unused*/) { return "<parsed unit>"; };
79 } // namespace hilti::rt::detail::adl
80 
81 namespace spicy::rt {
82 inline std::ostream& operator<<(std::ostream& out, const ParsedUnit& u) { return out << hilti::rt::to_string(u); }
83 } // namespace spicy::rt
std::string to_string(T &&x)
Definition: extension-points.h:26
Definition: parsed-unit.h:20
Definition: reference.h:633
static void initialize(ParsedUnit &u, const hilti::rt::ValueReference< T > &t, const hilti::rt::TypeInfo *ti)
Definition: parsed-unit.h:60
Definition: type-info.h:43
const T * get() const
Definition: reference.h:106
hilti::rt::type_info::Value value() const
Definition: parsed-unit.h:35
T * as() const
Definition: reference.h:647
Definition: reference.h:321
void reset()
Definition: reference.h:659
Definition: type-info.h:82
Definition: reference.h:47
Definition: deferred-expression.h:41
void reset()
Definition: parsed-unit.h:44
Definition: type-info.h:1146
void tie(hilti::rt::StrongReferenceGeneric value)
Definition: type-info.h:53