Getting started on <future>

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@112061 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/future b/include/future
index 54b751f..c898648 100644
--- a/include/future
+++ b/include/future
@@ -463,6 +463,8 @@
 
 };
 
+template <> struct is_error_code_enum<future_errc> : public true_type {};
+
 //enum class launch
 struct launch
 {
@@ -495,6 +497,32 @@
 
 };
 
+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
diff --git a/src/future.cpp b/src/future.cpp
new file mode 100644
index 0000000..f9547c6
--- /dev/null
+++ b/src/future.cpp
@@ -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
diff --git a/test/thread/futures/futures.errors/default_error_condition.pass.cpp b/test/thread/futures/futures.errors/default_error_condition.pass.cpp
new file mode 100644
index 0000000..b792a66
--- /dev/null
+++ b/test/thread/futures/futures.errors/default_error_condition.pass.cpp
@@ -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);
+}
diff --git a/test/thread/futures/futures.errors/equivalent_error_code_int.pass.cpp b/test/thread/futures/futures.errors/equivalent_error_code_int.pass.cpp
new file mode 100644
index 0000000..2c7aa89
--- /dev/null
+++ b/test/thread/futures/futures.errors/equivalent_error_code_int.pass.cpp
@@ -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));
+}
diff --git a/test/thread/futures/futures.errors/equivalent_int_error_condition.pass.cpp b/test/thread/futures/futures.errors/equivalent_int_error_condition.pass.cpp
new file mode 100644
index 0000000..b24a53c
--- /dev/null
+++ b/test/thread/futures/futures.errors/equivalent_int_error_condition.pass.cpp
@@ -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));
+}
diff --git a/test/thread/futures/futures.errors/future_category.pass.cpp b/test/thread/futures/futures.errors/future_category.pass.cpp
new file mode 100644
index 0000000..56a29d6
--- /dev/null
+++ b/test/thread/futures/futures.errors/future_category.pass.cpp
@@ -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);
+}
diff --git a/test/thread/futures/futures.errors/make_error_code.pass.cpp b/test/thread/futures/futures.errors/make_error_code.pass.cpp
new file mode 100644
index 0000000..4009056
--- /dev/null
+++ b/test/thread/futures/futures.errors/make_error_code.pass.cpp
@@ -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());
+    }
+}
diff --git a/test/thread/futures/futures.errors/make_error_condition.pass.cpp b/test/thread/futures/futures.errors/make_error_condition.pass.cpp
new file mode 100644
index 0000000..6a9b554
--- /dev/null
+++ b/test/thread/futures/futures.errors/make_error_condition.pass.cpp
@@ -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());
+    }
+}
diff --git a/test/thread/futures/futures.future_error/code.pass.cpp b/test/thread/futures/futures.future_error/code.pass.cpp
new file mode 100644
index 0000000..9b7407a
--- /dev/null
+++ b/test/thread/futures/futures.future_error/code.pass.cpp
@@ -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);
+    }
+}
diff --git a/test/thread/futures/futures.future_error/types.pass.cpp b/test/thread/futures/futures.future_error/types.pass.cpp
new file mode 100644
index 0000000..9802051
--- /dev/null
+++ b/test/thread/futures/futures.future_error/types.pass.cpp
@@ -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), "");
+}
diff --git a/test/thread/futures/futures.future_error/what.pass.cpp b/test/thread/futures/futures.future_error/what.pass.cpp
new file mode 100644
index 0000000..082308d
--- /dev/null
+++ b/test/thread/futures/futures.future_error/what.pass.cpp
@@ -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);
+    }
+}
diff --git a/test/thread/futures/futures.overview/is_error_code_enum_future_errc.pass.cpp b/test/thread/futures/futures.overview/is_error_code_enum_future_errc.pass.cpp
new file mode 100644
index 0000000..ba5f6e7
--- /dev/null
+++ b/test/thread/futures/futures.overview/is_error_code_enum_future_errc.pass.cpp
@@ -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, "");
+}
diff --git a/test/thread/futures/futures.state/nothing_to_do.pass.cpp b/test/thread/futures/futures.state/nothing_to_do.pass.cpp
new file mode 100644
index 0000000..b85601f
--- /dev/null
+++ b/test/thread/futures/futures.state/nothing_to_do.pass.cpp
@@ -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()
+{
+}