Getting started on <future>

llvm-svn: 112061
This commit is contained in:
Howard Hinnant 2010-08-25 17:32:05 +00:00
parent f1f2133ac0
commit dae3481b28
13 changed files with 376 additions and 0 deletions

View File

@ -463,6 +463,8 @@ enum _ {
};
template <> struct is_error_code_enum<future_errc> : public true_type {};
//enum class launch
struct launch
{
@ -495,6 +497,32 @@ enum _ {
};
const error_category& future_category();
inline _LIBCPP_INLINE_VISIBILITY
error_code
make_error_code(future_errc __e)
{
return error_code(static_cast<int>(__e), future_category());
}
inline _LIBCPP_INLINE_VISIBILITY
error_condition
make_error_condition(future_errc __e)
{
return error_condition(static_cast<int>(__e), future_category());
}
class future_error
: public logic_error
{
error_code __ec_;
public:
future_error(error_code __ec);
const error_code& code() const throw() {return __ec_;}
};
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_FUTURE

63
libcxx/src/future.cpp Normal file
View File

@ -0,0 +1,63 @@
//===------------------------- future.cpp ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "future"
#include "string"
_LIBCPP_BEGIN_NAMESPACE_STD
class _LIBCPP_HIDDEN __future_error_category
: public __do_message
{
public:
virtual const char* name() const;
virtual string message(int ev) const;
};
const char*
__future_error_category::name() const
{
return "future";
}
string
__future_error_category::message(int ev) const
{
switch (ev)
{
case future_errc::broken_promise:
return string("The associated promise has been destructed prior "
"to the associated state becoming ready.");
case future_errc::future_already_retrieved:
return string("The future has already been retrieved from "
"the promise or packaged_task.");
case future_errc::promise_already_satisfied:
return string("The state of the promise has already been set.");
case future_errc::no_state:
return string("Operation not permitted on an object without "
"an associated state.");
}
return string("unspecified future_errc value\n");
}
const error_category&
future_category()
{
static __future_error_category __f;
return __f;
}
future_error::future_error(error_code __ec)
: logic_error(__ec.message()),
__ec_(__ec)
{
}
_LIBCPP_END_NAMESPACE_STD

View File

@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// const error_category& future_category();
// virtual error_condition default_error_condition(int ev) const;
#include <future>
#include <cassert>
int main()
{
const std::error_category& e_cat = std::future_category();
std::error_condition e_cond = e_cat.default_error_condition(std::errc::not_a_directory);
assert(e_cond.category() == e_cat);
assert(e_cond.value() == std::errc::not_a_directory);
}

View File

@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// const error_category& future_category();
// virtual bool equivalent(const error_code& code, int condition) const;
#include <future>
#include <cassert>
int main()
{
const std::error_category& e_cat = std::future_category();
assert(e_cat.equivalent(std::error_code(5, e_cat), 5));
assert(!e_cat.equivalent(std::error_code(5, e_cat), 6));
}

View File

@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// const error_category& future_category();
// virtual bool equivalent(int code, const error_condition& condition) const;
#include <future>
#include <cassert>
int main()
{
const std::error_category& e_cat = std::future_category();
std::error_condition e_cond = e_cat.default_error_condition(5);
assert(e_cat.equivalent(5, e_cond));
assert(!e_cat.equivalent(6, e_cond));
}

View File

@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// const error_category& future_category();
#include <future>
#include <cstring>
#include <cassert>
int main()
{
const std::error_category& ec = std::future_category();
assert(std::strcmp(ec.name(), "future") == 0);
}

View File

@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// class error_code
// error_code make_error_code(future_errc e);
#include <future>
#include <cassert>
int main()
{
{
std::error_code ec = make_error_code(std::future_errc::broken_promise);
assert(ec.value() == static_cast<int>(std::future_errc::broken_promise));
assert(ec.category() == std::future_category());
}
}

View File

@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// class error_condition
// error_condition make_error_condition(future_errc e);
#include <future>
#include <cassert>
int main()
{
{
const std::error_condition ec1 =
std::make_error_condition(std::future_errc::future_already_retrieved);
assert(ec1.value() ==
static_cast<int>(std::future_errc::future_already_retrieved));
assert(ec1.category() == std::future_category());
}
}

View File

@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// class future_error
// const error_code& code() const throw();
#include <future>
#include <cassert>
int main()
{
{
std::error_code ec = std::make_error_code(std::future_errc::broken_promise);
std::future_error f(ec);
assert(f.code() == ec);
}
{
std::error_code ec = std::make_error_code(std::future_errc::future_already_retrieved);
std::future_error f(ec);
assert(f.code() == ec);
}
{
std::error_code ec = std::make_error_code(std::future_errc::promise_already_satisfied);
std::future_error f(ec);
assert(f.code() == ec);
}
{
std::error_code ec = std::make_error_code(std::future_errc::no_state);
std::future_error f(ec);
assert(f.code() == ec);
}
}

View File

@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// class future_error : public logic_error {...};
#include <future>
#include <type_traits>
int main()
{
static_assert((std::is_convertible<std::future_error*,
std::logic_error*>::value), "");
}

View File

@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// class future_error
// const char* what() const throw();
#include <future>
#include <cstring>
#include <cassert>
int main()
{
{
std::future_error f(std::make_error_code(std::future_errc::broken_promise));
assert(std::strcmp(f.what(), "The associated promise has been destructed prior "
"to the associated state becoming ready.") == 0);
}
{
std::future_error f(std::make_error_code(std::future_errc::future_already_retrieved));
assert(std::strcmp(f.what(), "The future has already been retrieved from "
"the promise or packaged_task.") == 0);
}
{
std::future_error f(std::make_error_code(std::future_errc::promise_already_satisfied));
assert(std::strcmp(f.what(), "The state of the promise has already been set.") == 0);
}
{
std::future_error f(std::make_error_code(std::future_errc::no_state));
assert(std::strcmp(f.what(), "Operation not permitted on an object without "
"an associated state.") == 0);
}
}

View File

@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <future>
// template <> struct is_error_code_enum<future_errc> : public true_type {};
#include <future>
int main()
{
static_assert(std::is_error_code_enum<std::future_errc>::value, "");
}

View File

@ -0,0 +1,13 @@
// -*- C++ -*-
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
int main()
{
}