ranges: Add transform overload taking directly a pointer to member

This overload will forward to the general transform implementation
using std::mem_fn to generate a callable.
This commit is contained in:
Maxime Coste 2018-03-13 14:24:03 +11:00
parent c82bf31378
commit a480e566dc
12 changed files with 21 additions and 14 deletions

View File

@ -538,10 +538,10 @@ Completions CommandManager::complete_command_name(const Context& context, String
{
auto commands = m_commands
| filter([](const CommandMap::Item& cmd) { return not (cmd.value.flags & CommandFlags::Hidden); })
| transform(std::mem_fn(&CommandMap::Item::key));
| transform(&CommandMap::Item::key);
auto aliases = context.aliases().flatten_aliases()
| transform(std::mem_fn(&HashItem<String, String>::key))
| transform(&HashItem<String, String>::key)
| filter([](auto& alias) { return alias.length() > 3; });
return {0, query.length(), Kakoune::complete(query, query.length(), concatenated(commands, aliases))};

View File

@ -669,7 +669,7 @@ Completions highlighter_cmd_completer(
else if (add and token_to_complete == 1)
{
StringView name = params[1];
return { 0_byte, name.length(), complete(name, pos_in_token, HighlighterRegistry::instance() | transform(std::mem_fn(&HighlighterRegistry::Item::key))) };
return { 0_byte, name.length(), complete(name, pos_in_token, HighlighterRegistry::instance() | transform(&HighlighterRegistry::Item::key)) };
}
else
return {};
@ -1593,7 +1593,7 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (*bufnames == "*")
{
for (auto&& buffer : BufferManager::instance()
| transform(std::mem_fn(&std::unique_ptr<Buffer>::get))
| transform(&std::unique_ptr<Buffer>::get)
| filter([](Buffer* buf) { return not (buf->flags() & Buffer::Flags::Debug); })
| gather<Vector<SafePtr<Buffer>>>()) // gather as we might be mutating the buffer list in the loop.
context_wrap_for_buffer(*buffer);

View File

@ -123,7 +123,7 @@ CandidateList FaceRegistry::complete_alias_name(StringView prefix,
ByteCount cursor_pos) const
{
return complete(prefix, cursor_pos,
m_aliases | transform(std::mem_fn(&AliasMap::Item::key)));
m_aliases | transform(&AliasMap::Item::key));
}
FaceRegistry::FaceRegistry()

View File

@ -1869,7 +1869,7 @@ public:
return offset_pos(hl.complete_child(path.substr(offset), cursor_pos - offset, group), offset);
}
auto container = m_groups | transform(std::mem_fn(&decltype(m_groups)::Item::key));
auto container = m_groups | transform(&decltype(m_groups)::Item::key);
return { 0, 0, complete(path, cursor_pos, container) };
}

View File

@ -90,7 +90,7 @@ static const EnvVarDesc builtin_env_vars[] = { {
"buflist", false,
[](StringView name, const Context& context)
{ return join(BufferManager::instance() |
transform(std::mem_fn(&Buffer::display_name)), ':'); }
transform(&Buffer::display_name), ':'); }
}, {
"buf_line_count", false,
[](StringView name, const Context& context) -> String

View File

@ -731,7 +731,7 @@ void NCursesUI::menu_show(ConstArrayView<DisplayLine> items,
const int item_count = items.size();
m_menu.items.clear(); // make sure it is empty
m_menu.items.reserve(item_count);
const auto longest = accumulate(items | transform(std::mem_fn(&DisplayLine::length)),
const auto longest = accumulate(items | transform(&DisplayLine::length),
1_col, [](auto&& lhs, auto&& rhs) { return std::max(lhs, rhs); });
const bool is_prompt = style == MenuStyle::Prompt;
@ -945,7 +945,7 @@ void NCursesUI::info_show(StringView title, StringView content,
}
const DisplayCoord size{(int)info_box.size(),
accumulate(info_box | transform(std::mem_fn(&String::column_length)), 0_col,
accumulate(info_box | transform(&String::column_length), 0_col,
[](auto&& lhs, auto&& rhs){ return std::max(lhs, rhs); })};
const Rect rect = {m_status_on_top ? 1_line : 0_line, m_dimensions};
DisplayCoord pos;

View File

@ -1200,7 +1200,7 @@ void select_object(Context& context, NormalParams params)
{ 'n', select_number },
{ 'u', select_argument },
};
auto obj_it = find(selectors | transform(std::mem_fn(&ObjectType::key)), key).base();
auto obj_it = find(selectors | transform(&ObjectType::key), key).base();
if (obj_it != std::end(selectors))
return select_and_set_last<mode>(
context, std::bind(obj_it->func, _1, _2, count, flags));

View File

@ -109,7 +109,7 @@ CandidateList OptionsRegistry::complete_option_name(StringView prefix,
return complete(prefix, cursor_pos, m_descs |
filter([](const OptionPtr& desc)
{ return not (desc->flags() & OptionFlags::Hidden); }) |
transform(std::mem_fn(&OptionDesc::name)));
transform(&OptionDesc::name));
}
}

View File

@ -100,7 +100,7 @@ public:
static const OptionMap empty;
auto& parent = m_parent ? m_parent->m_options : empty;
auto& grand_parent = (m_parent and m_parent->m_parent) ? m_parent->m_parent->m_options : empty;
return merge(merge(grand_parent, parent), m_options) | transform(std::mem_fn(&OptionMap::Item::value));
return merge(merge(grand_parent, parent), m_options) | transform(&OptionMap::Item::value);
}
void register_watcher(OptionManagerWatcher& watcher) const;

View File

@ -30,7 +30,7 @@ option_type_name(Meta::Type<Enum>)
{
return format("{}({})", with_bit_ops(Meta::Type<Enum>{}) ? "flags" : "enum",
join(enum_desc(Meta::Type<Enum>{}) |
transform(std::mem_fn(&EnumDesc<Enum>::name)), '|'));
transform(&EnumDesc<Enum>::name), '|'));
}
inline String option_to_string(int opt) { return to_string(opt); }

View File

@ -5,6 +5,7 @@
#include <utility>
#include <iterator>
#include <numeric>
#include <functional>
#include "constexpr_utils.hh"
@ -161,6 +162,12 @@ inline auto transform(Transform t)
});
}
template<typename M, typename T>
inline auto transform(M T::*m)
{
return transform(std::mem_fn(std::forward<decltype(m)>(m)));
}
template<typename Range, bool escape = false,
typename Element = ValueOf<Range>,
typename ValueTypeParam = void>

View File

@ -310,7 +310,7 @@ CandidateList ShellManager::complete_env_var(StringView prefix,
ByteCount cursor_pos) const
{
return complete(prefix, cursor_pos,
m_env_vars | transform(std::mem_fn(&EnvVarDesc::str)));
m_env_vars | transform(&EnvVarDesc::str));
}
}