susumu.yata
null+****@clear*****
Thu Oct 30 18:43:20 JST 2014
susumu.yata 2014-10-30 18:43:20 +0900 (Thu, 30 Oct 2014) New Revision: b395092c10a8057b28589bb2a2b3e21c4485c0fe https://github.com/groonga/grnxx/commit/b395092c10a8057b28589bb2a2b3e21c4485c0fe Message: Enable Error. Added files: include/grnxx/error.hpp Modified files: include/grnxx/Makefile.am lib/grnxx/Makefile.am lib/grnxx/error.cpp Modified: include/grnxx/Makefile.am (+1 -0) =================================================================== --- include/grnxx/Makefile.am 2014-10-30 18:39:08 +0900 (bbf1912) +++ include/grnxx/Makefile.am 2014-10-30 18:43:20 +0900 (d491d1a) @@ -3,6 +3,7 @@ SUBDIRS = \ features pkginclude_HEADERS = \ + error.hpp \ features.hpp \ library.hpp \ memory.hpp \ Added: include/grnxx/error.hpp (+104 -0) 100644 =================================================================== --- /dev/null +++ include/grnxx/error.hpp 2014-10-30 18:43:20 +0900 (1227470) @@ -0,0 +1,104 @@ +#ifndef GRNXX_ERROR_HPP +#define GRNXX_ERROR_HPP + +#include <cstddef> + +namespace grnxx { + +enum ErrorCode { + NO_ERROR, // No error occurred. + NOT_FOUND, // The target does not found. + ALREADY_EXISTS, // The target already exists. + NOT_REMOVABLE, // The target is not removable. + BROKEN, // The database is broken. + NO_MEMORY, // Memory allocation failed. + INVALID_NAME, // The string is invalid as an object name. + NO_KEY_COLUMN, // The table has no key column. + INVALID_ARGUMENT, // Invalid argument. + INVALID_OPERATION, // Invalid operation. + INVALID_OPERAND, // Invalid operand. + DIVISION_BY_ZERO, // Integer division by zero. + DIVISION_OVERFLOW, // Overflow in integer division. + NOT_SUPPORTED_YET // The operation is not supported yet. +}; + +// Many functions take a pointer to an Error object as the first argument +// (except an implicit "this" argument) for returning error information on +// failure. On success, the Error object will not be modified. If the pointer +// is nullptr, the error information will not be generated even on failure. +class Error { + public: + // The error code of an initialized Error object is NO_ERROR. + Error() + : code_(NO_ERROR), + line_(0), + file_(""), + function_(""), + message_() { + message_[0] = '\0'; + } + + // Error code. + ErrorCode code() const { + return code_; + } + // Line number (__LINE__). + int line() const { + return code_; + } + // File name (__FILE__). + const char *file() const { + return file_; + } + // Function name (__func__, __FUNCTION__, or __PRETTY_FUNCTION__). + const char *function() const { + return function_; + } + // Error message. + const char *message() const { + return message_; + } + + // Set an error code. + void set_code(ErrorCode code) { + code_ = code; + } + // Set a line number. + void set_line(int line) { + line_ = line; + } + // Set a file name. + void set_file(const char *file) { + file_ = file; + } + // Set a function name. + void set_function(const char *function) { + function_ = function; + } + // Generate an error message with the printf syntax. + // If the expected message is too long, the result will be truncated. + // Return true on success. + bool set_message(const char *format, ...) + __attribute__ ((format (printf, 2, 3))); + + private: + static constexpr size_t MESSAGE_BUF_SIZE = 256; + + ErrorCode code_; + int line_; + const char *file_; + const char *function_; + char message_[MESSAGE_BUF_SIZE]; +}; + +} // namespace grnxx + +// Set error information. +#define GRNXX_ERROR_SET(error, code, format, ...) \ + (((error) != nullptr) && ((error)->set_code(code), \ + (error)->set_line(__LINE__), \ + (error)->set_file(__FILE__), \ + (error)->set_function(__PRETTY_FUNCTION__), \ + (error)->set_message(format, ## __VA_ARGS__))) + +#endif // GRNXX_TYPES_ERROR_HPP Modified: lib/grnxx/Makefile.am (+1 -0) =================================================================== --- lib/grnxx/Makefile.am 2014-10-30 18:39:08 +0900 (d92c70c) +++ lib/grnxx/Makefile.am 2014-10-30 18:43:20 +0900 (61326f3) @@ -9,6 +9,7 @@ lib_LTLIBRARIES = libgrnxx.la libgrnxx_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_la_SOURCES = \ + error.cpp \ library.cpp # array.cpp \ Modified: lib/grnxx/error.cpp (+1 -1) =================================================================== --- lib/grnxx/error.cpp 2014-10-30 18:39:08 +0900 (88adc5d) +++ lib/grnxx/error.cpp 2014-10-30 18:43:20 +0900 (5536c35) @@ -1,4 +1,4 @@ -#include "grnxx/types.hpp" +#include "grnxx/error.hpp" #include <cstdio> #include <cstdarg> -------------- next part -------------- HTML����������������������������...Download