This file is indexed.

/usr/include/xbt/future.hpp is in libsimgrid-dev 3.18+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (c) 2015-2017. The SimGrid Team.
 * All rights reserved.                                                     */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

#ifndef XBT_FUTURE_HPP
#define XBT_FUTURE_HPP

#include <cstddef>

#include <exception>
#include <functional>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <xbt/ex.h>

namespace simgrid {
namespace xbt {

/** A value or an exception (or nothing)
 *
 *  This is similar to `optional<expected<T>>`` but it with a Future/Promise
 *  like API.
 *
 *  Also the name is not so great.
 **/
template<class T>
class Result {
  enum class ResultStatus {
    invalid,
    value,
    exception,
  };
public:
  Result() { /* Nothing to do */}
  ~Result() { this->reset(); }

  // Copy (if T is copyable) and move:
  Result(Result const& that)
  {
    (*this) = that;
  }
  Result& operator=(Result const& that)
  {
    this->reset();
    switch (that.status_) {
      case ResultStatus::invalid:
        break;
      case ResultStatus::value:
        new (&value_) T(that.value);
        break;
      case ResultStatus::exception:
        new (&exception_) T(that.exception);
        break;
      default:
        THROW_IMPOSSIBLE;
    }
    return *this;
  }
  Result(Result&& that)
  {
    *this = std::move(that);
  }
  Result& operator=(Result&& that)
  {
    this->reset();
    switch (that.status_) {
      case ResultStatus::invalid:
        break;
      case ResultStatus::value:
        new (&value_) T(std::move(that.value));
        that.value.~T();
        break;
      case ResultStatus::exception:
        new (&exception_) T(std::move(that.exception));
        that.exception.~exception_ptr();
        break;
      default:
        THROW_IMPOSSIBLE;
    }
    that.status_ = ResultStatus::invalid;
    return *this;
  }

  bool is_valid() const
  {
    return status_ != ResultStatus::invalid;
  }
  void reset()
  {
    switch (status_) {
      case ResultStatus::invalid:
        break;
      case ResultStatus::value:
        value_.~T();
        break;
      case ResultStatus::exception:
        exception_.~exception_ptr();
        break;
      default:
        THROW_IMPOSSIBLE;
    }
    status_ = ResultStatus::invalid;
  }
  void set_exception(std::exception_ptr e)
  {
    this->reset();
    new (&exception_) std::exception_ptr(std::move(e));
    status_ = ResultStatus::exception;
  }
  void set_value(T&& value)
  {
    this->reset();
    new (&value_) T(std::move(value));
    status_ = ResultStatus::value;
  }
  void set_value(T const& value)
  {
    this->reset();
    new (&value_) T(value);
    status_ = ResultStatus::value;
  }

  /** Extract the value from the future
   *
   *  After this, the value is invalid.
   **/
  T get()
  {
    switch (status_) {
      case ResultStatus::value: {
        T value = std::move(value_);
        value_.~T();
        status_ = ResultStatus::invalid;
        return std::move(value);
      }
      case ResultStatus::exception: {
        std::exception_ptr exception = std::move(exception_);
        exception_.~exception_ptr();
        status_ = ResultStatus::invalid;
        std::rethrow_exception(std::move(exception));
        break;
      }
      default:
        throw std::logic_error("Invalid result");
    }
  }
private:
  ResultStatus status_ = ResultStatus::invalid;
  union {
    T value_;
    std::exception_ptr exception_;
  };
};

template<>
class Result<void> : public Result<std::nullptr_t>
{
public:
  void set_value()
  {
    Result<std::nullptr_t>::set_value(nullptr);
  }
  void get()
  {
    Result<std::nullptr_t>::get();
  }
};

template<class T>
class Result<T&> : public Result<std::reference_wrapper<T>>
{
public:
  void set_value(T& value)
  {
    Result<std::reference_wrapper<T>>::set_value(std::ref(value));
  }
  T& get()
  {
    return Result<std::reference_wrapper<T>>::get();
  }
};

/** Execute some code and set a promise or result accordingly
 *
 *  Roughly this does:
 *
 *  <pre>
 *  promise.set_value(code());
 *  </pre>
 *
 *  but it takes care of exceptions and works with `void`.
 *
 *  We might need this when working with generic code because
 *  the trivial implementation does not work with `void` (before C++1z).
 *
 *  @param    code  What we want to do
 *  @param  promise Where to want to store the result
 */
template<class R, class F>
auto fulfillPromise(R& promise, F&& code)
-> decltype(promise.set_value(code()))
{
  try {
    promise.set_value(std::forward<F>(code)());
  }
  catch(...) {
    promise.set_exception(std::current_exception());
  }
}

template<class P, class F>
auto fulfillPromise(P& promise, F&& code)
-> decltype(promise.set_value())
{
  try {
    std::forward<F>(code)();
    promise.set_value();
  }
  catch(...) {
    promise.set_exception(std::current_exception());
  }
}

/** Set a promise/result from a future/result
 *
 *  Roughly this does:
 *
 *  <pre>promise.set_value(future);</pre>
 *
 *  but it takes care of exceptions and works with `void`.
 *
 *  We might need this when working with generic code because
 *  the trivial implementation does not work with `void` (before C++1z).
 *
 *  @param promise output (a valid future or a result)
 *  @param future  input (a ready/waitable future or a valid result)
 */
template<class P, class F> inline
void setPromise(P& promise, F&& future)
{
  fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });
}

}
}

#endif