diff --git a/doc/core.qbk b/doc/core.qbk index a184a98..df6330d 100644 --- a/doc/core.qbk +++ b/doc/core.qbk @@ -47,6 +47,7 @@ criteria for inclusion is that the utility component be: [include bit.qbk] [include checked_delete.qbk] [include cmath.qbk] +[include data.qbk] [include default_allocator.qbk] [include demangle.qbk] [include empty_value.qbk] @@ -71,6 +72,7 @@ criteria for inclusion is that the utility component be: [include quick_exit.qbk] [include ref.qbk] [include scoped_enum.qbk] +[include size.qbk] [include span.qbk] [include swap.qbk] [include typeinfo.qbk] diff --git a/doc/data.qbk b/doc/data.qbk new file mode 100644 index 0000000..9b2e74a --- /dev/null +++ b/doc/data.qbk @@ -0,0 +1,67 @@ +[/ +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +] + +[section:data data] + +[simplesect Authors] + +* Glen Fernandes + +[endsimplesect] + +[section Overview] + +The header provides function templates `data` +to obtain the pointer to the first element in a range. + +[endsect] + +[section Reference] + +``` +namespace boost { + +template +constexpr auto +data(C& c) noexcept(noexcept(c.data())) -> decltype(c.data()); + +template +constexpr auto +data(const C& c) noexcept(noexcept(c.data())) -> decltype(c.data()); + +template +constexpr T* +data(T(&a)[N]) noexcept; + +template +constexpr const T* +data(std::initializer_list l) noexcept; + +} /* boost */ +``` + +[section Functions] + +[variablelist +[[`template constexpr auto data(C& c) noexcept(noexcept(c.data())) -> +decltype(c.data());`] +[Returns `c.begin()`.]] +[[`template constexpr auto data(const C& c) +noexcept(noexcept(c.data())) -> decltype(c.data());`] +[Returns `c.begin()`.]] +[[`template constexpr T* data(T(&a)[N]) noexcept;`] +[Returns `a`.]] +[[`template constexpr const T* data(std::initializer_list l) +noexcept;`] +[Returns `l.begin()`.]]] + +[endsect] + +[endsect] + +[endsect] diff --git a/doc/size.qbk b/doc/size.qbk new file mode 100644 index 0000000..9f0c3b3 --- /dev/null +++ b/doc/size.qbk @@ -0,0 +1,54 @@ +[/ +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +] + +[section:size size] + +[simplesect Authors] + +* Glen Fernandes + +[endsimplesect] + +[section Overview] + +The header provides function templates `size` to obtain +the number of elements in a range. + +[endsect] + +[section Reference] + +``` +namespace boost { + +template +constexpr auto +size(const C& c) noexcept(noexcept(c.size())) -> decltype(c.size()); + +template +constexpr std::size_t +size(T(&)[N]) noexcept; + +} /* boost */ +``` + +[section Functions] + +[variablelist +[[`template constexpr auto size(const C& c) +noexcept(noexcept(c.size())) -> decltype(c.size());`] +[Returns `c.size()`.]] +[[`template constexpr std::size_t size(T(&)[N]) +noexcept;`] +[Returns `N`.]]] + +[endsect] + +[endsect] + +[endsect] diff --git a/include/boost/core/data.hpp b/include/boost/core/data.hpp new file mode 100644 index 0000000..44cf426 --- /dev/null +++ b/include/boost/core/data.hpp @@ -0,0 +1,46 @@ +/* +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#ifndef BOOST_CORE_DATA_HPP +#define BOOST_CORE_DATA_HPP + +#include +#include + +namespace boost { + +template +inline constexpr auto +data(C& c) noexcept(noexcept(c.data())) -> decltype(c.data()) +{ + return c.data(); +} + +template +inline constexpr auto +data(const C& c) noexcept(noexcept(c.data())) -> decltype(c.data()) +{ + return c.data(); +} + +template +inline constexpr T* +data(T(&a)[N]) noexcept +{ + return a; +} + +template +inline constexpr const T* +data(std::initializer_list l) noexcept +{ + return l.begin(); +} + +} /* boost */ + +#endif diff --git a/include/boost/core/size.hpp b/include/boost/core/size.hpp new file mode 100644 index 0000000..449ccb9 --- /dev/null +++ b/include/boost/core/size.hpp @@ -0,0 +1,31 @@ +/* +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#ifndef BOOST_CORE_SIZE_HPP +#define BOOST_CORE_SIZE_HPP + +#include + +namespace boost { + +template +inline constexpr auto +size(const C& c) noexcept(noexcept(c.size())) -> decltype(c.size()) +{ + return c.size(); +} + +template +inline constexpr std::size_t +size(T(&)[N]) noexcept +{ + return N; +} + +} /* boost */ + +#endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 65e17ae..6b642aa 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -360,5 +360,8 @@ run max_align_test.cpp ; run memory_resource_test.cpp ; +run data_test.cpp ; +run size_test.cpp ; + use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/data_test.cpp b/test/data_test.cpp new file mode 100644 index 0000000..f312d86 --- /dev/null +++ b/test/data_test.cpp @@ -0,0 +1,68 @@ +/* +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include +#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE) +#include +#include + +class range { +public: + int* data() { + return &v_[0]; + } + + const int* data() const { + return &v_[0]; + } + + std::size_t size() const { + return 4; + } + +private: + int v_[4]; +}; + +void test_range() +{ + range c; + BOOST_TEST_EQ(boost::data(c), c.data()); +} + +void test_const_range() +{ + const range c = range(); + BOOST_TEST_EQ(boost::data(c), c.data()); +} + +void test_array() +{ + int a[4]; + BOOST_TEST_EQ(boost::data(a), a); +} + +void test_initializer_list() +{ + std::initializer_list l{1, 2, 3, 4}; + BOOST_TEST_EQ(boost::data(l), l.begin()); +} + +int main() +{ + test_range(); + test_const_range(); + test_array(); + test_initializer_list(); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif diff --git a/test/size_test.cpp b/test/size_test.cpp new file mode 100644 index 0000000..140d403 --- /dev/null +++ b/test/size_test.cpp @@ -0,0 +1,42 @@ +/* +Copyright 2023 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include +#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE) +#include +#include + +struct range { + std::size_t size() const { + return 4; + } +}; + +void test_range() +{ + range c; + BOOST_TEST_EQ(boost::size(c), 4); +} + +void test_array() +{ + int a[4]; + BOOST_TEST_EQ(boost::size(a), 4); +} + +int main() +{ + test_range(); + test_array(); + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif