Misc cleanup in history_t

Add some C++11
This commit is contained in:
ridiculousfish 2018-07-14 11:47:34 -07:00
parent 2ce0240fc8
commit 63e0a3d7c2
3 changed files with 33 additions and 48 deletions

View File

@ -721,19 +721,7 @@ history_t &history_t::history_with_name(const wcstring &name) {
return histories.get_creating(name);
}
history_t::history_t(wcstring pname)
: name(std::move(pname)),
first_unwritten_new_item_index(0),
has_pending_item(false),
disable_automatic_save_counter(0),
mmap_start(NULL),
mmap_length(0),
mmap_type(history_file_type_t(-1)),
mmap_file_id(kInvalidFileID),
boundary_timestamp(time(NULL)),
countdown_to_vacuum(-1),
loaded_old(false),
chaos_mode(false) {}
history_t::history_t(wcstring pname) : name(std::move(pname)), boundary_timestamp(time(NULL)) {}
void history_t::add(const history_item_t &item, bool pending) {
scoped_lock locker(lock);
@ -849,11 +837,8 @@ void history_t::get_history(wcstring_list_t &result) {
bool next_is_pending = this->has_pending_item;
std::unordered_set<wcstring> seen;
// Append new items. Note that in principle we could use const_reverse_iterator, but we do not
// because reverse_iterator is not convertible to const_reverse_iterator. See
// https://github.com/fish-shell/fish-shell/issues/431.
for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter < new_items.rend();
++iter) {
// Append new items.
for (auto iter = new_items.crbegin(); iter < new_items.crend(); ++iter) {
// Skip a pending item if we have one.
if (next_is_pending) {
next_is_pending = false;
@ -865,8 +850,7 @@ void history_t::get_history(wcstring_list_t &result) {
// Append old items.
load_old_if_needed();
for (std::deque<size_t>::reverse_iterator iter = old_item_offsets.rbegin();
iter != old_item_offsets.rend(); ++iter) {
for (auto iter = old_item_offsets.crbegin(); iter != old_item_offsets.crend(); ++iter) {
size_t offset = *iter;
const history_item_t item =
decode_item(mmap_start + offset, mmap_length - offset, mmap_type);

View File

@ -134,29 +134,29 @@ class history_t {
history_item_list_t new_items;
// The index of the first new item that we have not yet written.
size_t first_unwritten_new_item_index;
size_t first_unwritten_new_item_index{0};
// Whether we have a pending item. If so, the most recently added item is ignored by
// item_at_index.
bool has_pending_item;
bool has_pending_item{false};
// Whether we should disable saving to the file for a time.
uint32_t disable_automatic_save_counter;
uint32_t disable_automatic_save_counter{0};
// Deleted item contents.
std::unordered_set<wcstring> deleted_items;
// The mmaped region for the history file.
const char *mmap_start;
const char *mmap_start{nullptr};
// The size of the mmap'd region.
size_t mmap_length;
size_t mmap_length{0};
// The type of file we mmap'd.
history_file_type_t mmap_type;
history_file_type_t mmap_type{history_file_type_t(-1)};
// The file ID of the file we mmap'd.
file_id_t mmap_file_id;
file_id_t mmap_file_id{kInvalidFileID};
// The boundary timestamp distinguishes old items from new items. Items whose timestamps are <=
// the boundary are considered "old". Items whose timestemps are > the boundary are new, and are
@ -165,22 +165,26 @@ class history_t {
time_t boundary_timestamp;
// How many items we add until the next vacuum. Initially a random value.
int countdown_to_vacuum;
int countdown_to_vacuum{-1};
// Figure out the offsets of our mmap data.
void populate_from_mmap(void);
// Whether we've loaded old items.
bool loaded_old{false};
// List of old items, as offsets into out mmap data.
std::deque<size_t> old_item_offsets;
// Whether we've loaded old items.
bool loaded_old;
// Whether we're in maximum chaos mode, useful for testing.
// This causes things like locks to fail.
bool chaos_mode{false};
// Loads old if necessary.
bool load_old_if_needed(void);
// Figure out the offsets of our mmap data.
void populate_from_mmap();
// Memory maps the history file if necessary.
bool mmap_if_needed(void);
// Loads old items if necessary.
bool load_old_if_needed();
// Reads the history file if necessary.
bool mmap_if_needed();
// Deletes duplicates in new_items.
void compact_new_items();
@ -209,9 +213,6 @@ class history_t {
// Like map_file but takes a file descriptor
bool map_fd(int fd, const char **out_map_start, size_t *out_map_len) const;
// Whether we're in maximum chaos mode, useful for testing.
bool chaos_mode;
// Implementation of item_at_index and items_at_indexes
history_item_t item_at_index_assume_locked(size_t idx);
@ -223,7 +224,7 @@ class history_t {
// Determines whether the history is empty. Unfortunately this cannot be const, since it may
// require populating the history.
bool is_empty(void);
bool is_empty();
// Add a new history item to the end. If pending is set, the item will not be returned by
// item_at_index until a call to resolve_pending(). Pending items are tracked with an offset
@ -320,25 +321,25 @@ class history_search_t {
void skip_matches(const wcstring_list_t &skips);
// Finds the next search term (forwards in time). Returns true if one was found.
bool go_forwards(void);
bool go_forwards();
// Finds the previous search result (backwards in time). Returns true if one was found.
bool go_backwards(void);
bool go_backwards();
// Goes to the end (forwards).
void go_to_end(void);
void go_to_end();
// Returns if we are at the end. We start out at the end.
bool is_at_end(void) const;
bool is_at_end() const;
// Goes to the beginning (backwards).
void go_to_beginning(void);
void go_to_beginning();
// Returns the current search result item. asserts if there is no current item.
history_item_t current_item(void) const;
history_item_t current_item() const;
// Returns the current search result item contents. asserts if there is no current item.
wcstring current_string(void) const;
wcstring current_string() const;
// Constructor.
history_search_t(history_t &hist, const wcstring &str,

View File

@ -101,7 +101,7 @@ void reader_run_command(const wcstring &buff);
const wchar_t *reader_get_buffer();
/// Returns the current reader's history.
history_t *reader_get_history(void);
history_t *reader_get_history();
/// Set the string of characters in the command buffer, as well as the cursor position.
///