ArrayView content is not const anymore

As in upcoming std c++ array_view, ArrayView<T> points to mutable
data, use ArrayView<const T> or alias ConstArrayView<T> for const
data.
This commit is contained in:
Maxime Coste 2015-03-09 13:48:41 +00:00
parent 44f81d0b8b
commit 1cec8df45e
29 changed files with 82 additions and 79 deletions

View File

@ -3,6 +3,7 @@
#include <vector>
#include <initializer_list>
#include <type_traits>
namespace Kakoune
{
@ -18,42 +19,42 @@ public:
constexpr ArrayView()
: m_pointer(nullptr), m_size(0) {}
constexpr ArrayView(const T& oneval)
constexpr ArrayView(T& oneval)
: m_pointer(&oneval), m_size(1) {}
constexpr ArrayView(const T* pointer, size_t size)
constexpr ArrayView(T* pointer, size_t size)
: m_pointer(pointer), m_size(size) {}
constexpr ArrayView(const T* begin, const T* end)
constexpr ArrayView(T* begin, T* end)
: m_pointer(begin), m_size(end - begin) {}
template<size_t N>
constexpr ArrayView(const T(&array)[N]) : m_pointer(array), m_size(N) {}
constexpr ArrayView(T(&array)[N]) : m_pointer(array), m_size(N) {}
template<typename Iterator>
constexpr ArrayView(const Iterator& begin, const Iterator& end)
: m_pointer(&(*begin)), m_size(end - begin) {}
template<typename Alloc>
constexpr ArrayView(const std::vector<T, Alloc>& v)
template<typename Alloc, typename U>
constexpr ArrayView(const std::vector<U, Alloc>& v)
: m_pointer(&v[0]), m_size(v.size()) {}
constexpr ArrayView(const std::initializer_list<T>& v)
: m_pointer(v.begin()), m_size(v.size()) {}
constexpr const T* pointer() const { return m_pointer; }
constexpr T* pointer() const { return m_pointer; }
constexpr size_t size() const { return m_size; }
constexpr const T& operator[](size_t n) const { return *(m_pointer + n); }
constexpr T& operator[](size_t n) const { return *(m_pointer + n); }
constexpr const T* begin() const { return m_pointer; }
constexpr const T* end() const { return m_pointer+m_size; }
constexpr T* begin() const { return m_pointer; }
constexpr T* end() const { return m_pointer+m_size; }
using reverse_iterator = std::reverse_iterator<const T*>;
using reverse_iterator = std::reverse_iterator<T*>;
constexpr reverse_iterator rbegin() const { return reverse_iterator(m_pointer+m_size); }
constexpr reverse_iterator rend() const { return reverse_iterator(m_pointer); }
constexpr const T& front() const { return *m_pointer; }
constexpr const T& back() const { return *(m_pointer + m_size - 1); }
constexpr T& front() const { return *m_pointer; }
constexpr T& back() const { return *(m_pointer + m_size - 1); }
constexpr bool empty() const { return m_size == 0; }
@ -63,10 +64,13 @@ public:
}
private:
const T* m_pointer;
T* m_pointer;
size_t m_size;
};
template<typename T>
using ConstArrayView = ArrayView<const T>;
}
#endif // array_view_hh_INCLUDED

View File

@ -164,7 +164,7 @@ public:
ByteCoord begin;
ByteCoord end;
};
ArrayView<Change> changes_since(size_t timestamp) const;
ConstArrayView<Change> changes_since(size_t timestamp) const;
String debug_description() const;
private:

View File

