pstore2
common.hpp
1 //===- include/pstore/serialize/common.hpp ----------------*- mode: C++ -*-===//
2 //* *
3 //* ___ ___ _ __ ___ _ __ ___ ___ _ __ *
4 //* / __/ _ \| '_ ` _ \| '_ ` _ \ / _ \| '_ \ *
5 //* | (_| (_) | | | | | | | | | | | (_) | | | | *
6 //* \___\___/|_| |_| |_|_| |_| |_|\___/|_| |_| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef PSTORE_SERIALIZE_COMMON_HPP
18 #define PSTORE_SERIALIZE_COMMON_HPP
19 
20 namespace pstore {
21  namespace serialize {
22 
44 
45  template <typename T>
46  class sticky_assign {
47  public:
48  sticky_assign () = default;
51  : t_ (rhs.get ())
52  , is_first_{false} {}
53 
56  template <typename Other>
57  explicit sticky_assign (Other const & t)
58  : t_ (t)
59  , is_first_{false} {}
60  sticky_assign (sticky_assign &&) = delete;
61 
62  ~sticky_assign () = default;
63 
67  template <typename Other>
68  sticky_assign & operator= (Other const & rhs) {
69  if (is_first_) {
70  t_ = rhs;
71  is_first_ = false;
72  }
73  return *this;
74  }
75  sticky_assign & operator= (sticky_assign const &) = delete;
76  sticky_assign & operator= (sticky_assign &&) = delete;
77 
78  T const & get () const { return t_; }
79 
80  private:
81  T t_{};
82  bool is_first_ = true;
83  };
84 
85  } // namespace serialize
86 } // namespace pstore
87 #endif // PSTORE_SERIALIZE_COMMON_HPP
A helper class which remembers the first time that it is assigned to.
Definition: common.hpp:46
sticky_assign & operator=(Other const &rhs)
Assign from a type that is implicitly convertible to T.
Definition: common.hpp:68
sticky_assign(Other const &t)
Construct from a type that is implicitly convertible to T.
Definition: common.hpp:57
Definition: nonpod2.cpp:40
sticky_assign(sticky_assign< T > const &rhs)
Construct from a sticky_assign<T>. This is considered equivalent to assignment.
Definition: common.hpp:50