exception: refactoring

This commit is contained in:
Maxime Coste 2011-09-09 18:40:59 +00:00
parent 0676eaec5c
commit 3caf962110
9 changed files with 95 additions and 44 deletions

View File

@ -1,14 +1,15 @@
#include "buffer_manager.hh" #include "buffer_manager.hh"
#include <cassert>
#include "buffer.hh" #include "buffer.hh"
#include "window.hh" #include "window.hh"
#include <cassert> #include "exception.hh"
namespace Kakoune namespace Kakoune
{ {
struct name_not_unique {}; struct name_not_unique : logic_error {};
BufferManager* BufferManager::ms_instance = nullptr; BufferManager* BufferManager::ms_instance = nullptr;

View File

@ -37,10 +37,10 @@ static std::vector<std::string> split(const std::string& line)
return result; return result;
} }
struct command_not_found : public std::runtime_error struct command_not_found : runtime_error
{ {
command_not_found(const std::string& what) command_not_found(const std::string& command)
: std::runtime_error("command not found: " + what) {} : runtime_error(command + " : no such command") {}
}; };
void CommandManager::execute(const std::string& command_line) void CommandManager::execute(const std::string& command_line)

View File

@ -4,17 +4,17 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include <stdexcept>
#include <functional> #include <functional>
#include "exception.hh"
namespace Kakoune namespace Kakoune
{ {
struct wrong_argument_count : public std::runtime_error struct wrong_argument_count : runtime_error
{ {
wrong_argument_count() wrong_argument_count() : runtime_error("wrong argument count") {}
: std::runtime_error("wrong argument count") {}
}; };
typedef std::vector<std::string> CommandParameters; typedef std::vector<std::string> CommandParameters;

14
src/exception.cc Normal file
View File

@ -0,0 +1,14 @@
#include "exception.hh"
#include <string>
#include <typeinfo>
namespace Kakoune
{
std::string exception::description() const
{
return typeid(*this).name();
}
}

32
src/exception.hh Normal file
View File

@ -0,0 +1,32 @@
#ifndef exception_hh_INCLUDED
#define exception_hh_INCLUDED
#include <string>
namespace Kakoune
{
struct exception
{
virtual ~exception() {}
virtual std::string description() const;
};
struct runtime_error : exception
{
runtime_error(const std::string description)
: m_description(description) {}
std::string description() const { return m_description; }
private:
std::string m_description;
};
struct logic_error : exception
{
};
}
#endif // exception_hh_INCLUDED

View File

@ -1,5 +1,7 @@
#include "file.hh" #include "file.hh"
#include "buffer.hh" #include "buffer.hh"
#include "buffer_manager.hh"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -16,9 +18,9 @@ Buffer* create_buffer_from_file(const std::string& filename)
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
throw file_not_found(strerror(errno)); throw file_not_found(filename);
throw open_file_error(strerror(errno)); throw file_access_error(filename, strerror(errno));
} }
std::string content; std::string content;
@ -32,6 +34,10 @@ Buffer* create_buffer_from_file(const std::string& filename)
content += std::string(buf, size); content += std::string(buf, size);
} }
close(fd); close(fd);
if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
BufferManager::instance().delete_buffer(buffer);
return new Buffer(filename, content); return new Buffer(filename, content);
} }
@ -39,7 +45,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
{ {
int fd = open(filename.c_str(), O_CREAT | O_WRONLY, 0644); int fd = open(filename.c_str(), O_CREAT | O_WRONLY, 0644);
if (fd == -1) if (fd == -1)
throw open_file_error(strerror(errno)); throw file_access_error(filename, strerror(errno));
const BufferString& content = buffer.content(); const BufferString& content = buffer.content();
ssize_t count = content.length() * sizeof(BufferChar); ssize_t count = content.length() * sizeof(BufferChar);
@ -52,7 +58,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
count -= written; count -= written;
if (written == -1) if (written == -1)
throw write_file_error(strerror(errno)); throw file_access_error(filename, strerror(errno));
} }
close(fd); close(fd);
} }

View File

@ -2,27 +2,24 @@
#define file_hh_INCLUDED #define file_hh_INCLUDED
#include <string> #include <string>
#include <stdexcept>
#include "exception.hh"
namespace Kakoune namespace Kakoune
{ {
struct open_file_error : public std::runtime_error struct file_access_error : runtime_error
{ {
open_file_error(const std::string& what) public:
: std::runtime_error(what) {} file_access_error(const std::string& filename,
const std::string& error_desc)
: runtime_error(filename + ": " + error_desc) {}
}; };
struct file_not_found : public open_file_error struct file_not_found : file_access_error
{ {
file_not_found(const std::string& what) file_not_found(const std::string& filename)
: open_file_error(what) {} : file_access_error(filename, "file not found") {}
};
struct write_file_error : public std::runtime_error
{
write_file_error(const std::string& what)
: std::runtime_error(what) {}
}; };
class Buffer; class Buffer;

View File

@ -216,10 +216,6 @@ void do_command()
command_manager.execute(prompt(":")); command_manager.execute(prompt(":"));
} }
catch (prompt_aborted&) {} catch (prompt_aborted&) {}
catch (std::runtime_error& err)
{
print_status(err.what());
}
} }
bool is_blank(char c) bool is_blank(char c)
@ -270,7 +266,6 @@ void do_search(Window& window)
std::string ex = prompt("/"); std::string ex = prompt("/");
window.select(false, RegexSelector(ex)); window.select(false, RegexSelector(ex));
} }
catch (boost::regex_error&) {}
catch (prompt_aborted&) {} catch (prompt_aborted&) {}
} }
@ -313,18 +308,25 @@ int main()
int count = 0; int count = 0;
while(not quit_requested) while(not quit_requested)
{ {
char c = getch(); try
if (isdigit(c))
count = count * 10 + c - '0';
else
{ {
if (keymap.find(c) != keymap.end()) char c = getch();
if (isdigit(c))
count = count * 10 + c - '0';
else
{ {
keymap[c](*current_window, count); if (keymap.find(c) != keymap.end())
draw_window(*current_window); {
keymap[c](*current_window, count);
draw_window(*current_window);
}
count = 0;
} }
count = 0; }
catch (Kakoune::runtime_error& error)
{
print_status(error.description());
} }
} }
deinit_ncurses(); deinit_ncurses();

View File

@ -1,6 +1,5 @@
#include "regex_selector.hh" #include "regex_selector.hh"
#include "exception.hh"
void print_status(const std::string&);
namespace Kakoune namespace Kakoune
{ {
@ -21,7 +20,7 @@ Selection RegexSelector::operator()(const BufferIterator& cursor) const
} }
catch (boost::regex_error& err) catch (boost::regex_error& err)
{ {
print_status("regex error"); throw runtime_error("regex error");
} }
return Selection(cursor, cursor); return Selection(cursor, cursor);