xdrpp
RFC4506 XDR compiler and message library
clear.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /** \file clear.h Support for clearing an XDR data structure. */
4 
5 #ifndef _XDRPP_CLEAR_H_HEADER_INCLUDED_
6 #define _XDRPP_CLEAR_H_HEADER_INCLUDED_ 1
7 
8 #include <cstring>
9 #include <type_traits>
10 #include <xdrpp/types.h>
11 
12 namespace xdr {
13 
14 namespace detail{
15 //! Helper type for xdr::xdr_clear function.
16 struct xdr_clear_t {
17  Constexpr xdr_clear_t() {}
18 
19  template<typename T> typename
20  std::enable_if<xdr_traits<T>::variable_nelem>::type
21  operator()(T &t) const { t.resize(0); }
22 
23  template<std::uint32_t N> void operator()(opaque_array<N> &t) const {
24  std::memset(t.data(), 0, t.size());
25  }
26 
27  template<typename T> typename
28  std::enable_if<xdr_traits<T>::is_struct>::type
29  operator()(T &t) const { xdr_traits<T>::load(*this, t); }
30 
31  template<typename T> typename
32  std::enable_if<!xdr_traits<T>::variable_nelem>::type
33  operator()(T &t) const { xdr_traits<T>::load(*this, t); }
34 
35  template<typename T> typename std::enable_if<xdr_traits<T>::is_union>::type
36  operator()(T &t) const {
37  auto dt = typename xdr_traits<T>::discriminant_type{};
38  if (t._xdr_field_number(dt) > 0)
39  xdr_traits<T>::load(*this, t);
40  else
41  t._xdr_discriminant(dt, false);
42  }
43 
44  template<typename T> typename
45  std::enable_if<xdr_traits<T>::is_numeric || xdr_traits<T>::is_enum>::type
46  operator()(T &t) const { t = T{}; }
47 };
48 }
49 
50 //! Reset XDR data structure to its default contents. All vectors and
51 //! strings are set to length 0, all fixed-size opaque arrays zeroed
52 //! out, and all numeric and enum types sent to their default values
53 //! (generally 0).
54 template<typename T> void
55 xdr_clear(T &t)
56 {
57  static Constexpr detail::xdr_clear_t c;
58  archive(c, t);
59 }
60 
61 }
62 
63 #endif // !_XDRPP_CLEAR_H_HEADER_INCLUDED
void xdr_clear(T &t)
Reset XDR data structure to its default contents.
Definition: clear.h:55
Type definitions for xdrc compiler output.
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
static Constexpr const bool is_enum
T is an XDR enum or bool (traits have enum_name).
Definition: types.h:160