summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/xalloc.hxx
blob: 8ae692a6245bf914d1a286725025806b5360c869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 ** \file misc/xalloc.hxx
 ** \brief Implementation for misc/xalloc.hh.
 */

#pragma once

#include <utility>

#include <misc/xalloc.hh>

namespace misc
{
  /*----------------.
  | iomanipulator.  |
  `----------------*/

  inline std::ostream& operator<<(std::ostream& o, const iomanipulator& g)
  {
    g(o);
    return o;
  }

  /*---------------------.
  | xalloc<StoredType>.  |
  `---------------------*/

  template <class StoredType>
  template <typename... Args>
  xalloc<StoredType>::xalloc(Args&&... args)
    : index_(std::ios::xalloc())
    , model_{std::forward<Args>(args)...}
  {}

  template <class StoredType> long int xalloc<StoredType>::index() const
  {
    return index_;
  }

  template <class StoredType>
  StoredType& xalloc<StoredType>::operator()(std::ostream& ostr) const
  {
    void*& storage_ptr = ostr.pword(index_);

    if (!storage_ptr)
      {
        storage_ptr = new StoredType{model_};
        ostr.register_callback(xalloc::deallocate, index_);
      }

    return *static_cast<StoredType*>(storage_ptr);
  }

  template <class StoredType>
  void xalloc<StoredType>::deallocate(std::ios_base::event type,
                                      std::ios_base& ios,
                                      int index)
  {
    if (type == std::ios_base::erase_event)
      {
        StoredType* ptr = static_cast<StoredType*>(ios.pword(index));
        delete ptr;
      }
  }

  /*-----------.
  | set_type.  |
  `-----------*/

  template <class StoredType>
  xalloc<StoredType>::set_type::set_type(const xalloc& slot, StoredType& data)
    : slot_(slot)
    , data_(data)
  {}

  template <class StoredType>
  void xalloc<StoredType>::set_type::operator()(std::ostream& ostr) const
  {
    slot_(ostr) = data_;
  }

  template <class StoredType>
  typename xalloc<StoredType>::set_type
  xalloc<StoredType>::set(StoredType& data) const
  {
    return set_type(*this, data);
  }

  /*-----------.
  | get_type.  |
  `-----------*/

  template <class StoredType>
  xalloc<StoredType>::get_type::get_type(const xalloc& slot, StoredType& data)
    : slot_(slot)
    , data_(data)
  {}

  template <class StoredType>
  void xalloc<StoredType>::get_type::operator()(std::ostream& ostr) const
  {
    data_ = slot_(ostr);
  }

  template <class StoredType>
  typename xalloc<StoredType>::get_type
  xalloc<StoredType>::get(StoredType& data) const
  {
    return get_type(*this, data);
  }

  /*------------.
  | swap_type.  |
  `------------*/

  template <class StoredType>
  xalloc<StoredType>::swap_type::swap_type(const xalloc& slot, StoredType& data)
    : slot_(slot)
    , data_(data)
  {}

  template <class StoredType>
  void xalloc<StoredType>::swap_type::operator()(std::ostream& ostr) const
  {
    std::swap(slot_(ostr), data_);
  }

  template <class StoredType>
  typename xalloc<StoredType>::swap_type
  xalloc<StoredType>::swap(StoredType& data) const
  {
    return swap_type(*this, data);
  }

} // namespace misc