X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/595e59c568ff5f8510de201bfd800951cdc2adcb..ace6ec5d4b81b85275732c9ba244d358ddc30107:/include/xbt/utility.hpp diff --git a/include/xbt/utility.hpp b/include/xbt/utility.hpp index 095130f8a1..c35be72d7f 100644 --- a/include/xbt/utility.hpp +++ b/include/xbt/utility.hpp @@ -1,80 +1,83 @@ -/* Copyright (c) 2016. The SimGrid Team. +/* Copyright (c) 2016-2023. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include - -namespace simgrid { -namespace xbt { +#ifndef XBT_UTILITY_HPP +#define XBT_UTILITY_HPP -// integer_sequence and friends from C++14 -// We need them to implement `apply` from C++17. +#include +#include +#include +#include +#include -/** A compile-time sequence of integers (from C++14) +/** @brief Helper macro to declare enum class * - * `index_sequence` represents the sequence `(1,5,7,9)`. - * - * @code{.cpp} - * template - * auto extract_tuple(T&& t, integer_sequence) - * -> decltype(std::make_tuple(std::get(std::forward(t))...)) - * { - * return std::make_tuple(std::get(std::forward(t))...); - * } - * - * int main() - * { - * integer_sequence seq; - * auto a = std::make_tuple(1, 2.0, false, 'a'); - * auto b = extract_tuple(a, seq); - * std::cout << std::get<0>(b) << '\n'; // 2 - * std::cout << std::get<1>(b) << '\n'; // a - * return 0; - * } - * @endcode + * Declares an enum class EnumType, and a function "const char* to_c_str(EnumType)" to retrieve a C-string description + * for each value. */ -template -class integer_sequence { - static constexpr std::size_t size() - { - return std::tuple_size::value; - } -}; +#define XBT_DECLARE_ENUM_CLASS(EnumType, ...) \ + enum class EnumType; \ + static constexpr char const* to_c_str(EnumType value) \ + { \ + constexpr std::array names{{_XBT_STRINGIFY_ARGS(__VA_ARGS__)}}; \ + return names.at(static_cast(value)); \ + } \ + static constexpr bool is_valid_##EnumType(int raw_value) \ + { \ + return raw_value >= 0 && raw_value < _XBT_COUNT_ARGS(__VA_ARGS__); \ + } \ + enum class EnumType { __VA_ARGS__ } /* defined here to handle trailing semicolon */ -namespace bits { - template - struct make_integer_sequence : - make_integer_sequence - {}; - template - struct make_integer_sequence { - typedef integer_sequence type; - }; -} +namespace simgrid::xbt { -/** A compile-time sequence of integers of the form `(0,1,2,3,...,N-1)` (from C++14) */ -template -using make_integer_sequence = typename simgrid::xbt::bits::make_integer_sequence::type; +/** @brief Replacement for C++20's std::type_identity_t + */ +#if __cplusplus >= 201806L // __cpp_lib_type_identity +template using type_identity_t = typename std::type_identity_t; +#else +template struct type_identity { + using type = T; +}; -/** A compile-time sequence of indices (from C++14) */ -template -using index_sequence = integer_sequence; +template using type_identity_t = typename type_identity::type; +#endif -/** A compile-time sequence of indices of the form `(0,1,2,3,...,N-1)` (from C++14) */ -template -using make_index_sequence = make_integer_sequence; +/** @brief A hash which works with more stuff + * + * It can hash pairs: the standard hash currently doesn't include this. + */ +template class hash : public std::hash { +}; -/** Convert a type parameter pack into a index_sequence (from C++14) */ -template -using index_sequence_for = make_index_sequence; +template class hash> { +public: + std::size_t operator()(std::pair const& x) const + { + hash h1; + hash h2; + return h1(x.first) ^ h2(x.second); + } +}; -static_assert(std::is_same< make_index_sequence<0>, index_sequence<> >::value, "seq0"); -static_assert(std::is_same< make_index_sequence<1>, index_sequence<0> >::value, "seq1"); -static_assert(std::is_same< make_index_sequence<2>, index_sequence<0, 1> >::value, "seq2"); -static_assert(std::is_same< make_index_sequence<3>, index_sequence<0, 1, 2> >::value, "seq3"); -static_assert(std::is_same< index_sequence_for, make_index_sequence<3> >::value, "seq4"); +/** @brief Comparator class for using with std::priority_queue or boost::heap. + * + * Compare two std::pair by their first element (of type double), and return true when the first is greater than the + * second. Useful to have priority queues with the smallest element on top. + */ +template class HeapComparator { +public: + bool operator()(const Pair& a, const Pair& b) const { return a.first > b.first; } +}; +/** @brief Erase an element given by reference from a boost::intrusive::list. + */ +template inline void intrusive_erase(List& list, Elem& elem) +{ + list.erase(list.iterator_to(elem)); } -} + +} // namespace simgrid::xbt +#endif