diff options
Diffstat (limited to 'src/boost/libs/functional/factory')
12 files changed, 681 insertions, 0 deletions
diff --git a/src/boost/libs/functional/factory/index.html b/src/boost/libs/functional/factory/index.html new file mode 100644 index 000000000..180302981 --- /dev/null +++ b/src/boost/libs/functional/factory/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> + <head> + <meta http-equiv="refresh" content="0; URL=doc/html/index.html"> + </head> + <body> + Automatic redirection failed, click this + <a href="doc/html/index.html">link</a> <hr> + <p>© Copyright Tobias Schwinger, 2009</p> + <p>Distributed under the Boost Software License, Version 1.0. (See + accompanying file <a href="../../../LICENSE_1_0.txt"> + LICENSE_1_0.txt</a> or copy at + <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p> + </body> +</html> diff --git a/src/boost/libs/functional/factory/test/Jamfile b/src/boost/libs/functional/factory/test/Jamfile new file mode 100644 index 000000000..7030e9392 --- /dev/null +++ b/src/boost/libs/functional/factory/test/Jamfile @@ -0,0 +1,20 @@ +# Copyright 2007,2008 Tobias Schwinger +# +# Copyright 2019 Glen Joseph Fernandes +# (glenjofe@gmail.com) +# +# Distributed under the Boost Software License, Version 1.0. +# (http://www.boost.org/LICENSE_1_0.txt) + +import testing ; + +run value_factory.cpp ; +run value_factory_args.cpp ; +run value_factory_move.cpp ; +run factory.cpp ; +run factory_args.cpp ; +run factory_move.cpp ; +run factory_with_allocator.cpp ; +run factory_with_std_allocator.cpp ; +run factory_allocator_throws.cpp ; +run factory_default_allocator.cpp : : : <exception-handling>off ; diff --git a/src/boost/libs/functional/factory/test/factory.cpp b/src/boost/libs/functional/factory/test/factory.cpp new file mode 100644 index 000000000..2da5cf41a --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory.cpp @@ -0,0 +1,87 @@ +/* +Copyright 2007 Tobias Schwinger + +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/scoped_ptr.hpp> + +class sum { +public: + explicit sum(int i0 = 0, int i1 = 0, int i2 = 0, int i3 = 0, + int i4 = 0, int i5 = 0, int i6 = 0, int i7 = 0, + int i8 = 0, int i9 = 0) + : value_(i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + boost::factory<sum*> x; + int a = 1; + int b = 2; + int c = 3; + int d = 4; + int e = 5; + int f = 6; + int g = 7; + int h = 8; + int i = 9; + int j = 10; + { + boost::scoped_ptr<sum> s(x()); + BOOST_TEST(s->get() == 0); + } + { + boost::scoped_ptr<sum> s(x(a)); + BOOST_TEST(s->get() == 1); + } + { + boost::scoped_ptr<sum> s(x(a, b)); + BOOST_TEST(s->get() == 3); + } + { + boost::scoped_ptr<sum> s(x(a, b, c)); + BOOST_TEST(s->get() == 6); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d)); + BOOST_TEST(s->get() == 10); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e)); + BOOST_TEST(s->get() == 15); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e, f)); + BOOST_TEST(s->get() == 21); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g)); + BOOST_TEST(s->get() == 28); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h)); + BOOST_TEST(s->get() == 36); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h, i)); + BOOST_TEST(s->get() == 45); + } + { + boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h, i, j)); + BOOST_TEST(s->get() == 55); + } + return boost::report_errors(); +} diff --git a/src/boost/libs/functional/factory/test/factory_allocator_throws.cpp b/src/boost/libs/functional/factory/test/factory_allocator_throws.cpp new file mode 100644 index 000000000..0186b7936 --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_allocator_throws.cpp @@ -0,0 +1,85 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/shared_ptr.hpp> + +struct type { + explicit type(bool b) { + if (b) { + throw true; + } + } +}; + +template<class T> +class creator { +public: + static int count; + + typedef T value_type; + typedef T* pointer; + + template<class U> + struct rebind { + typedef creator<U> other; + }; + + creator() { } + + template<class U> + creator(const creator<U>&) { } + + T* allocate(std::size_t size) { + ++count; + return static_cast<T*>(::operator new(sizeof(T) * size)); + } + + void deallocate(T* ptr, std::size_t) { + --count; + ::operator delete(ptr); + } +}; + +template<class T> +int creator<T>::count = 0; + +template<class T, class U> +inline bool +operator==(const creator<T>&, const creator<U>&) +{ + return true; +} + +template<class T, class U> +inline bool +operator!=(const creator<T>&, const creator<U>&) +{ + return false; +} + +int main() +{ + bool b = true; + try { + boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>, + creator<void>, + boost::factory_alloc_for_pointee_and_deleter>()(b)); + } catch (...) { + BOOST_TEST(creator<type>::count == 0); + } + try { + boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>, + creator<void>, + boost::factory_passes_alloc_to_smart_pointer>()(b)); + } catch (...) { + BOOST_TEST(creator<type>::count == 0); + } + return boost::report_errors(); +} + diff --git a/src/boost/libs/functional/factory/test/factory_args.cpp b/src/boost/libs/functional/factory/test/factory_args.cpp new file mode 100644 index 000000000..bec677e8d --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_args.cpp @@ -0,0 +1,42 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/config.hpp> +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/scoped_ptr.hpp> + +class sum { +public: + explicit sum(int a = 0, int b = 0, int c = 0, int d = 0, + int e = 0, int f = 0, int g = 0, int h = 0, + int i = 0, int j = 0, int k = 0, int l = 0) + : value_(a + b + c + d + e + f + g + h + i + j + k + l) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + boost::scoped_ptr<sum> s(boost::factory<sum*>()(1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12)); + BOOST_TEST(s->get() == 78); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif diff --git a/src/boost/libs/functional/factory/test/factory_default_allocator.cpp b/src/boost/libs/functional/factory/test/factory_default_allocator.cpp new file mode 100644 index 000000000..c6450ba45 --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_default_allocator.cpp @@ -0,0 +1,56 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/factory.hpp> +#include <boost/core/default_allocator.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/shared_ptr.hpp> +#include <boost/config.hpp> +#include <exception> + +#if defined(BOOST_NO_EXCEPTIONS) +namespace boost { + +BOOST_NORETURN void throw_exception(const std::exception&) +{ + std::terminate(); +} + +} +#endif + +class sum { +public: + sum(int a, int b) + : value_(a + b) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + int a = 1; + int b = 2; + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + boost::default_allocator<void>, + boost::factory_alloc_for_pointee_and_deleter>()(a, b)); + BOOST_TEST(s->get() == 3); + } + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + boost::default_allocator<void>, + boost::factory_passes_alloc_to_smart_pointer>()(a, b)); + BOOST_TEST(s->get() == 3); + } + return boost::report_errors(); +} diff --git a/src/boost/libs/functional/factory/test/factory_move.cpp b/src/boost/libs/functional/factory/test/factory_move.cpp new file mode 100644 index 000000000..5b6575b94 --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_move.cpp @@ -0,0 +1,55 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/config.hpp> +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/scoped_ptr.hpp> + +class arg { +public: + explicit arg(int n) + : value_(n) { } + + arg(arg&& a) + : value_(a.value_) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +class sum { +public: + explicit sum(arg&& a1, arg&& a2) + : value_(a1.get() + a2.get()) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + boost::scoped_ptr<sum> s(boost::factory<sum*>()(arg(1), arg(2))); + BOOST_TEST(s->get() == 3); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif diff --git a/src/boost/libs/functional/factory/test/factory_with_allocator.cpp b/src/boost/libs/functional/factory/test/factory_with_allocator.cpp new file mode 100644 index 000000000..f321df184 --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_with_allocator.cpp @@ -0,0 +1,95 @@ +/* +Copyright 2007 Tobias Schwinger + +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/shared_ptr.hpp> + +class sum { +public: + sum(int a, int b) + : value_(a + b) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +template<class T> +class creator { +public: + static int count; + + typedef T value_type; + typedef T* pointer; + + template<class U> + struct rebind { + typedef creator<U> other; + }; + + creator() { } + + template<class U> + creator(const creator<U>&) { } + + T* allocate(std::size_t size) { + ++count; + return static_cast<T*>(::operator new(sizeof(T) * size)); + } + + void deallocate(T* ptr, std::size_t) { + --count; + ::operator delete(ptr); + } +}; + +template<class T> +int creator<T>::count = 0; + +template<class T, class U> +inline bool +operator==(const creator<T>&, const creator<U>&) +{ + return true; +} + +template<class T, class U> +inline bool +operator!=(const creator<T>&, const creator<U>&) +{ + return false; +} + +int main() +{ + int a = 1; + int b = 2; + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + creator<void>, + boost::factory_alloc_for_pointee_and_deleter>()(a, b)); + BOOST_TEST(creator<sum>::count == 1); + BOOST_TEST(s->get() == 3); + } + BOOST_TEST(creator<sum>::count == 0); + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + creator<void>, + boost::factory_passes_alloc_to_smart_pointer>()(a, b)); + BOOST_TEST(creator<sum>::count == 1); + BOOST_TEST(s->get() == 3); + } + BOOST_TEST(creator<sum>::count == 0); + return boost::report_errors(); +} + diff --git a/src/boost/libs/functional/factory/test/factory_with_std_allocator.cpp b/src/boost/libs/functional/factory/test/factory_with_std_allocator.cpp new file mode 100644 index 000000000..6b7def46a --- /dev/null +++ b/src/boost/libs/functional/factory/test/factory_with_std_allocator.cpp @@ -0,0 +1,46 @@ +/* +Copyright 2007 Tobias Schwinger +Copyright 2017 Daniel James + +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/factory.hpp> +#include <boost/core/lightweight_test.hpp> +#include <boost/smart_ptr/shared_ptr.hpp> +#include <memory> + +class sum { +public: + sum(int a, int b) + : value_(a + b) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + int a = 1; + int b = 2; + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + std::allocator<char>, + boost::factory_alloc_for_pointee_and_deleter>()(a, b)); + BOOST_TEST(s->get() == 3); + } + { + boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>, + std::allocator<char>, + boost::factory_passes_alloc_to_smart_pointer>()(a, b)); + BOOST_TEST(s->get() == 3); + } + return boost::report_errors(); +} diff --git a/src/boost/libs/functional/factory/test/value_factory.cpp b/src/boost/libs/functional/factory/test/value_factory.cpp new file mode 100644 index 000000000..7af232c05 --- /dev/null +++ b/src/boost/libs/functional/factory/test/value_factory.cpp @@ -0,0 +1,86 @@ +/* +Copyright 2007 Tobias Schwinger + +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/functional/value_factory.hpp> +#include <boost/core/lightweight_test.hpp> + +class sum { +public: + explicit sum(int i0 = 0, int i1 = 0, int i2 = 0, int i3 = 0, + int i4 = 0, int i5 = 0, int i6 = 0, int i7 = 0, + int i8 = 0, int i9 = 0) + : value_(i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + boost::value_factory<sum> x; + int a = 1; + int b = 2; + int c = 3; + int d = 4; + int e = 5; + int f = 6; + int g = 7; + int h = 8; + int i = 9; + int j = 10; + { + sum s(x()); + BOOST_TEST(s.get() == 0); + } + { + sum s(x(a)); + BOOST_TEST(s.get() == 1); + } + { + sum s(x(a, b)); + BOOST_TEST(s.get() == 3); + } + { + sum s(x(a, b, c)); + BOOST_TEST(s.get() == 6); + } + { + sum s(x(a, b, c, d)); + BOOST_TEST(s.get() == 10); + } + { + sum s(x(a, b, c, d, e)); + BOOST_TEST(s.get() == 15); + } + { + sum s(x(a, b, c, d, e, f)); + BOOST_TEST(s.get() == 21); + } + { + sum s(x(a, b, c, d, e, f, g)); + BOOST_TEST(s.get() == 28); + } + { + sum s(x(a, b, c, d, e, f, g, h)); + BOOST_TEST(s.get() == 36); + } + { + sum s(x(a, b, c, d, e, f, g, h, i)); + BOOST_TEST(s.get() == 45); + } + { + sum s(x(a, b, c, d, e, f, g, h, i, j)); + BOOST_TEST(s.get() == 55); + } + return boost::report_errors(); +} diff --git a/src/boost/libs/functional/factory/test/value_factory_args.cpp b/src/boost/libs/functional/factory/test/value_factory_args.cpp new file mode 100644 index 000000000..0288fcd57 --- /dev/null +++ b/src/boost/libs/functional/factory/test/value_factory_args.cpp @@ -0,0 +1,40 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/config.hpp> +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include <boost/functional/value_factory.hpp> +#include <boost/core/lightweight_test.hpp> + +class sum { +public: + explicit sum(int a = 0, int b = 0, int c = 0, int d = 0, + int e = 0, int f = 0, int g = 0, int h = 0, + int i = 0, int j = 0, int k = 0, int l = 0) + : value_(a + b + c + d + e + f + g + h + i + j + k + l) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + sum s(boost::value_factory<sum>()(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); + BOOST_TEST(s.get() == 78); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif diff --git a/src/boost/libs/functional/factory/test/value_factory_move.cpp b/src/boost/libs/functional/factory/test/value_factory_move.cpp new file mode 100644 index 000000000..c0dd37fc6 --- /dev/null +++ b/src/boost/libs/functional/factory/test/value_factory_move.cpp @@ -0,0 +1,54 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include <boost/config.hpp> +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) +#include <boost/functional/value_factory.hpp> +#include <boost/core/lightweight_test.hpp> + +class arg { +public: + explicit arg(int n) + : value_(n) { } + + arg(arg&& a) + : value_(a.value_) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +class sum { +public: + explicit sum(arg&& a1, arg&& a2) + : value_(a1.get() + a2.get()) { } + + int get() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + sum s(boost::value_factory<sum>()(arg(1), arg(2))); + BOOST_TEST(s.get() == 3); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif |