mirror of
https://github.com/boostorg/core.git
synced 2025-05-09 23:03:54 +00:00
Implement data and size functions
This commit is contained in:
parent
8977da6f50
commit
249c5bece2
@ -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]
|
||||
|
67
doc/data.qbk
Normal file
67
doc/data.qbk
Normal file
@ -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 <boost/core/data.hpp> provides function templates `data`
|
||||
to obtain the pointer to the first element in a range.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Reference]
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
template<class C>
|
||||
constexpr auto
|
||||
data(C& c) noexcept(noexcept(c.data())) -> decltype(c.data());
|
||||
|
||||
template<class C>
|
||||
constexpr auto
|
||||
data(const C& c) noexcept(noexcept(c.data())) -> decltype(c.data());
|
||||
|
||||
template<class T, std::size_t N>
|
||||
constexpr T*
|
||||
data(T(&a)[N]) noexcept;
|
||||
|
||||
template<class T>
|
||||
constexpr const T*
|
||||
data(std::initializer_list<T> l) noexcept;
|
||||
|
||||
} /* boost */
|
||||
```
|
||||
|
||||
[section Functions]
|
||||
|
||||
[variablelist
|
||||
[[`template<class C> constexpr auto data(C& c) noexcept(noexcept(c.data())) ->
|
||||
decltype(c.data());`]
|
||||
[Returns `c.begin()`.]]
|
||||
[[`template<class C> constexpr auto data(const C& c)
|
||||
noexcept(noexcept(c.data())) -> decltype(c.data());`]
|
||||
[Returns `c.begin()`.]]
|
||||
[[`template<class T, std::size_t N> constexpr T* data(T(&a)[N]) noexcept;`]
|
||||
[Returns `a`.]]
|
||||
[[`template<class T> constexpr const T* data(std::initializer_list<T> l)
|
||||
noexcept;`]
|
||||
[Returns `l.begin()`.]]]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
54
doc/size.qbk
Normal file
54
doc/size.qbk
Normal file
@ -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 <boost/core/size.hpp> provides function templates `size` to obtain
|
||||
the number of elements in a range.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Reference]
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
template<class C>
|
||||
constexpr auto
|
||||
size(const C& c) noexcept(noexcept(c.size())) -> decltype(c.size());
|
||||
|
||||
template<class T, std::size_t N>
|
||||
constexpr std::size_t
|
||||
size(T(&)[N]) noexcept;
|
||||
|
||||
} /* boost */
|
||||
```
|
||||
|
||||
[section Functions]
|
||||
|
||||
[variablelist
|
||||
[[`template<class C> constexpr auto size(const C& c)
|
||||
noexcept(noexcept(c.size())) -> decltype(c.size());`]
|
||||
[Returns `c.size()`.]]
|
||||
[[`template<class T, std::size_t N> constexpr std::size_t size(T(&)[N])
|
||||
noexcept;`]
|
||||
[Returns `N`.]]]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
46
include/boost/core/data.hpp
Normal file
46
include/boost/core/data.hpp
Normal file
@ -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 <initializer_list>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
|
||||
template<class C>
|
||||
inline constexpr auto
|
||||
data(C& c) noexcept(noexcept(c.data())) -> decltype(c.data())
|
||||
{
|
||||
return c.data();
|
||||
}
|
||||
|
||||
template<class C>
|
||||
inline constexpr auto
|
||||
data(const C& c) noexcept(noexcept(c.data())) -> decltype(c.data())
|
||||
{
|
||||
return c.data();
|
||||
}
|
||||
|
||||
template<class T, std::size_t N>
|
||||
inline constexpr T*
|
||||
data(T(&a)[N]) noexcept
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline constexpr const T*
|
||||
data(std::initializer_list<T> l) noexcept
|
||||
{
|
||||
return l.begin();
|
||||
}
|
||||
|
||||
} /* boost */
|
||||
|
||||
#endif
|
31
include/boost/core/size.hpp
Normal file
31
include/boost/core/size.hpp
Normal file
@ -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 <cstddef>
|
||||
|
||||
namespace boost {
|
||||
|
||||
template<class C>
|
||||
inline constexpr auto
|
||||
size(const C& c) noexcept(noexcept(c.size())) -> decltype(c.size())
|
||||
{
|
||||
return c.size();
|
||||
}
|
||||
|
||||
template<class T, std::size_t N>
|
||||
inline constexpr std::size_t
|
||||
size(T(&)[N]) noexcept
|
||||
{
|
||||
return N;
|
||||
}
|
||||
|
||||
} /* boost */
|
||||
|
||||
#endif
|
@ -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 ;
|
||||
|
68
test/data_test.cpp
Normal file
68
test/data_test.cpp
Normal file
@ -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 <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE)
|
||||
#include <boost/core/data.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
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<int> 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
|
42
test/size_test.cpp
Normal file
42
test/size_test.cpp
Normal file
@ -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 <boost/config.hpp>
|
||||
#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE)
|
||||
#include <boost/core/size.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user