Skip to content

Commit

Permalink
fix 2968, 3005, 2994 (#3304)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywqzzy authored Nov 2, 2021
1 parent 53fe479 commit b6a6170
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 47 deletions.
4 changes: 2 additions & 2 deletions libs/libcommon/include/common/demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ inline std::string demangle(const char * name)
struct FreeingDeleter
{
template <typename PointerType>
void operator() (PointerType ptr)
void operator()(PointerType ptr)
{
std::free(ptr);
}
};

typedef std::unique_ptr<char, FreeingDeleter> DemangleResult;
using DemangleResult = std::unique_ptr<char, FreeingDeleter>;

DemangleResult tryDemangle(const char * name);
73 changes: 41 additions & 32 deletions libs/libcommon/include/ext/enumerate.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <ext/size.h>
#include <iterator>
#include <type_traits>
#include <utility>
#include <iterator>


/** \brief Provides a wrapper view around a container, allowing to iterate over it's elements and indices.
Expand All @@ -15,46 +15,55 @@
*/
namespace ext
{
template <typename It> struct enumerate_iterator
{
using traits = typename std::iterator_traits<It>;
using iterator_category = typename traits::iterator_category;
using value_type = std::pair<const std::size_t, typename traits::value_type>;
using difference_type = typename traits::difference_type;
using reference = std::pair<const std::size_t, typename traits::reference>;
template <typename It>
struct EnumerateIterator
{
using traits = typename std::iterator_traits<It>;
using iterator_category = typename traits::iterator_category;
using value_type = std::pair<const std::size_t, typename traits::value_type>;
using difference_type = typename traits::difference_type;
using reference = std::pair<const std::size_t, typename traits::reference>;

std::size_t idx;
It it;
std::size_t idx;
It it;

enumerate_iterator(const std::size_t idx, It it) : idx{idx}, it{it} {}
EnumerateIterator(const std::size_t idx, It it)
: idx{idx}
, it{it}
{}

auto operator*() const { return reference(idx, *it); }
auto operator*() const { return reference(idx, *it); }

bool operator!=(const enumerate_iterator & other) const { return it != other.it; }
bool operator!=(const EnumerateIterator & other) const { return it != other.it; }

enumerate_iterator & operator++() { return ++idx, ++it, *this; }
};
EnumerateIterator & operator++() { return ++idx, ++it, *this; }
};

template <typename Collection> struct enumerate_wrapper
{
using underlying_iterator = decltype(std::begin(std::declval<Collection &>()));
using iterator = enumerate_iterator<underlying_iterator>;
template <typename Collection>
struct EnumerateWrapper
{
using underlying_iterator = decltype(std::begin(std::declval<Collection &>()));
using iterator = EnumerateIterator<underlying_iterator>;

Collection & collection;
Collection & collection;

enumerate_wrapper(Collection & collection) : collection(collection) {}
explicit EnumerateWrapper(Collection & collection)
: collection(collection)
{}

auto begin() { return iterator(0, std::begin(collection)); }
auto end() { return iterator(ext::size(collection), std::end(collection)); }
};
auto begin() { return iterator(0, std::begin(collection)); }
auto end() { return iterator(ext::size(collection), std::end(collection)); }
};

template <typename Collection> auto enumerate(Collection & collection)
{
return enumerate_wrapper<Collection>{collection};
}
template <typename Collection>
auto enumerate(Collection & collection)
{
return EnumerateWrapper<Collection>{collection};
}

template <typename Collection> auto enumerate(const Collection & collection)
{
return enumerate_wrapper<const Collection>{collection};
}
template <typename Collection>
auto enumerate(const Collection & collection)
{
return EnumerateWrapper<const Collection>{collection};
}
} // namespace ext
39 changes: 26 additions & 13 deletions libs/libcommon/include/ext/identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@

namespace ext
{
/// \brief Identity function for use with other algorithms as a pass-through.
class identity
{
/** \brief Function pointer type template for converting identity to a function pointer.
/// \brief Identity function for use with other algorithms as a pass-through.
class Identity
{
/** \brief Function pointer type template for converting identity to a function pointer.
* Presumably useless, provided for completeness. */
template <typename T> using function_ptr_t = T &&(*)(T &&);
template <typename T>
using function_ptr_t = T && (*)(T &&);

/** \brief Implementation of identity as a non-instance member function for taking function pointer. */
template <typename T> static T && invoke(T && t) { return std::forward<T>(t); }
/** \brief Implementation of identity as a non-instance member function for taking function pointer. */
template <typename T>
static T && invoke(T && t)
{
return std::forward<T>(t);
}

public:
/** \brief Returns the value passed as a sole argument using perfect forwarding. */
template <typename T> T && operator()(T && t) const { return std::forward<T>(t); }
public:
/** \brief Returns the value passed as a sole argument using perfect forwarding. */
template <typename T>
T && operator()(T && t) const
{
return std::forward<T>(t);
}

/** \brief Allows conversion of identity instance to a function pointer. */
template <typename T> operator function_ptr_t<T>() const { return &invoke; };
/** \brief Allows conversion of identity instance to a function pointer. */
template <typename T>
explicit operator function_ptr_t<T>() const
{
return &invoke;
};
}
};
} // namespace ext

0 comments on commit b6a6170

Please sign in to comment.