@ -92,7 +92,7 @@ inline size_t Buffer::timestamp() const
return m_changes.size();
}
inline ArrayView<Buffer::Change> Buffer::changes_since(size_t timestamp) const
inline ConstArrayView<Buffer::Change> Buffer::changes_since(size_t timestamp) const
{
return { m_changes.data() + timestamp,
m_changes.data() + m_changes.size() };

View File

@ -270,11 +270,11 @@ TokenList parse(StringView line)
}
String eval_token(const Token& token, Context& context,
ArrayView<String> shell_params,
ConstArrayView<String> shell_params,
const EnvVarMap& env_vars);
String eval(StringView str, Context& context,
ArrayView<String> shell_params,
ConstArrayView<String> shell_params,
const EnvVarMap& env_vars)
{
String res;
@ -303,7 +303,7 @@ String eval(StringView str, Context& context,
}
String eval_token(const Token& token, Context& context,
ArrayView<String> shell_params,
ConstArrayView<String> shell_params,
const EnvVarMap& env_vars)
{
auto& content = token.content();
@ -356,7 +356,7 @@ void CommandManager::execute_single_command(CommandParameters params,
if (params.empty())
return;
ArrayView<String> param_view(params.begin()+1, params.end());
ConstArrayView<String> param_view(params.begin()+1, params.end());
auto command_it = find_command(context, params[0]);
if (command_it == m_commands.end())
throw command_not_found(params[0]);
@ -396,7 +396,7 @@ static CharCoord find_coord(StringView str, ByteCount offset)
void CommandManager::execute(StringView command_line,
Context& context,
ArrayView<String> shell_params,
ConstArrayView<String> shell_params,
const EnvVarMap& env_vars)
{
TokenList tokens = parse<true>(command_line);

View File

@ -18,7 +18,7 @@ namespace Kakoune
{
class Context;
using CommandParameters = ArrayView<String>;
using CommandParameters = ConstArrayView<String>;
using Command = std::function<void (const ParametersParser& parser, Context& context)>;
using CommandCompleter = std::function<Completions (const Context& context,
CompletionFlags,
@ -40,7 +40,7 @@ public:
using ArgumentCompleter = std::function<Completions (const Context&,
CompletionFlags flags,
const String&, ByteCount)>;
using ArgumentCompleterList = ArrayView<ArgumentCompleter>;
using ArgumentCompleterList = ConstArrayView<ArgumentCompleter>;
PerArgumentCommandCompleter(ArgumentCompleterList completers)
: m_completers(completers.begin(), completers.end()) {}
@ -61,7 +61,7 @@ class CommandManager : public Singleton<CommandManager>
{
public:
void execute(StringView command_line, Context& context,
ArrayView<String> shell_params = {},
ConstArrayView<String> shell_params = {},
const EnvVarMap& env_vars = EnvVarMap{});
Completions complete(const Context& context, CompletionFlags flags,

View File

@ -1267,7 +1267,7 @@ const CommandDesc prompt_cmd = {
{
if (event != PromptEvent::Validate)
return;
RegisterManager::instance()[reg] = ArrayView<String>(str);
RegisterManager::instance()[reg] = ConstArrayView<String>(str);
CommandManager::instance().execute(command, context);
});
@ -1449,7 +1449,7 @@ const CommandDesc set_register_cmd = {
CommandCompleter{},
[](const ParametersParser& parser, Context& context)
{
RegisterManager::instance()[parser[0]] = ArrayView<String>(parser[1]);
RegisterManager::instance()[parser[0]] = ConstArrayView<String>(parser[1]);
}
};
@ -1474,7 +1474,7 @@ public:
RegisterRestorer(char name, const Context& context)
: m_name(name)
{
ArrayView<String> save = RegisterManager::instance()[name].values(context);
ConstArrayView<String> save = RegisterManager::instance()[name].values(context);
m_save = Vector<String>(save.begin(), save.end());
}
@ -1488,7 +1488,7 @@ private:
}
void exec_keys(ArrayView<Key> keys, Context& context)
void exec_keys(ConstArrayView<Key> keys, Context& context)
{
RegisterRestorer quote('"', context);
RegisterRestorer slash('/', context);

View File

@ -10,7 +10,7 @@ namespace Kakoune
class Context;
void register_commands();
void exec_keys(ArrayView<Key> keys, Context& context);
void exec_keys(ConstArrayView<Key> keys, Context& context);
}

View File

@ -255,7 +255,7 @@ void write_buffer_to_backup_file(Buffer& buffer)
}
}
String find_file(StringView filename, ArrayView<String> paths)
String find_file(StringView filename, ConstArrayView<String> paths)
{
struct stat buf;
if (filename.length() > 1 and filename[0] == '/')

View File

@ -1,6 +1,7 @@
#ifndef file_hh_INCLUDED
#define file_hh_INCLUDED
#include "array_view.hh"
#include "completion.hh"
#include "exception.hh"
#include "regex.hh"
@ -23,7 +24,6 @@ struct file_not_found : file_access_error
};
class Buffer;
template<typename T> class ArrayView;
class String;
class StringView;
@ -43,7 +43,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename);
void write_buffer_to_fd(Buffer& buffer, int fd);
void write_buffer_to_backup_file(Buffer& buffer);
String find_file(StringView filename, ArrayView<String> paths);
String find_file(StringView filename, ConstArrayView<String> paths);
time_t get_fs_timestamp(StringView filename);

View File

@ -64,7 +64,7 @@ std::unique_ptr<SimpleHighlighter<T>> make_simple_highlighter(T func)
return make_unique<SimpleHighlighter<T>>(std::move(func));
}
using HighlighterParameters = ArrayView<String>;
using HighlighterParameters = ConstArrayView<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
struct HighlighterFactoryAndDocstring

View File

@ -822,7 +822,7 @@ void find_matches(const Buffer& buffer, RegexMatchList& matches, const Regex& re
}
}
void update_matches(const Buffer& buffer, ArrayView<LineModification> modifs,
void update_matches(const Buffer& buffer, ConstArrayView<LineModification> modifs,
RegexMatchList& matches, const Regex& regex)
{
// remove out of date matches and update line for others
@ -939,7 +939,7 @@ struct RegionDesc
}
void update_matches(const Buffer& buffer,
ArrayView<LineModification> modifs,
ConstArrayView<LineModification> modifs,
RegionMatches& matches) const
{
Kakoune::update_matches(buffer, modifs, matches.begin_matches, m_begin);

View File

@ -342,7 +342,7 @@ private:
class Menu : public InputMode
{
public:
Menu(InputHandler& input_handler, ArrayView<String> choices,
Menu(InputHandler& input_handler, ConstArrayView<String> choices,
MenuCallback callback)
: InputMode(input_handler),
m_callback(callback), m_choices(choices.begin(), choices.end()),
@ -460,7 +460,7 @@ private:
LineEditor m_filter_editor;
};
String common_prefix(ArrayView<String> strings)
String common_prefix(ConstArrayView<String> strings)
{
String res;
if (strings.empty())
@ -928,7 +928,7 @@ private:
selections.sort_and_merge_overlapping();
}
void insert(ArrayView<String> strings)
void insert(ConstArrayView<String> strings)
{
context().selections().insert(strings, InsertMode::InsertCursor);
}
@ -1096,7 +1096,7 @@ void InputHandler::set_prompt_face(Face prompt_face)
prompt->set_prompt_face(prompt_face);
}
void InputHandler::menu(ArrayView<String> choices,
void InputHandler::menu(ConstArrayView<String> choices,
MenuCallback callback)
{
change_input_mode(new InputModes::Menu(*this, choices, callback));
@ -1153,7 +1153,7 @@ bool InputHandler::is_recording() const
void InputHandler::stop_recording()
{
kak_assert(m_recording_reg != 0);
RegisterManager::instance()[m_recording_reg] = ArrayView<String>(m_recorded_keys);
RegisterManager::instance()[m_recording_reg] = ConstArrayView<String>(m_recorded_keys);
m_recording_reg = 0;
}

View File

@ -61,7 +61,7 @@ public:
// abort or validation with corresponding MenuEvent value
// returns to normal mode after validation if callback does
// not change the mode itself
void menu(ArrayView<String> choices, MenuCallback callback);
void menu(ArrayView<const String> choices, MenuCallback callback);
// execute callback on next keypress and returns to normal mode
// if callback does not change the mode itself

View File

@ -23,7 +23,7 @@ bool KeymapManager::is_mapped(Key key, KeymapMode mode) const
(m_parent and m_parent->is_mapped(key, mode));
}
ArrayView<Key> KeymapManager::get_mapping(Key key, KeymapMode mode) const
ConstArrayView<Key> KeymapManager::get_mapping(Key key, KeymapMode mode) const
{
auto it = m_mapping.find({key, mode});
if (it != m_mapping.end())

View File

@ -1,6 +1,7 @@
#ifndef keymap_manager_hh_INCLUDED
#define keymap_manager_hh_INCLUDED
#include "array_view.hh"
#include "keys.hh"
#include "hash.hh"
#include "unordered_map.hh"
@ -21,8 +22,6 @@ enum class KeymapMode : int
User,
};
template<typename T> class ArrayView;
class KeymapManager
{
public:
@ -33,7 +32,7 @@ public:
void unmap_key(Key key, KeymapMode mode);
bool is_mapped(Key key, KeymapMode mode) const;
ArrayView<Key> get_mapping(Key key, KeymapMode mode) const;
ConstArrayView<Key> get_mapping(Key key, KeymapMode mode) const;
private:
KeymapManager()
: m_parent(nullptr) {}

View File

@ -314,7 +314,7 @@ int run_client(StringView session, StringView init_command)
}
int run_server(StringView session, StringView init_command,
bool ignore_kakrc, bool daemon, ArrayView<StringView> files)
bool ignore_kakrc, bool daemon, ConstArrayView<StringView> files)
{
static bool terminate = false;
if (daemon)
@ -416,7 +416,7 @@ int run_server(StringView session, StringView init_command,
return 0;
}
int run_filter(StringView keystr, ArrayView<StringView> files, bool quiet)
int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
{
StringRegistry string_registry;
GlobalScope global_scope;

View File

@ -564,7 +564,7 @@ void NCursesUI::draw_menu()
m_dirty = true;
}
void NCursesUI::menu_show(ArrayView<String> items,
void NCursesUI::menu_show(ConstArrayView<String> items,
CharCoord anchor, Face fg, Face bg,
MenuStyle style)
{
@ -714,7 +714,7 @@ static CharCoord compute_pos(CharCoord anchor, CharCoord size,
}
String make_info_box(StringView title, StringView message, CharCount max_width,
ArrayView<StringView> assistant)
ConstArrayView<StringView> assistant)
{
CharCoord assistant_size;
if (not assistant.empty())
@ -851,7 +851,7 @@ void NCursesUI::set_ui_options(const Options& options)
else if (it->second == "clippy")
m_assistant = assistant_clippy;
else if (it->second == "none" or it->second == "off")
m_assistant = ArrayView<StringView>{};
m_assistant = ConstArrayView<StringView>{};
}
}

View File

@ -28,7 +28,7 @@ public:
bool is_key_available() override;
Key get_key() override;
void menu_show(ArrayView<String> items,
void menu_show(ConstArrayView<String> items,
CharCoord anchor, Face fg, Face bg,
MenuStyle style) override;
void menu_select(int selected) override;
@ -73,7 +73,7 @@ private:
InputCallback m_input_callback;
bool m_status_on_top = false;
ArrayView<StringView> m_assistant;
ConstArrayView<StringView> m_assistant;
bool m_dirty = false;
};

View File

@ -1079,7 +1079,7 @@ void replay_macro(Context& context, NormalParams params)
if (running_macros[idx])
throw runtime_error("recursive macros call detected");
ArrayView<String> reg_val = RegisterManager::instance()[name].values(context);
ConstArrayView<String> reg_val = RegisterManager::instance()[name].values(context);
if (not reg_val.empty())
{
running_macros[idx] = true;

View File

@ -109,7 +109,7 @@ struct TupleOptionDetail
tuple_separator + escape(option_to_string(std::get<I>(opt)), tuple_separator, '\\');
}
static void from_string(ArrayView<String> elems, std::tuple<Types...>& opt)
static void from_string(ConstArrayView<String> elems, std::tuple<Types...>& opt)
{
option_from_string(elems[I], std::get<I>(opt));
TupleOptionDetail<I-1, Types...>::from_string(elems, opt);
@ -124,7 +124,7 @@ struct TupleOptionDetail<0, Types...>
return option_to_string(std::get<0>(opt));
}
static void from_string(ArrayView<String> elems, std::tuple<Types...>& opt)
static void from_string(ConstArrayView<String> elems, std::tuple<Types...>& opt)
{
option_from_string(elems[0], std::get<0>(opt));
}

View File

@ -10,7 +10,7 @@
namespace Kakoune
{
using ParameterList = ArrayView<String>;
using ParameterList = ConstArrayView<String>;
struct parameter_error : public runtime_error
{

View File

@ -13,9 +13,9 @@ class Register
{
public:
virtual ~Register() {}
virtual Register& operator=(ArrayView<String> values) = 0;
virtual Register& operator=(ConstArrayView<String> values) = 0;
virtual ArrayView<String> values(const Context& context) = 0;
virtual ConstArrayView<String> values(const Context& context) = 0;
};
}

View File

@ -12,18 +12,18 @@ namespace Kakoune
class StaticRegister : public Register
{
public:
Register& operator=(ArrayView<String> values) override
Register& operator=(ConstArrayView<String> values) override
{
m_content = Vector<String, MemoryDomain::Registers>(values.begin(), values.end());
return *this;
}
ArrayView<String> values(const Context&) override
ConstArrayView<String> values(const Context&) override
{
if (m_content.empty())
return ArrayView<String>(ms_empty);
return ConstArrayView<String>(ms_empty);
else
return ArrayView<String>(m_content);
return ConstArrayView<String>(m_content);
}
protected:
Vector<String, MemoryDomain::Registers> m_content;
@ -41,12 +41,12 @@ public:
DynamicRegister(RegisterRetriever function)
: m_function(std::move(function)) {}
Register& operator=(ArrayView<String> values) override
Register& operator=(ConstArrayView<String> values) override
{
throw runtime_error("this register is not assignable");
}
ArrayView<String> values(const Context& context) override
ConstArrayView<String> values(const Context& context) override
{
m_content = m_function(context);
return StaticRegister::values(context);

View File

@ -68,7 +68,7 @@ public:
}
template<typename T>
void write(ArrayView<T> view)
void write(ConstArrayView<T> view)
{
write<uint32_t>(view.size());
for (auto& val : view)
@ -78,7 +78,7 @@ public:
template<typename T, MemoryDomain domain>
void write(const Vector<T, domain>& vec)
{
write(ArrayView<T>(vec));
write(ConstArrayView<T>(vec));
}
template<typename Key, typename Val, MemoryDomain domain>
@ -249,7 +249,7 @@ public:
RemoteUI(int socket);
~RemoteUI();
void menu_show(ArrayView<String> choices,
void menu_show(ConstArrayView<String> choices,
CharCoord anchor, Face fg, Face bg,
MenuStyle style) override;
void menu_select(int selected) override;
@ -298,7 +298,7 @@ RemoteUI::~RemoteUI()
m_socket_watcher.close_fd();
}
void RemoteUI::menu_show(ArrayView<String> choices,
void RemoteUI::menu_show(ConstArrayView<String> choices,
CharCoord anchor, Face fg, Face bg,
MenuStyle style)
{

View File

@ -196,7 +196,7 @@ const Buffer::Change* backward_sorted_until(const Buffer::Change* first, const B
return last;
}
void update_forward(ArrayView<Buffer::Change> changes, Vector<Selection>& selections, size_t& main)
void update_forward(ConstArrayView<Buffer::Change> changes, Vector<Selection>& selections, size_t& main)
{
ForwardChangesTracker changes_tracker;
@ -221,7 +221,7 @@ void update_forward(ArrayView<Buffer::Change> changes, Vector<Selection>& select
kak_assert(std::is_sorted(selections.begin(), selections.end(), compare_selections));
}
void update_backward(ArrayView<Buffer::Change> changes, Vector<Selection>& selections, size_t& main)
void update_backward(ConstArrayView<Buffer::Change> changes, Vector<Selection>& selections, size_t& main)
{
ForwardChangesTracker changes_tracker;
@ -463,7 +463,7 @@ BufferIterator prepare_insert(Buffer& buffer, const Selection& sel, InsertMode m
return {};
}
void SelectionList::insert(ArrayView<String> strings, InsertMode mode,
void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted)
{
if (strings.empty())

View File

@ -128,7 +128,7 @@ struct SelectionList
size_t timestamp() const { return m_timestamp; }
void update_timestamp() { m_timestamp = m_buffer->timestamp(); }
void insert(ArrayView<String> strings, InsertMode mode,
void insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted = false);
void erase();

View File

@ -29,7 +29,7 @@ ShellManager::ShellManager()
}
String ShellManager::eval(StringView cmdline, const Context& context,
ArrayView<String> params,
ConstArrayView<String> params,
const EnvVarMap& env_vars,
int* exit_status)
{
@ -38,7 +38,7 @@ String ShellManager::eval(StringView cmdline, const Context& context,
String ShellManager::pipe(StringView input,
StringView cmdline, const Context& context,
ArrayView<String> params,
ConstArrayView<String> params,
const EnvVarMap& env_vars,
int* exit_status)
{

View File

@ -1,6 +1,7 @@
#ifndef shell_manager_hh_INCLUDED
#define shell_manager_hh_INCLUDED
#include "array_view.hh"
#include "regex.hh"
#include "utils.hh"
#include "env_vars.hh"
@ -9,7 +10,6 @@ namespace Kakoune
{
class Context;
template<typename T> class ArrayView;
class String;
class StringView;
@ -21,13 +21,13 @@ public:
ShellManager();
String eval(StringView cmdline, const Context& context,
ArrayView<String> params,
ConstArrayView<String> params,
const EnvVarMap& env_vars,
int* exit_status = nullptr);
String pipe(StringView input,
StringView cmdline, const Context& context,
ArrayView<String> params,
ConstArrayView<String> params,
const EnvVarMap& env_vars,
int* exit_status = nullptr);

View File

@ -1,6 +1,7 @@
#ifndef user_interface_hh_INCLUDED
#define user_interface_hh_INCLUDED
#include "array_view.hh"
#include "safe_ptr.hh"
#include "unordered_map.hh"
@ -15,7 +16,6 @@ class DisplayLine;
struct CharCoord;
struct Face;
struct Key;
template<typename T> class ArrayView;
enum class MenuStyle
{
@ -41,7 +41,7 @@ class UserInterface : public SafeCountable
public:
virtual ~UserInterface() {}
virtual void menu_show(ArrayView<String> choices,
virtual void menu_show(ConstArrayView<String> choices,
CharCoord anchor, Face fg, Face bg,
MenuStyle style) = 0;
virtual void menu_select(int selected) = 0;