Skip to content

Commit

Permalink
Add detail::has_constant_size
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Dec 7, 2023
1 parent b9c1bc3 commit 8848891
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
25 changes: 25 additions & 0 deletions include/boost/hash2/detail/has_constant_size.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BOOST_HASH2_DETAIL_HAS_CONSTANT_SIZE_HPP_INCLUDED
#define BOOST_HASH2_DETAIL_HAS_CONSTANT_SIZE_HPP_INCLUDED

// Copyright 2023 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/mp11/integral.hpp>
#include <boost/mp11/utility.hpp>

namespace boost
{
namespace hash2
{
namespace detail
{

template<class T> using has_constant_size_impl = mp11::mp_size_t< T().size() >;
template<class T> using has_constant_size = mp11::mp_valid<has_constant_size_impl, T>;

} // namespace detail
} // namespace hash2
} // namespace boost

#endif // #ifndef BOOST_HASH2_DETAIL_HAS_CONSTANT_SIZE_HPP_INCLUDED
3 changes: 2 additions & 1 deletion test/Jamfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017-2019 Peter Dimov
# Copyright 2017-2023 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.

import testing ;
Expand All @@ -11,6 +11,7 @@ run is_contiguous_range.cpp ;
run is_unordered_range.cpp ;
run is_tuple_like.cpp ;
run endian.cpp ;
run has_constant_size.cpp ;

# helpers

Expand Down
91 changes: 91 additions & 0 deletions test/has_constant_size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2023 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/hash2/detail/has_constant_size.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/array.hpp>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <array>
#include <forward_list>
#include <unordered_set>
#include <unordered_map>

struct X
{
};

int main()
{
using boost::hash2::detail::has_constant_size;

BOOST_TEST_TRAIT_FALSE((has_constant_size<void>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<void const>));

BOOST_TEST_TRAIT_FALSE((has_constant_size<int>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<int const>));

BOOST_TEST_TRAIT_FALSE((has_constant_size<X>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<X const>));

BOOST_TEST_TRAIT_FALSE((has_constant_size<int[2]>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<int const[2]>));

BOOST_TEST_TRAIT_FALSE((has_constant_size<std::string>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<std::string const>));

BOOST_TEST_TRAIT_FALSE((has_constant_size<std::wstring>));
BOOST_TEST_TRAIT_FALSE((has_constant_size<std::wstring const>));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::vector<X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::vector<X> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::deque<X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::deque<X> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::set<int> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::set<int> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::multiset<int> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::multiset<int> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::map<int, X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::map<int, X> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::multimap<int, X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::multimap<int, X> const >));

BOOST_TEST_TRAIT_TRUE((has_constant_size< std::array<X, 2> >));
BOOST_TEST_TRAIT_TRUE((has_constant_size< std::array<X, 2> const >));

BOOST_TEST_TRAIT_TRUE((has_constant_size< std::array<X, 0> >));
BOOST_TEST_TRAIT_TRUE((has_constant_size< std::array<X, 0> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::forward_list<X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::forward_list<X> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_set<int> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_set<int> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_multiset<int> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_multiset<int> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_map<int, X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_map<int, X> const >));

BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_multimap<int, X> >));
BOOST_TEST_TRAIT_FALSE((has_constant_size< std::unordered_multimap<int, X> const >));

BOOST_TEST_TRAIT_TRUE((has_constant_size< boost::array<X, 2> >));
BOOST_TEST_TRAIT_TRUE((has_constant_size< boost::array<X, 2> const >));

BOOST_TEST_TRAIT_TRUE((has_constant_size< boost::array<X, 0> >));
BOOST_TEST_TRAIT_TRUE((has_constant_size< boost::array<X, 0> const >));

return boost::report_errors();
}

0 comments on commit 8848891

Please sign in to comment.