diff options
author | sangwan.kwon <sangwan.kwon@samsung.com> | 2016-05-30 14:27:13 +0900 |
---|---|---|
committer | sangwan.kwon <sangwan.kwon@samsung.com> | 2016-05-30 14:35:25 +0900 |
commit | b8ffd7662428b4baff92a0c8271e84c2b55ce47b (patch) | |
tree | cdeecf4ba84a2fb7c852fa6c538a304f2a458196 /src/dpl/core | |
parent | 30177b747ab42a7cedc5228803e057d05583b1a6 (diff) | |
download | cert-checker-b8ffd7662428b4baff92a0c8271e84c2b55ce47b.tar.gz cert-checker-b8ffd7662428b4baff92a0c8271e84c2b55ce47b.tar.bz2 cert-checker-b8ffd7662428b4baff92a0c8271e84c2b55ce47b.zip |
Apply tizen coding rule
* It depends on cpp rule checker(version 160520)
Change-Id: Ica308e1296be4f3567725fdb004f3bae8a456a0c
Diffstat (limited to 'src/dpl/core')
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/assert.h | 14 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/colors.h | 74 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/exception.h | 617 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/noncopyable.h | 15 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/optional.h | 274 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/scoped_array.h | 51 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/scoped_free.h | 32 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/scoped_resource.h | 77 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/serialization.h | 849 | ||||
-rw-r--r-- | src/dpl/core/include/cchecker/dpl/string.h | 123 | ||||
-rw-r--r-- | src/dpl/core/src/assert.cpp | 64 | ||||
-rw-r--r-- | src/dpl/core/src/colors.cpp | 74 | ||||
-rw-r--r-- | src/dpl/core/src/errno_string.cpp | 87 | ||||
-rw-r--r-- | src/dpl/core/src/exception.cpp | 34 | ||||
-rw-r--r-- | src/dpl/core/src/string.cpp | 332 |
15 files changed, 1343 insertions, 1374 deletions
diff --git a/src/dpl/core/include/cchecker/dpl/assert.h b/src/dpl/core/include/cchecker/dpl/assert.h index 03dd7d0..1bb918e 100644 --- a/src/dpl/core/include/cchecker/dpl/assert.h +++ b/src/dpl/core/include/cchecker/dpl/assert.h @@ -29,15 +29,15 @@ namespace CCHECKER { // Do not call directly // Always use Assert macro CCHECKER_NORETURN void AssertProc(const char *condition, - const char *file, - int line, - const char *function); + const char *file, + int line, + const char *function); } // namespace CCHECKER #define Assert(Condition) do { if (!(Condition)) { CCHECKER::AssertProc(#Condition, \ - __FILE__, \ - __LINE__, \ - __FUNCTION__); \ - } } while (0) + __FILE__, \ + __LINE__, \ + __FUNCTION__); \ + } } while (0) #endif // CCHECKER_ASSERT_H diff --git a/src/dpl/core/include/cchecker/dpl/colors.h b/src/dpl/core/include/cchecker/dpl/colors.h index d652f5b..b1cf04a 100644 --- a/src/dpl/core/include/cchecker/dpl/colors.h +++ b/src/dpl/core/include/cchecker/dpl/colors.h @@ -27,46 +27,46 @@ namespace CCHECKER { namespace Colors { namespace Text { -extern const char* BOLD_GREEN_BEGIN; -extern const char* BOLD_GREEN_END; -extern const char* PURPLE_BEGIN; -extern const char* PURPLE_END; -extern const char* RED_BEGIN; -extern const char* RED_END; -extern const char* GREEN_BEGIN; -extern const char* GREEN_END; -extern const char* CYAN_BEGIN; -extern const char* CYAN_END; -extern const char* BOLD_RED_BEGIN; -extern const char* BOLD_RED_END; -extern const char* BOLD_YELLOW_BEGIN; -extern const char* BOLD_YELLOW_END; -extern const char* BOLD_GOLD_BEGIN; -extern const char* BOLD_GOLD_END; -extern const char* BOLD_WHITE_BEGIN; -extern const char* BOLD_WHITE_END; -extern const char* COLOR_END; +extern const char *BOLD_GREEN_BEGIN; +extern const char *BOLD_GREEN_END; +extern const char *PURPLE_BEGIN; +extern const char *PURPLE_END; +extern const char *RED_BEGIN; +extern const char *RED_END; +extern const char *GREEN_BEGIN; +extern const char *GREEN_END; +extern const char *CYAN_BEGIN; +extern const char *CYAN_END; +extern const char *BOLD_RED_BEGIN; +extern const char *BOLD_RED_END; +extern const char *BOLD_YELLOW_BEGIN; +extern const char *BOLD_YELLOW_END; +extern const char *BOLD_GOLD_BEGIN; +extern const char *BOLD_GOLD_END; +extern const char *BOLD_WHITE_BEGIN; +extern const char *BOLD_WHITE_END; +extern const char *COLOR_END; } //namespace Text namespace Html { -extern const char* BOLD_GREEN_BEGIN; -extern const char* BOLD_GREEN_END; -extern const char* PURPLE_BEGIN; -extern const char* PURPLE_END; -extern const char* RED_BEGIN; -extern const char* RED_END; -extern const char* GREEN_BEGIN; -extern const char* GREEN_END; -extern const char* CYAN_BEGIN; -extern const char* CYAN_END; -extern const char* BOLD_RED_BEGIN; -extern const char* BOLD_RED_END; -extern const char* BOLD_YELLOW_BEGIN; -extern const char* BOLD_YELLOW_END; -extern const char* BOLD_GOLD_BEGIN; -extern const char* BOLD_GOLD_END; -extern const char* BOLD_WHITE_BEGIN; -extern const char* BOLD_WHITE_END; +extern const char *BOLD_GREEN_BEGIN; +extern const char *BOLD_GREEN_END; +extern const char *PURPLE_BEGIN; +extern const char *PURPLE_END; +extern const char *RED_BEGIN; +extern const char *RED_END; +extern const char *GREEN_BEGIN; +extern const char *GREEN_END; +extern const char *CYAN_BEGIN; +extern const char *CYAN_END; +extern const char *BOLD_RED_BEGIN; +extern const char *BOLD_RED_END; +extern const char *BOLD_YELLOW_BEGIN; +extern const char *BOLD_YELLOW_END; +extern const char *BOLD_GOLD_BEGIN; +extern const char *BOLD_GOLD_END; +extern const char *BOLD_WHITE_BEGIN; +extern const char *BOLD_WHITE_END; } //namespace Html } //namespace Colors } //namespace CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/exception.h b/src/dpl/core/include/cchecker/dpl/exception.h index f7b9afc..a23e4ef 100644 --- a/src/dpl/core/include/cchecker/dpl/exception.h +++ b/src/dpl/core/include/cchecker/dpl/exception.h @@ -32,340 +32,331 @@ namespace CCHECKER { void LogUnhandledException(const std::string &str); void LogUnhandledException(const std::string &str, - const char *filename, - int line, - const char *function); + const char *filename, + int line, + const char *function); } namespace CCHECKER { -class Exception -{ - private: - static unsigned int m_exceptionCount; - static Exception* m_lastException; - static void (*m_terminateHandler)(); - - static void AddRef(Exception* exception) - { - if (!m_exceptionCount) { - m_terminateHandler = std::set_terminate(&TerminateHandler); - } - - ++m_exceptionCount; - m_lastException = exception; - } - - static void UnRef(Exception* e) - { - if (m_lastException == e) { - m_lastException = NULL; - } - - --m_exceptionCount; - - if (!m_exceptionCount) { - std::set_terminate(m_terminateHandler); - m_terminateHandler = NULL; - } - } - - static void TerminateHandler() - { - if (m_lastException != NULL) { - DisplayKnownException(*m_lastException); - abort(); - } else { - DisplayUnknownException(); - abort(); - } - } - - Exception *m_reason; - std::string m_path; - std::string m_function; - int m_line; - - protected: - std::string m_message; - std::string m_className; - - public: - static std::string KnownExceptionToString(const Exception &e) - { - std::ostringstream message; - message << - "\033[1;5;31m\n=== Unhandled CCHECKER exception occurred ===\033[m\n\n"; - message << "\033[1;33mException trace:\033[m\n\n"; - message << e.DumpToString(); - message << "\033[1;31m\n=== Will now abort ===\033[m\n"; - - return message.str(); - } - - static std::string UnknownExceptionToString() - { - std::ostringstream message; - message << - "\033[1;5;31m\n=== Unhandled non-CCHECKER exception occurred ===\033[m\n\n"; - message << "\033[1;31m\n=== Will now abort ===\033[m\n"; - - return message.str(); - } - - static void DisplayKnownException(const Exception& e) - { - LogUnhandledException(KnownExceptionToString(e).c_str()); - } - - static void DisplayUnknownException() - { - LogUnhandledException(UnknownExceptionToString().c_str()); - } - - Exception(const Exception &other) - { - // Deep copy - if (other.m_reason != NULL) { - m_reason = new Exception(*other.m_reason); - } else { - m_reason = NULL; - } - - m_message = other.m_message; - m_path = other.m_path; - m_function = other.m_function; - m_line = other.m_line; - - m_className = other.m_className; - - AddRef(this); - } - - const Exception &operator =(const Exception &other) - { - if (this == &other) { - return *this; - } - - // Deep copy - if (other.m_reason != NULL) { - m_reason = new Exception(*other.m_reason); - } else { - m_reason = NULL; - } - - m_message = other.m_message; - m_path = other.m_path; - m_function = other.m_function; - m_line = other.m_line; - - m_className = other.m_className; - - AddRef(this); - - return *this; - } - - Exception(const char *path, - const char *function, - int line, - const std::string &message) : - m_reason(NULL), - m_path(path), - m_function(function), - m_line(line), - m_message(message) - { - AddRef(this); - } - - Exception(const char *path, - const char *function, - int line, - const Exception &reason, - const std::string &message) : - m_reason(new Exception(reason)), - m_path(path), - m_function(function), - m_line(line), - m_message(message) - { - AddRef(this); - } - - virtual ~Exception() throw() - { - if (m_reason != NULL) { - delete m_reason; - m_reason = NULL; - } - - UnRef(this); - } - - void Dump() const - { - // Show reason first - if (m_reason != NULL) { - m_reason->Dump(); - } - - // Afterward, dump exception - const char *file = strchr(m_path.c_str(), '/'); - - if (file == NULL) { - file = m_path.c_str(); - } else { - ++file; - } - - printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", - file, m_line, - m_function.c_str(), - m_className.c_str(), - m_message.empty() ? "<EMPTY>" : m_message.c_str()); - } - - std::string DumpToString() const - { - std::string ret; - if (m_reason != NULL) { - ret = m_reason->DumpToString(); - } - - const char *file = strchr(m_path.c_str(), '/'); - - if (file == NULL) { - file = m_path.c_str(); - } else { - ++file; - } - - char buf[1024]; - snprintf(buf, - sizeof(buf), - "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", - file, - m_line, - m_function.c_str(), - m_className.c_str(), - m_message.empty() ? "<EMPTY>" : m_message.c_str()); - - buf[sizeof(buf) - 1] = '\n'; - ret += buf; - - return ret; - } - - Exception *GetReason() const - { - return m_reason; - } - - std::string GetPath() const - { - return m_path; - } - - std::string GetFunction() const - { - return m_function; - } - - int GetLine() const - { - return m_line; - } - - std::string GetMessage() const - { - return m_message; - } - - std::string GetClassName() const - { - return m_className; - } +class Exception { +private: + static unsigned int m_exceptionCount; + static Exception *m_lastException; + static void (*m_terminateHandler)(); + + static void AddRef(Exception *exception) + { + if (!m_exceptionCount) { + m_terminateHandler = std::set_terminate(&TerminateHandler); + } + + ++m_exceptionCount; + m_lastException = exception; + } + + static void UnRef(Exception *e) + { + if (m_lastException == e) { + m_lastException = NULL; + } + + --m_exceptionCount; + + if (!m_exceptionCount) { + std::set_terminate(m_terminateHandler); + m_terminateHandler = NULL; + } + } + + static void TerminateHandler() + { + if (m_lastException != NULL) { + DisplayKnownException(*m_lastException); + abort(); + } else { + DisplayUnknownException(); + abort(); + } + } + + Exception *m_reason; + std::string m_path; + std::string m_function; + int m_line; + +protected: + std::string m_message; + std::string m_className; + +public: + static std::string KnownExceptionToString(const Exception &e) + { + std::ostringstream message; + message << + "\033[1;5;31m\n=== Unhandled CCHECKER exception occurred ===\033[m\n\n"; + message << "\033[1;33mException trace:\033[m\n\n"; + message << e.DumpToString(); + message << "\033[1;31m\n=== Will now abort ===\033[m\n"; + return message.str(); + } + + static std::string UnknownExceptionToString() + { + std::ostringstream message; + message << + "\033[1;5;31m\n=== Unhandled non-CCHECKER exception occurred ===\033[m\n\n"; + message << "\033[1;31m\n=== Will now abort ===\033[m\n"; + return message.str(); + } + + static void DisplayKnownException(const Exception &e) + { + LogUnhandledException(KnownExceptionToString(e).c_str()); + } + + static void DisplayUnknownException() + { + LogUnhandledException(UnknownExceptionToString().c_str()); + } + + Exception(const Exception &other) + { + // Deep copy + if (other.m_reason != NULL) { + m_reason = new Exception(*other.m_reason); + } else { + m_reason = NULL; + } + + m_message = other.m_message; + m_path = other.m_path; + m_function = other.m_function; + m_line = other.m_line; + m_className = other.m_className; + AddRef(this); + } + + const Exception &operator =(const Exception &other) + { + if (this == &other) { + return *this; + } + + // Deep copy + if (other.m_reason != NULL) { + m_reason = new Exception(*other.m_reason); + } else { + m_reason = NULL; + } + + m_message = other.m_message; + m_path = other.m_path; + m_function = other.m_function; + m_line = other.m_line; + m_className = other.m_className; + AddRef(this); + return *this; + } + + Exception(const char *path, + const char *function, + int line, + const std::string &message) : + m_reason(NULL), + m_path(path), + m_function(function), + m_line(line), + m_message(message) + { + AddRef(this); + } + + Exception(const char *path, + const char *function, + int line, + const Exception &reason, + const std::string &message) : + m_reason(new Exception(reason)), + m_path(path), + m_function(function), + m_line(line), + m_message(message) + { + AddRef(this); + } + + virtual ~Exception() throw() + { + if (m_reason != NULL) { + delete m_reason; + m_reason = NULL; + } + + UnRef(this); + } + + void Dump() const + { + // Show reason first + if (m_reason != NULL) { + m_reason->Dump(); + } + + // Afterward, dump exception + const char *file = strchr(m_path.c_str(), '/'); + + if (file == NULL) { + file = m_path.c_str(); + } else { + ++file; + } + + printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", + file, m_line, + m_function.c_str(), + m_className.c_str(), + m_message.empty() ? "<EMPTY>" : m_message.c_str()); + } + + std::string DumpToString() const + { + std::string ret; + + if (m_reason != NULL) { + ret = m_reason->DumpToString(); + } + + const char *file = strchr(m_path.c_str(), '/'); + + if (file == NULL) { + file = m_path.c_str(); + } else { + ++file; + } + + char buf[1024]; + snprintf(buf, + sizeof(buf), + "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", + file, + m_line, + m_function.c_str(), + m_className.c_str(), + m_message.empty() ? "<EMPTY>" : m_message.c_str()); + buf[sizeof(buf) - 1] = '\n'; + ret += buf; + return ret; + } + + Exception *GetReason() const + { + return m_reason; + } + + std::string GetPath() const + { + return m_path; + } + + std::string GetFunction() const + { + return m_function; + } + + int GetLine() const + { + return m_line; + } + + std::string GetMessage() const + { + return m_message; + } + + std::string GetClassName() const + { + return m_className; + } }; } // namespace CCHECKER #define Try try #define Throw(ClassName) \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__) + throw ClassName(__FILE__, __FUNCTION__, __LINE__) #define ThrowMsg(ClassName, Message) \ - do \ - { \ - std::ostringstream dplLoggingStream; \ - dplLoggingStream << Message; \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \ - } while (0) + do \ + { \ + std::ostringstream dplLoggingStream; \ + dplLoggingStream << Message; \ + throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \ + } while (0) #define ReThrow(ClassName) \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception) + throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception) #define ReThrowMsg(ClassName, Message) \ - throw ClassName(__FILE__, \ - __FUNCTION__, \ - __LINE__, \ - _rethrown_exception, \ - Message) + throw ClassName(__FILE__, \ + __FUNCTION__, \ + __LINE__, \ + _rethrown_exception, \ + Message) #define Catch(ClassName) \ - catch (const ClassName &_rethrown_exception) + catch (const ClassName &_rethrown_exception) #define DECLARE_EXCEPTION_TYPE(BaseClass, Class) \ - class Class : \ - public BaseClass \ - { \ - public: \ - Class(const char *path, \ - const char *function, \ - int line, \ - const std::string & message = std::string()) : \ - BaseClass(path, function, line, message) \ - { \ - BaseClass::m_className = #Class; \ - } \ - \ - Class(const char *path, \ - const char *function, \ - int line, \ - const CCHECKER::Exception & reason, \ - const std::string & message = std::string()) : \ - BaseClass(path, function, line, reason, message) \ - { \ - BaseClass::m_className = #Class; \ - } \ - }; + class Class : \ + public BaseClass \ + { \ + public: \ + Class(const char *path, \ + const char *function, \ + int line, \ + const std::string & message = std::string()) : \ + BaseClass(path, function, line, message) \ + { \ + BaseClass::m_className = #Class; \ + } \ + \ + Class(const char *path, \ + const char *function, \ + int line, \ + const CCHECKER::Exception & reason, \ + const std::string & message = std::string()) : \ + BaseClass(path, function, line, reason, message) \ + { \ + BaseClass::m_className = #Class; \ + } \ + }; #define UNHANDLED_EXCEPTION_HANDLER_BEGIN try #define UNHANDLED_EXCEPTION_HANDLER_END \ - catch (const CCHECKER::Exception &exception) \ - { \ - std::ostringstream msg; \ - msg << CCHECKER::Exception::KnownExceptionToString(exception); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } \ - catch (std::exception& e) \ - { \ - std::ostringstream msg; \ - msg << e.what(); \ - msg << "\n"; \ - msg << CCHECKER::Exception::UnknownExceptionToString(); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } \ - catch (...) \ - { \ - std::ostringstream msg; \ - msg << CCHECKER::Exception::UnknownExceptionToString(); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } + catch (const CCHECKER::Exception &exception) \ + { \ + std::ostringstream msg; \ + msg << CCHECKER::Exception::KnownExceptionToString(exception); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } \ + catch (std::exception& e) \ + { \ + std::ostringstream msg; \ + msg << e.what(); \ + msg << "\n"; \ + msg << CCHECKER::Exception::UnknownExceptionToString(); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } \ + catch (...) \ + { \ + std::ostringstream msg; \ + msg << CCHECKER::Exception::UnknownExceptionToString(); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } namespace CCHECKER { namespace CommonException { @@ -377,8 +368,8 @@ namespace CommonException { * important messages. */ DECLARE_EXCEPTION_TYPE(Exception, InternalError) ///< Unexpected error from - // underlying libraries or - // kernel +// underlying libraries or +// kernel } } diff --git a/src/dpl/core/include/cchecker/dpl/noncopyable.h b/src/dpl/core/include/cchecker/dpl/noncopyable.h index 747299c..47fc8b3 100644 --- a/src/dpl/core/include/cchecker/dpl/noncopyable.h +++ b/src/dpl/core/include/cchecker/dpl/noncopyable.h @@ -23,15 +23,14 @@ #define CCHECKER_NONCOPYABLE_H namespace CCHECKER { -class Noncopyable -{ - private: - Noncopyable(const Noncopyable &); - const Noncopyable &operator=(const Noncopyable &); +class Noncopyable { +private: + Noncopyable(const Noncopyable &); + const Noncopyable &operator=(const Noncopyable &); - public: - Noncopyable(); - virtual ~Noncopyable(); +public: + Noncopyable(); + virtual ~Noncopyable(); }; } // namespace CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/optional.h b/src/dpl/core/include/cchecker/dpl/optional.h index 42d1bd4..0f19d86 100644 --- a/src/dpl/core/include/cchecker/dpl/optional.h +++ b/src/dpl/core/include/cchecker/dpl/optional.h @@ -26,135 +26,137 @@ namespace CCHECKER { template <typename Type> -class Optional -{ - class Exception - { - public: - DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) - DECLARE_EXCEPTION_TYPE(Base, NullReference) - }; - - public: - Optional() : - m_null(true), - m_value() - {} - - Optional(const Type& t) : - m_null(false), - m_value(t) - {} - - bool IsNull() const - { - return m_null; - } - - Type& operator*() - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return m_value; - } - - const Type& operator*() const - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return m_value; - } - - const Type* operator->() const - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return &m_value; - } - - Type* operator->() - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return &m_value; - } - - bool operator!() const - { - return m_null; - } - - Optional<Type>& operator=(const Type& other) - { - m_null = false; - m_value = other; - return *this; - } - - bool operator==(const Optional<Type>& aSecond) const - { - return LogicalOperator<true>(*this, aSecond, - std::equal_to<Type>(), std::equal_to<bool>()); - } - - bool operator==(const Type& aSecond) const - { - return Optional<Type>(aSecond) == *this; - } - - bool operator!=(const Optional<Type>& aSecond) const - { - return !(*this == aSecond); - } - - bool operator<(const Optional<Type>& aSecond) const - { - return LogicalOperator<false>(*this, aSecond, - std::less<Type>(), std::less<bool>()); - } - - bool operator>(const Optional<Type>& aSecond) const - { - return LogicalOperator<false>(*this, aSecond, - std::greater<Type>(), std::greater<bool>()); - } - - bool operator<=(const Optional<Type>& aSecond) const - { - return *this == aSecond || *this < aSecond; - } - - bool operator>=(const Optional<Type>& aSecond) const - { - return *this == aSecond || *this > aSecond; - } - - static Optional<Type> Null; - - private: - bool m_null; - Type m_value; - - template <bool taEquality, typename taComparator, typename taNullComparator> - static bool LogicalOperator(const Optional<Type>& aFirst, - const Optional<Type>& aSecond, - taComparator aComparator, - taNullComparator aNullComparator) - { - if (aFirst.m_null == aSecond.m_null) { - if (aFirst.m_null) { - return taEquality; - } else { - return aComparator(aFirst.m_value, aSecond.m_value); - } - } else { - return aNullComparator(aFirst.m_null, aSecond.m_null); - } - } +class Optional { + class Exception { + public: + DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) + DECLARE_EXCEPTION_TYPE(Base, NullReference) + }; + +public: + Optional() : + m_null(true), + m_value() + {} + + Optional(const Type &t) : + m_null(false), + m_value(t) + {} + + bool IsNull() const + { + return m_null; + } + + Type &operator*() + { + if (m_null) { + Throw(typename Exception::NullReference); + } + + return m_value; + } + + const Type &operator*() const + { + if (m_null) { + Throw(typename Exception::NullReference); + } + + return m_value; + } + + const Type *operator->() const + { + if (m_null) { + Throw(typename Exception::NullReference); + } + + return &m_value; + } + + Type *operator->() + { + if (m_null) { + Throw(typename Exception::NullReference); + } + + return &m_value; + } + + bool operator!() const + { + return m_null; + } + + Optional<Type> &operator=(const Type &other) + { + m_null = false; + m_value = other; + return *this; + } + + bool operator==(const Optional<Type> &aSecond) const + { + return LogicalOperator<true>(*this, aSecond, + std::equal_to<Type>(), std::equal_to<bool>()); + } + + bool operator==(const Type &aSecond) const + { + return Optional<Type>(aSecond) == *this; + } + + bool operator!=(const Optional<Type> &aSecond) const + { + return !(*this == aSecond); + } + + bool operator<(const Optional<Type> &aSecond) const + { + return LogicalOperator<false>(*this, aSecond, + std::less<Type>(), std::less<bool>()); + } + + bool operator>(const Optional<Type> &aSecond) const + { + return LogicalOperator<false>(*this, aSecond, + std::greater<Type>(), std::greater<bool>()); + } + + bool operator<=(const Optional<Type> &aSecond) const + { + return *this == aSecond || *this < aSecond; + } + + bool operator>=(const Optional<Type> &aSecond) const + { + return *this == aSecond || *this > aSecond; + } + + static Optional<Type> Null; + +private: + bool m_null; + Type m_value; + + template <bool taEquality, typename taComparator, typename taNullComparator> + static bool LogicalOperator(const Optional<Type> &aFirst, + const Optional<Type> &aSecond, + taComparator aComparator, + taNullComparator aNullComparator) + { + if (aFirst.m_null == aSecond.m_null) { + if (aFirst.m_null) { + return taEquality; + } else { + return aComparator(aFirst.m_value, aSecond.m_value); + } + } else { + return aNullComparator(aFirst.m_null, aSecond.m_null); + } + } }; template<typename Type> @@ -162,14 +164,14 @@ Optional<Type> Optional<Type>::Null = Optional<Type>(); } //namespace CCHECKER template<typename Type> -std::ostream& operator<<(std::ostream& aStream, - const CCHECKER::Optional<Type>& aOptional) +std::ostream &operator<<(std::ostream &aStream, + const CCHECKER::Optional<Type> &aOptional) { - if (aOptional.IsNull()) { - return aStream << "null optional"; - } else { - return aStream << *aOptional; - } + if (aOptional.IsNull()) { + return aStream << "null optional"; + } else { + return aStream << *aOptional; + } } #endif // CCHECKER_OPTIONAL_VALUE_H diff --git a/src/dpl/core/include/cchecker/dpl/scoped_array.h b/src/dpl/core/include/cchecker/dpl/scoped_array.h index 54c0c80..f38ae83 100644 --- a/src/dpl/core/include/cchecker/dpl/scoped_array.h +++ b/src/dpl/core/include/cchecker/dpl/scoped_array.h @@ -29,36 +29,33 @@ namespace CCHECKER { template<typename Class> -struct ScopedArrayPolicy -{ - typedef Class* Type; - static Type NullValue() - { - return NULL; - } - static void Destroy(Type ptr) - { - delete[] ptr; - } +struct ScopedArrayPolicy { + typedef Class *Type; + static Type NullValue() + { + return NULL; + } + static void Destroy(Type ptr) + { + delete[] ptr; + } }; template<typename Class> -class ScopedArray : public ScopedResource<ScopedArrayPolicy<Class> > -{ - typedef ScopedArrayPolicy<Class> Policy; - typedef ScopedResource<Policy> BaseType; - - public: - explicit ScopedArray(Class *ptr = Policy::NullValue()) : BaseType(ptr) { } - - Class &operator [](std::ptrdiff_t k) const - { - Assert(this->m_value != Policy::NullValue() && - "Dereference of scoped NULL array!"); - Assert(k >= 0 && "Negative array index"); - - return this->m_value[k]; - } +class ScopedArray : public ScopedResource<ScopedArrayPolicy<Class>> { + typedef ScopedArrayPolicy<Class> Policy; + typedef ScopedResource<Policy> BaseType; + +public: + explicit ScopedArray(Class *ptr = Policy::NullValue()) : BaseType(ptr) { } + + Class &operator[](std::ptrdiff_t k) const + { + Assert(this->m_value != Policy::NullValue() && + "Dereference of scoped NULL array!"); + Assert(k >= 0 && "Negative array index"); + return this->m_value[k]; + } }; } // namespace CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/scoped_free.h b/src/dpl/core/include/cchecker/dpl/scoped_free.h index d8fee50..72b0ae0 100644 --- a/src/dpl/core/include/cchecker/dpl/scoped_free.h +++ b/src/dpl/core/include/cchecker/dpl/scoped_free.h @@ -30,27 +30,25 @@ namespace CCHECKER { template<typename Class> -struct ScopedFreePolicy -{ - typedef Class* Type; - static Type NullValue() - { - return NULL; - } - static void Destroy(Type ptr) - { - free(ptr); - } +struct ScopedFreePolicy { + typedef Class *Type; + static Type NullValue() + { + return NULL; + } + static void Destroy(Type ptr) + { + free(ptr); + } }; template<typename Memory> -class ScopedFree : public ScopedResource<ScopedFreePolicy<Memory> > -{ - typedef ScopedFreePolicy<Memory> Policy; - typedef ScopedResource<Policy> BaseType; +class ScopedFree : public ScopedResource<ScopedFreePolicy<Memory>> { + typedef ScopedFreePolicy<Memory> Policy; + typedef ScopedResource<Policy> BaseType; - public: - explicit ScopedFree(Memory *ptr = Policy::NullValue()) : BaseType(ptr) { } +public: + explicit ScopedFree(Memory *ptr = Policy::NullValue()) : BaseType(ptr) { } }; } // namespace CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/scoped_resource.h b/src/dpl/core/include/cchecker/dpl/scoped_resource.h index 2b3c72d..42aef36 100644 --- a/src/dpl/core/include/cchecker/dpl/scoped_resource.h +++ b/src/dpl/core/include/cchecker/dpl/scoped_resource.h @@ -27,53 +27,52 @@ namespace CCHECKER { template<typename ClassPolicy> class ScopedResource : - private Noncopyable -{ - public: - typedef typename ClassPolicy::Type ValueType; - typedef ScopedResource<ClassPolicy> ThisType; + private Noncopyable { +public: + typedef typename ClassPolicy::Type ValueType; + typedef ScopedResource<ClassPolicy> ThisType; - protected: - ValueType m_value; +protected: + ValueType m_value; - public: - explicit ScopedResource(ValueType value) : m_value(value) { } +public: + explicit ScopedResource(ValueType value) : m_value(value) { } - ~ScopedResource() - { - ClassPolicy::Destroy(m_value); - } + ~ScopedResource() + { + ClassPolicy::Destroy(m_value); + } - ValueType Get() const - { - return m_value; - } + ValueType Get() const + { + return m_value; + } - void Reset(ValueType value = ClassPolicy::NullValue()) - { - ClassPolicy::Destroy(m_value); - m_value = value; - } + void Reset(ValueType value = ClassPolicy::NullValue()) + { + ClassPolicy::Destroy(m_value); + m_value = value; + } - ValueType Release() - { - ValueType value = m_value; - m_value = ClassPolicy::NullValue(); - return value; - } - typedef ValueType ThisType::*UnknownBoolType; + ValueType Release() + { + ValueType value = m_value; + m_value = ClassPolicy::NullValue(); + return value; + } + typedef ValueType ThisType::*UnknownBoolType; - operator UnknownBoolType() const - { - return m_value == ClassPolicy::NullValue() ? - 0 : //0 is valid here because it converts to false - &ThisType::m_value; //it converts to true - } + operator UnknownBoolType() const + { + return m_value == ClassPolicy::NullValue() ? + 0 : //0 is valid here because it converts to false + &ThisType::m_value; //it converts to true + } - bool operator !() const - { - return m_value == ClassPolicy::NullValue(); - } + bool operator !() const + { + return m_value == ClassPolicy::NullValue(); + } }; } // namespace CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/serialization.h b/src/dpl/core/include/cchecker/dpl/serialization.h index 5628350..c39ca09 100644 --- a/src/dpl/core/include/cchecker/dpl/serialization.h +++ b/src/dpl/core/include/cchecker/dpl/serialization.h @@ -32,423 +32,424 @@ namespace CCHECKER { // Abstract data stream buffer -class IStream -{ - public: - virtual void Read(size_t num, void * bytes) = 0; - virtual void Write(size_t num, const void * bytes) = 0; - virtual ~IStream(){} +class IStream { +public: + virtual void Read(size_t num, void *bytes) = 0; + virtual void Write(size_t num, const void *bytes) = 0; + virtual ~IStream() {} }; // Serializable interface -class ISerializable -{ - public: - /* ISerializable(){}; - * ISerializable(IStream&){}; */ - virtual void Serialize(IStream &) const = 0; - virtual ~ISerializable(){} +class ISerializable { +public: + /* ISerializable(){}; + * ISerializable(IStream&){}; */ + virtual void Serialize(IStream &) const = 0; + virtual ~ISerializable() {} }; struct Serialization { - // serialization - // normal functions - - // ISerializable objects - static void Serialize(IStream& stream, const ISerializable& object) - { - object.Serialize(stream); - } - - static void Serialize(IStream& stream, const ISerializable* const object) - { - object->Serialize(stream); - } - - // char - static void Serialize(IStream& stream, const char value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const char* const value) - { - stream.Write(sizeof(*value), value); - } - - // unsigned char - static void Serialize(IStream& stream, const unsigned char value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const unsigned char* const value) - { - stream.Write(sizeof(*value), value); - } - - // unsigned int32 - static void Serialize(IStream& stream, const uint32_t value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const uint32_t* const value) - { - stream.Write(sizeof(*value), value); - } - - // int32 - static void Serialize(IStream& stream, const int32_t value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const int32_t* const value) - { - stream.Write(sizeof(*value), value); - } - - // unsigned int64 - static void Serialize(IStream& stream, const uint64_t value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const uint64_t* const value) - { - stream.Write(sizeof(*value), value); - } - - // int64 - static void Serialize(IStream& stream, const int64_t value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const int64_t* const value) - { - stream.Write(sizeof(*value), value); - } - - // bool - static void Serialize(IStream& stream, const bool value) - { - stream.Write(sizeof(value), &value); - } - static void Serialize(IStream& stream, const bool* const value) - { - stream.Write(sizeof(*value), value); - } - - // std::string - template <typename T, typename R, typename A> - static void Serialize(IStream& stream, const std::basic_string<T,R,A>& str) - { - int length = str.size(); - stream.Write(sizeof(length), &length); - stream.Write(length*sizeof(T), str.data()); - } - - template<typename T, typename R, typename A> - static void Serialize(IStream& stream, const std::basic_string<T,R,A>* const str) - { - int length = str->size(); - stream.Write(sizeof(length), &length); - stream.Write(length*sizeof(T), str->data()); - } - - // STL templates - - // std::list - template <typename T> - static void Serialize(IStream& stream, const std::list<T>& list) - { - int length = list.size(); - stream.Write(sizeof(length), &length); - for (typename std::list<T>::const_iterator list_iter = list.begin(); - list_iter != list.end(); list_iter++) - { - Serialize(stream, *list_iter); - } - } - template <typename T> - static void Serialize(IStream& stream, const std::list<T>* const list) - { - Serialize(stream, *list); - } - - // RawBuffer - template <typename A> - static void Serialize(IStream& stream, const std::vector<unsigned char, A>& vec) - { - int length = vec.size(); - stream.Write(sizeof(length), &length); - stream.Write(length, vec.data()); - } - - template <typename A> - static void Serialize(IStream& stream, const std::vector<unsigned char, A>* const vec) - { - Serialize(stream, *vec); - } - - // std::vector - template <typename T, typename A> - static void Serialize(IStream& stream, const std::vector<T, A>& vec) - { - int length = vec.size(); - stream.Write(sizeof(length), &length); - for (const auto &i : vec) - { - Serialize(stream, i); - } - } - template <typename T, typename A> - static void Serialize(IStream& stream, const std::vector<T, A>* const vec) - { - Serialize(stream, *vec); - } - - // std::pair - template <typename A, typename B> - static void Serialize(IStream& stream, const std::pair<A, B>& p) - { - Serialize(stream, p.first); - Serialize(stream, p.second); - } - template <typename A, typename B> - static void Serialize(IStream& stream, const std::pair<A, B>* const p) - { - Serialize(stream, *p); - } - - // std::map - template <typename K, typename T> - static void Serialize(IStream& stream, const std::map<K, T>& map) - { - int length = map.size(); - stream.Write(sizeof(length), &length); - typename std::map<K, T>::const_iterator it; - for (it = map.begin(); it != map.end(); ++it) { - Serialize(stream, (*it).first); - Serialize(stream, (*it).second); - } - } - template <typename K, typename T> - static void Serialize(IStream& stream, const std::map<K, T>* const map) - { - Serialize(stream, *map); - } - - // std::unique_ptr - template <typename T> - static void Serialize(IStream& stream, const std::unique_ptr<T>& p) - { - Serialize(stream, *p); - } - + // serialization + // normal functions + + // ISerializable objects + static void Serialize(IStream &stream, const ISerializable &object) + { + object.Serialize(stream); + } + + static void Serialize(IStream &stream, const ISerializable *const object) + { + object->Serialize(stream); + } + + // char + static void Serialize(IStream &stream, const char value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const char *const value) + { + stream.Write(sizeof(*value), value); + } + + // unsigned char + static void Serialize(IStream &stream, const unsigned char value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const unsigned char *const value) + { + stream.Write(sizeof(*value), value); + } + + // unsigned int32 + static void Serialize(IStream &stream, const uint32_t value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const uint32_t *const value) + { + stream.Write(sizeof(*value), value); + } + + // int32 + static void Serialize(IStream &stream, const int32_t value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const int32_t *const value) + { + stream.Write(sizeof(*value), value); + } + + // unsigned int64 + static void Serialize(IStream &stream, const uint64_t value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const uint64_t *const value) + { + stream.Write(sizeof(*value), value); + } + + // int64 + static void Serialize(IStream &stream, const int64_t value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const int64_t *const value) + { + stream.Write(sizeof(*value), value); + } + + // bool + static void Serialize(IStream &stream, const bool value) + { + stream.Write(sizeof(value), &value); + } + static void Serialize(IStream &stream, const bool *const value) + { + stream.Write(sizeof(*value), value); + } + + // std::string + template <typename T, typename R, typename A> + static void Serialize(IStream &stream, const std::basic_string<T, R, A> &str) + { + int length = str.size(); + stream.Write(sizeof(length), &length); + stream.Write(length * sizeof(T), str.data()); + } + + template<typename T, typename R, typename A> + static void Serialize(IStream &stream, const std::basic_string<T, R, A> *const str) + { + int length = str->size(); + stream.Write(sizeof(length), &length); + stream.Write(length * sizeof(T), str->data()); + } + + // STL templates + + // std::list + template <typename T> + static void Serialize(IStream &stream, const std::list<T> &list) + { + int length = list.size(); + stream.Write(sizeof(length), &length); + + for (typename std::list<T>::const_iterator list_iter = list.begin(); + list_iter != list.end(); list_iter++) { + Serialize(stream, *list_iter); + } + } + template <typename T> + static void Serialize(IStream &stream, const std::list<T> *const list) + { + Serialize(stream, *list); + } + + // RawBuffer + template <typename A> + static void Serialize(IStream &stream, const std::vector<unsigned char, A> &vec) + { + int length = vec.size(); + stream.Write(sizeof(length), &length); + stream.Write(length, vec.data()); + } + + template <typename A> + static void Serialize(IStream &stream, const std::vector<unsigned char, A> *const vec) + { + Serialize(stream, *vec); + } + + // std::vector + template <typename T, typename A> + static void Serialize(IStream &stream, const std::vector<T, A> &vec) + { + int length = vec.size(); + stream.Write(sizeof(length), &length); + + for (const auto &i : vec) { + Serialize(stream, i); + } + } + template <typename T, typename A> + static void Serialize(IStream &stream, const std::vector<T, A> *const vec) + { + Serialize(stream, *vec); + } + + // std::pair + template <typename A, typename B> + static void Serialize(IStream &stream, const std::pair<A, B> &p) + { + Serialize(stream, p.first); + Serialize(stream, p.second); + } + template <typename A, typename B> + static void Serialize(IStream &stream, const std::pair<A, B> *const p) + { + Serialize(stream, *p); + } + + // std::map + template <typename K, typename T> + static void Serialize(IStream &stream, const std::map<K, T> &map) + { + int length = map.size(); + stream.Write(sizeof(length), &length); + typename std::map<K, T>::const_iterator it; + + for (it = map.begin(); it != map.end(); ++it) { + Serialize(stream, (*it).first); + Serialize(stream, (*it).second); + } + } + template <typename K, typename T> + static void Serialize(IStream &stream, const std::map<K, T> *const map) + { + Serialize(stream, *map); + } + + // std::unique_ptr + template <typename T> + static void Serialize(IStream &stream, const std::unique_ptr<T> &p) + { + Serialize(stream, *p); + } }; // struct Serialization struct Deserialization { - // deserialization - // normal functions - - // ISerializable objects - // T instead of ISerializable is needed to call proper constructor - template <typename T> - static void Deserialize(IStream& stream, T& object) - { - object = T(stream); - } - template <typename T> - static void Deserialize(IStream& stream, T*& object) - { - object = new T(stream); - } - - // char - static void Deserialize(IStream& stream, char& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, char*& value) - { - value = new char; - stream.Read(sizeof(*value), value); - } - - // unsigned char - static void Deserialize(IStream& stream, unsigned char& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, unsigned char*& value) - { - value = new unsigned char; - stream.Read(sizeof(*value), value); - } - - // unsigned int32 - static void Deserialize(IStream& stream, uint32_t& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, uint32_t*& value) - { - value = new uint32_t; - stream.Read(sizeof(*value), value); - } - - // int32 - static void Deserialize(IStream& stream, int32_t& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, int32_t*& value) - { - value = new int32_t; - stream.Read(sizeof(*value), value); - } - - // unsigned int64 - static void Deserialize(IStream& stream, uint64_t& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, uint64_t*& value) - { - value = new uint64_t; - stream.Read(sizeof(*value), value); - } - - // int64 - static void Deserialize(IStream& stream, int64_t& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, int64_t*& value) - { - value = new int64_t; - stream.Read(sizeof(*value), value); - } - - // bool - static void Deserialize(IStream& stream, bool& value) - { - stream.Read(sizeof(value), &value); - } - static void Deserialize(IStream& stream, bool*& value) - { - value = new bool; - stream.Read(sizeof(*value), value); - } - - template <typename T, typename R, typename A> - static void Deserialize(IStream& stream, std::basic_string<T,R,A>& str) - { - int length; - stream.Read(sizeof(length), &length); - std::vector<T> buf(length); - stream.Read(length*sizeof(T), buf.data()); - str = std::basic_string<T,R,A>(buf.data(), buf.data()+length); - } - - template <typename T, typename R, typename A> - static void Deserialize(IStream& stream, std::basic_string<T,R,A>*& str) - { - int length; - stream.Read(sizeof(length), &length); - std::vector<T> buf(length); - stream.Read(length*sizeof(T), buf.data()); - str = new std::basic_string<T,R,A>(buf.data(), buf.data()+length); - } - - // STL templates - - // std::list - template <typename T> - static void Deserialize(IStream& stream, std::list<T>& list) - { - int length; - stream.Read(sizeof(length), &length); - for (int i = 0; i < length; ++i) { - T obj; - Deserialize(stream, obj); - list.push_back(std::move(obj)); - } - } - template <typename T> - static void Deserialize(IStream& stream, std::list<T>*& list) - { - list = new std::list<T>; - Deserialize(stream, *list); - } - - // RawBuffer - template <typename A> - static void Deserialize(IStream& stream, std::vector<unsigned char, A>& vec) - { - int length; - stream.Read(sizeof(length), &length); - vec.resize(length); - stream.Read(length, vec.data()); - } - - template <typename A> - static void Deserialize(IStream& stream, std::vector<unsigned char, A>*& vec) - { - vec = new std::vector<unsigned char,A>; - Deserialize(stream, *vec); - } - - // std::vector - template <typename T, typename A> - static void Deserialize(IStream& stream, std::vector<T,A>& vec) - { - int length; - stream.Read(sizeof(length), &length); - for (int i = 0; i < length; ++i) { - T obj; - Deserialize(stream, obj); - vec.push_back(std::move(obj)); - } - } - template <typename T, typename A> - static void Deserialize(IStream& stream, std::vector<T,A>*& vec) - { - vec = new std::vector<T,A>; - Deserialize(stream, *vec); - } - - // std::pair - template <typename A, typename B> - static void Deserialize(IStream& stream, std::pair<A, B>& p) - { - Deserialize(stream, p.first); - Deserialize(stream, p.second); - } - template <typename A, typename B> - static void Deserialize(IStream& stream, std::pair<A, B>*& p) - { - p = new std::pair<A, B>; - Deserialize(stream, *p); - } - - // std::map - template <typename K, typename T> - static void Deserialize(IStream& stream, std::map<K, T>& map) - { - int length; - stream.Read(sizeof(length), &length); - for (int i = 0; i < length; ++i) { - K key; - T obj; - Deserialize(stream, key); - Deserialize(stream, obj); - map[key] = std::move(obj); - } - } - template <typename K, typename T> - static void Deserialize(IStream& stream, std::map<K, T>*& map) - { - map = new std::map<K, T>; - Deserialize(stream, *map); - } + // deserialization + // normal functions + + // ISerializable objects + // T instead of ISerializable is needed to call proper constructor + template <typename T> + static void Deserialize(IStream &stream, T &object) + { + object = T(stream); + } + template <typename T> + static void Deserialize(IStream &stream, T *&object) + { + object = new T(stream); + } + + // char + static void Deserialize(IStream &stream, char &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, char *&value) + { + value = new char; + stream.Read(sizeof(*value), value); + } + + // unsigned char + static void Deserialize(IStream &stream, unsigned char &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, unsigned char *&value) + { + value = new unsigned char; + stream.Read(sizeof(*value), value); + } + + // unsigned int32 + static void Deserialize(IStream &stream, uint32_t &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, uint32_t *&value) + { + value = new uint32_t; + stream.Read(sizeof(*value), value); + } + + // int32 + static void Deserialize(IStream &stream, int32_t &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, int32_t *&value) + { + value = new int32_t; + stream.Read(sizeof(*value), value); + } + + // unsigned int64 + static void Deserialize(IStream &stream, uint64_t &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, uint64_t *&value) + { + value = new uint64_t; + stream.Read(sizeof(*value), value); + } + + // int64 + static void Deserialize(IStream &stream, int64_t &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, int64_t *&value) + { + value = new int64_t; + stream.Read(sizeof(*value), value); + } + + // bool + static void Deserialize(IStream &stream, bool &value) + { + stream.Read(sizeof(value), &value); + } + static void Deserialize(IStream &stream, bool *&value) + { + value = new bool; + stream.Read(sizeof(*value), value); + } + + template <typename T, typename R, typename A> + static void Deserialize(IStream &stream, std::basic_string<T, R, A> &str) + { + int length; + stream.Read(sizeof(length), &length); + std::vector<T> buf(length); + stream.Read(length * sizeof(T), buf.data()); + str = std::basic_string<T, R, A>(buf.data(), buf.data() + length); + } + + template <typename T, typename R, typename A> + static void Deserialize(IStream &stream, std::basic_string<T, R, A> *&str) + { + int length; + stream.Read(sizeof(length), &length); + std::vector<T> buf(length); + stream.Read(length * sizeof(T), buf.data()); + str = new std::basic_string<T, R, A>(buf.data(), buf.data() + length); + } + + // STL templates + + // std::list + template <typename T> + static void Deserialize(IStream &stream, std::list<T> &list) + { + int length; + stream.Read(sizeof(length), &length); + + for (int i = 0; i < length; ++i) { + T obj; + Deserialize(stream, obj); + list.push_back(std::move(obj)); + } + } + template <typename T> + static void Deserialize(IStream &stream, std::list<T> *&list) + { + list = new std::list<T>; + Deserialize(stream, *list); + } + + // RawBuffer + template <typename A> + static void Deserialize(IStream &stream, std::vector<unsigned char, A> &vec) + { + int length; + stream.Read(sizeof(length), &length); + vec.resize(length); + stream.Read(length, vec.data()); + } + + template <typename A> + static void Deserialize(IStream &stream, std::vector<unsigned char, A> *&vec) + { + vec = new std::vector<unsigned char, A>; + Deserialize(stream, *vec); + } + + // std::vector + template <typename T, typename A> + static void Deserialize(IStream &stream, std::vector<T, A> &vec) + { + int length; + stream.Read(sizeof(length), &length); + + for (int i = 0; i < length; ++i) { + T obj; + Deserialize(stream, obj); + vec.push_back(std::move(obj)); + } + } + template <typename T, typename A> + static void Deserialize(IStream &stream, std::vector<T, A> *&vec) + { + vec = new std::vector<T, A>; + Deserialize(stream, *vec); + } + + // std::pair + template <typename A, typename B> + static void Deserialize(IStream &stream, std::pair<A, B> &p) + { + Deserialize(stream, p.first); + Deserialize(stream, p.second); + } + template <typename A, typename B> + static void Deserialize(IStream &stream, std::pair<A, B> *&p) + { + p = new std::pair<A, B>; + Deserialize(stream, *p); + } + + // std::map + template <typename K, typename T> + static void Deserialize(IStream &stream, std::map<K, T> &map) + { + int length; + stream.Read(sizeof(length), &length); + + for (int i = 0; i < length; ++i) { + K key; + T obj; + Deserialize(stream, key); + Deserialize(stream, obj); + map[key] = std::move(obj); + } + } + template <typename K, typename T> + static void Deserialize(IStream &stream, std::map<K, T> *&map) + { + map = new std::map<K, T>; + Deserialize(stream, *map); + } }; // struct Deserialization // generic serialization @@ -457,18 +458,20 @@ struct Serializer; template <typename First, typename... Args> struct Serializer<First, Args...> : public Serializer<Args...> { - static void Serialize(IStream& stream, const First& f, const Args&... args) { - Serialization::Serialize(stream, f); - Serializer<Args...>::Serialize(stream, args...); - } + static void Serialize(IStream &stream, const First &f, const Args &... args) + { + Serialization::Serialize(stream, f); + Serializer<Args...>::Serialize(stream, args...); + } }; // end of recursion template <> struct Serializer<> { - static void Serialize(IStream&) { - return; - } + static void Serialize(IStream &) + { + return; + } }; // generic deserialization @@ -477,18 +480,20 @@ struct Deserializer; template <typename First, typename... Args> struct Deserializer<First, Args...> : public Deserializer<Args...> { - static void Deserialize(IStream& stream, First& f, Args&... args) { - Deserialization::Deserialize(stream, f); - Deserializer<Args...>::Deserialize(stream, args...); - } + static void Deserialize(IStream &stream, First &f, Args &... args) + { + Deserialization::Deserialize(stream, f); + Deserializer<Args...>::Deserialize(stream, args...); + } }; // end of recursion template <> struct Deserializer<> { - static void Deserialize(IStream&) { - return; - } + static void Deserialize(IStream &) + { + return; + } }; } // CCHECKER diff --git a/src/dpl/core/include/cchecker/dpl/string.h b/src/dpl/core/include/cchecker/dpl/string.h index d9c3bbd..5d12e17 100644 --- a/src/dpl/core/include/cchecker/dpl/string.h +++ b/src/dpl/core/include/cchecker/dpl/string.h @@ -33,47 +33,46 @@ namespace CCHECKER { typedef std::basic_string<wchar_t, CharTraits> String; // @brief String exception class -class StringException -{ - public: - DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) +class StringException { +public: + DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) - // @brief Invalid init for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32) + // @brief Invalid init for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32) - // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8) + // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8) - // @brief Invalid conversion for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32) + // @brief Invalid conversion for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32) - // @brief Invalid conversion for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8) + // @brief Invalid conversion for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8) - // @brief Invalid ASCII character detected in FromASCII - DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter) + // @brief Invalid ASCII character detected in FromASCII + DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter) - // @brief Invalid ASCII character detected in FromASCII - DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound) + // @brief Invalid ASCII character detected in FromASCII + DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound) }; //!\brief convert ASCII string to CCHECKER::String -String FromASCIIString(const std::string& aString); +String FromASCIIString(const std::string &aString); //!\brief convert UTF32 string to CCHECKER::String -String FromUTF32String(const std::wstring& aString); +String FromUTF32String(const std::wstring &aString); //@brief Returns String object created from UTF8 string //@param[in] aString input UTF-8 string -String FromUTF8String(const std::string& aString); +String FromUTF8String(const std::string &aString); //@brief Returns String content as std::string -std::string ToUTF8String(const String& aString); +std::string ToUTF8String(const String &aString); //@brief Compare two unicode strings int StringCompare(const String &left, - const String &right, - bool caseInsensitive = false); + const String &right, + bool caseInsensitive = false); //@brief Splits the string into substrings. //@param[in] str Input string @@ -81,61 +80,63 @@ int StringCompare(const String &left, // delimiters. Can be also a single delimiter character. //@param[in] it InserterIterator that is used to save the generated substrings. template<typename StringType, typename Delimiters, typename InserterIterator> -void Tokenize(const StringType& str, - const Delimiters& delimiters, - InserterIterator it, - bool ignoreEmpty = false) +void Tokenize(const StringType &str, + const Delimiters &delimiters, + InserterIterator it, + bool ignoreEmpty = false) { - typename StringType::size_type nextSearchStart = 0; - typename StringType::size_type pos; - typename StringType::size_type length; - - while (true) { - pos = str.find_first_of(delimiters, nextSearchStart); - length = - ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart; - - if (!ignoreEmpty || length > 0) { - *it = str.substr(nextSearchStart, length); - it++; - } - - if (pos == StringType::npos) { - return; - } - - nextSearchStart = pos + 1; - } + typename StringType::size_type nextSearchStart = 0; + typename StringType::size_type pos; + typename StringType::size_type length; + + while (true) { + pos = str.find_first_of(delimiters, nextSearchStart); + length = + ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart; + + if (!ignoreEmpty || length > 0) { + *it = str.substr(nextSearchStart, length); + it++; + } + + if (pos == StringType::npos) { + return; + } + + nextSearchStart = pos + 1; + } } namespace Utils { -template<typename T> class ConcatFunc : public std::binary_function<T, T, T> -{ +template<typename T> class ConcatFunc : public std::binary_function<T, T, T> { public: - explicit ConcatFunc(const T & val) : m_delim(val) {} - T operator()(const T & arg1, const T & arg2) const - { - return arg1 + m_delim + arg2; - } + explicit ConcatFunc(const T &val) : m_delim(val) {} + T operator()(const T &arg1, const T &arg2) const + { + return arg1 + m_delim + arg2; + } private: - T m_delim; + T m_delim; }; } template<typename ForwardIterator> -typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator end, typename ForwardIterator::value_type delim) +typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator end, + typename ForwardIterator::value_type delim) { - typedef typename ForwardIterator::value_type value; - if(begin == end) return value(); - Utils::ConcatFunc<value> func(delim); - ForwardIterator init = begin; - return std::accumulate(++begin, end, *init, func); + typedef typename ForwardIterator::value_type value; + + if (begin == end) return value(); + + Utils::ConcatFunc<value> func(delim); + ForwardIterator init = begin; + return std::accumulate(++begin, end, *init, func); } } //namespace CCHECKER -std::ostream& operator<<(std::ostream& aStream, const CCHECKER::String& aString); +std::ostream &operator<<(std::ostream &aStream, const CCHECKER::String &aString); #endif // CCHECKER_STRING diff --git a/src/dpl/core/src/assert.cpp b/src/dpl/core/src/assert.cpp index e367562..acaa958 100644 --- a/src/dpl/core/src/assert.cpp +++ b/src/dpl/core/src/assert.cpp @@ -28,41 +28,39 @@ namespace CCHECKER { void AssertProc(const char *condition, - const char *file, - int line, - const char *function) + const char *file, + int line, + const char *function) { #define INTERNAL_LOG(message) \ - do \ - { \ - std::ostringstream platformLog; \ - platformLog << message; \ - JournalLog(LOG_DEBUG, \ - platformLog.str().c_str(), \ - __FILE__, __LINE__, __FUNCTION__); \ - } \ - while (0) + do \ + { \ + std::ostringstream platformLog; \ + platformLog << message; \ + JournalLog(LOG_DEBUG, \ + platformLog.str().c_str(), \ + __FILE__, __LINE__, __FUNCTION__); \ + } \ + while (0) + // Try to log failed assertion to log system + Try { + INTERNAL_LOG( + "################################################################################"); + INTERNAL_LOG( + "### cert-checker assertion failed! ###"); + INTERNAL_LOG( + "################################################################################"); + INTERNAL_LOG("### Condition: " << condition); + INTERNAL_LOG("### File: " << file); + INTERNAL_LOG("### Line: " << line); + INTERNAL_LOG("### Function: " << function); + INTERNAL_LOG( + "################################################################################"); + } catch (Exception) { + // Just ignore possible double errors + } - // Try to log failed assertion to log system - Try - { - INTERNAL_LOG( - "################################################################################"); - INTERNAL_LOG( - "### cert-checker assertion failed! ###"); - INTERNAL_LOG( - "################################################################################"); - INTERNAL_LOG("### Condition: " << condition); - INTERNAL_LOG("### File: " << file); - INTERNAL_LOG("### Line: " << line); - INTERNAL_LOG("### Function: " << function); - INTERNAL_LOG( - "################################################################################"); - } catch (Exception) { - // Just ignore possible double errors - } - - // Fail with c-library abort - abort(); + // Fail with c-library abort + abort(); } } // namespace CCHECKER diff --git a/src/dpl/core/src/colors.cpp b/src/dpl/core/src/colors.cpp index 272c06b..1f380b9 100644 --- a/src/dpl/core/src/colors.cpp +++ b/src/dpl/core/src/colors.cpp @@ -27,46 +27,46 @@ namespace CCHECKER { namespace Colors { namespace Text { -const char* BOLD_GREEN_BEGIN = "\033[1;32m"; -const char* BOLD_GREEN_END = "\033[m"; -const char* RED_BEGIN = "\033[0;31m"; -const char* RED_END = "\033[m"; -const char* PURPLE_BEGIN = "\033[0;35m"; -const char* PURPLE_END = "\033[m"; -const char* GREEN_BEGIN = "\033[0;32m"; -const char* GREEN_END = "\033[m"; -const char* CYAN_BEGIN = "\033[0;36m"; -const char* CYAN_END = "\033[m"; -const char* BOLD_RED_BEGIN = "\033[1;31m"; -const char* BOLD_RED_END = "\033[m"; -const char* BOLD_YELLOW_BEGIN = "\033[1;33m"; -const char* BOLD_YELLOW_END = "\033[m"; -const char* BOLD_GOLD_BEGIN = "\033[0;33m"; -const char* BOLD_GOLD_END = "\033[m"; -const char* BOLD_WHITE_BEGIN = "\033[1;37m"; -const char* BOLD_WHITE_END = "\033[m"; -const char* COLOR_END = "\033[m"; +const char *BOLD_GREEN_BEGIN = "\033[1;32m"; +const char *BOLD_GREEN_END = "\033[m"; +const char *RED_BEGIN = "\033[0;31m"; +const char *RED_END = "\033[m"; +const char *PURPLE_BEGIN = "\033[0;35m"; +const char *PURPLE_END = "\033[m"; +const char *GREEN_BEGIN = "\033[0;32m"; +const char *GREEN_END = "\033[m"; +const char *CYAN_BEGIN = "\033[0;36m"; +const char *CYAN_END = "\033[m"; +const char *BOLD_RED_BEGIN = "\033[1;31m"; +const char *BOLD_RED_END = "\033[m"; +const char *BOLD_YELLOW_BEGIN = "\033[1;33m"; +const char *BOLD_YELLOW_END = "\033[m"; +const char *BOLD_GOLD_BEGIN = "\033[0;33m"; +const char *BOLD_GOLD_END = "\033[m"; +const char *BOLD_WHITE_BEGIN = "\033[1;37m"; +const char *BOLD_WHITE_END = "\033[m"; +const char *COLOR_END = "\033[m"; } //namespace Text namespace Html { -const char* BOLD_GREEN_BEGIN = "<font color=\"green\"><b>"; -const char* BOLD_GREEN_END = "</b></font>"; -const char* PURPLE_BEGIN = "<font color=\"purple\"><b>"; -const char* PURPLE_END = "</b></font>"; -const char* RED_BEGIN = "<font color=\"red\"><b>"; -const char* RED_END = "</b></font>"; -const char* GREEN_BEGIN = "<font color=\"green\">"; -const char* GREEN_END = "</font>"; -const char* CYAN_BEGIN = "<font color=\"cyan\">"; -const char* CYAN_END = "</font>"; -const char* BOLD_RED_BEGIN = "<font color=\"red\"><b>"; -const char* BOLD_RED_END = "</b></font>"; -const char* BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>"; -const char* BOLD_YELLOW_END = "</b></font>"; -const char* BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>"; -const char* BOLD_GOLD_END = "</b></font>"; -const char* BOLD_WHITE_BEGIN = "<font color=\"white\"><b>"; -const char* BOLD_WHITE_END = "</b></font>"; +const char *BOLD_GREEN_BEGIN = "<font color=\"green\"><b>"; +const char *BOLD_GREEN_END = "</b></font>"; +const char *PURPLE_BEGIN = "<font color=\"purple\"><b>"; +const char *PURPLE_END = "</b></font>"; +const char *RED_BEGIN = "<font color=\"red\"><b>"; +const char *RED_END = "</b></font>"; +const char *GREEN_BEGIN = "<font color=\"green\">"; +const char *GREEN_END = "</font>"; +const char *CYAN_BEGIN = "<font color=\"cyan\">"; +const char *CYAN_END = "</font>"; +const char *BOLD_RED_BEGIN = "<font color=\"red\"><b>"; +const char *BOLD_RED_END = "</b></font>"; +const char *BOLD_YELLOW_BEGIN = "<font color=\"yellow\"><b>"; +const char *BOLD_YELLOW_END = "</b></font>"; +const char *BOLD_GOLD_BEGIN = "<font color=\"gold\"><b>"; +const char *BOLD_GOLD_END = "</b></font>"; +const char *BOLD_WHITE_BEGIN = "<font color=\"white\"><b>"; +const char *BOLD_WHITE_END = "</b></font>"; } //namespace Html } //namespace Colors } //namespace CCHECKER diff --git a/src/dpl/core/src/errno_string.cpp b/src/dpl/core/src/errno_string.cpp index 5b1de37..fa04a45 100644 --- a/src/dpl/core/src/errno_string.cpp +++ b/src/dpl/core/src/errno_string.cpp @@ -34,66 +34,65 @@ #include <cchecker/dpl/scoped_free.h> namespace CCHECKER { -namespace // anonymous -{ +namespace { // anonymous const size_t DEFAULT_ERRNO_STRING_SIZE = 32; } // namespace anonymous std::string GetErrnoString(int error) { - size_t size = DEFAULT_ERRNO_STRING_SIZE; - char *buffer = NULL; - - for (;;) { - // Add one extra characted for end of string null value - char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1)); + size_t size = DEFAULT_ERRNO_STRING_SIZE; + char *buffer = NULL; - if (!newBuffer) { - // Failed to realloc - ::free(buffer); - throw std::bad_alloc(); - } + for (;;) { + // Add one extra characted for end of string null value + char *newBuffer = static_cast<char *>(::realloc(buffer, size + 1)); - // Setup reallocated buffer - buffer = newBuffer; - ::memset(buffer, 0, size + 1); + if (!newBuffer) { + // Failed to realloc + ::free(buffer); + throw std::bad_alloc(); + } - // Try to retrieve error string + // Setup reallocated buffer + buffer = newBuffer; + ::memset(buffer, 0, size + 1); + // Try to retrieve error string #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE - // The XSI-compliant version of strerror_r() is provided if: - int result = ::strerror_r(error, buffer, size); + // The XSI-compliant version of strerror_r() is provided if: + int result = ::strerror_r(error, buffer, size); + + if (result == 0) { + ScopedFree<char> scopedBufferFree(buffer); + return std::string(buffer); + } - if (result == 0) { - ScopedFree<char> scopedBufferFree(buffer); - return std::string(buffer); - } #else - errno = 0; + errno = 0; + // Otherwise, the GNU-specific version is provided. + char *result = ::strerror_r(error, buffer, size); - // Otherwise, the GNU-specific version is provided. - char *result = ::strerror_r(error, buffer, size); + if (result != NULL) { + ScopedFree<char> scopedBufferFree(buffer); + return std::string(result); + } - if (result != NULL) { - ScopedFree<char> scopedBufferFree(buffer); - return std::string(result); - } #endif - // Interpret errors - switch (errno) { - case EINVAL: - // We got an invalid errno value - ::free(buffer); - ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error); + // Interpret errors + switch (errno) { + case EINVAL: + // We got an invalid errno value + ::free(buffer); + ThrowMsg(InvalidErrnoValue, "Invalid errno value: " << error); - case ERANGE: - // Incease buffer size and retry - size <<= 1; - continue; + case ERANGE: + // Incease buffer size and retry + size <<= 1; + continue; - default: - Assert(0 && "Invalid errno value after call to strerror_r!"); - } - } + default: + Assert(0 && "Invalid errno value after call to strerror_r!"); + } + } } } // namespace CCHECKER diff --git a/src/dpl/core/src/exception.cpp b/src/dpl/core/src/exception.cpp index bee161b..66024fc 100644 --- a/src/dpl/core/src/exception.cpp +++ b/src/dpl/core/src/exception.cpp @@ -26,32 +26,30 @@ #include "common/log.h" namespace CCHECKER { -Exception* Exception::m_lastException = NULL; +Exception *Exception::m_lastException = NULL; unsigned int Exception::m_exceptionCount = 0; void (*Exception::m_terminateHandler)() = NULL; void LogUnhandledException(const std::string &str) { - // Logging to console - printf("%s\n", str.c_str()); - - // Logging to dlog - LogDebug(str); + // Logging to console + printf("%s\n", str.c_str()); + // Logging to dlog + LogDebug(str); } void LogUnhandledException(const std::string &str, - const char *filename, - int line, - const char *function) + const char *filename, + int line, + const char *function) { - // Logging to console - std::ostringstream msg; - msg << "\033[1;5;31m\n=== [" << filename << ":" << line << "] " << - function << " ===\033[m"; - msg << str; - printf("%s\n", msg.str().c_str()); - - // Logging to dlog - LogError(str.c_str() << filename << line << function); + // Logging to console + std::ostringstream msg; + msg << "\033[1;5;31m\n=== [" << filename << ":" << line << "] " << + function << " ===\033[m"; + msg << str; + printf("%s\n", msg.str().c_str()); + // Logging to dlog + LogError(str.c_str() << filename << line << function); } } // namespace CCHECKER diff --git a/src/dpl/core/src/string.cpp b/src/dpl/core/src/string.cpp index 7c8223d..72ade02 100644 --- a/src/dpl/core/src/string.cpp +++ b/src/dpl/core/src/string.cpp @@ -37,215 +37,197 @@ // TODO: Completely move to ICU namespace CCHECKER { -namespace //anonymous -{ -class ASCIIValidator -{ - const std::string& m_TestedString; +namespace { //anonymous +class ASCIIValidator { + const std::string &m_TestedString; - public: - ASCIIValidator(const std::string& aTestedString); +public: + ASCIIValidator(const std::string &aTestedString); - void operator()(signed char aCharacter) const; + void operator()(signed char aCharacter) const; }; -ASCIIValidator::ASCIIValidator(const std::string& aTestedString) : - m_TestedString(aTestedString) +ASCIIValidator::ASCIIValidator(const std::string &aTestedString) : + m_TestedString(aTestedString) {} void ASCIIValidator::operator()(signed char aCharacter) const { - // Check for ASCII data range - if (aCharacter <= 0) { - ThrowMsg( - StringException::InvalidASCIICharacter, - "invalid character code " << static_cast<int>(aCharacter) - << " from string [" << - m_TestedString - << "] passed as ASCII"); - } + // Check for ASCII data range + if (aCharacter <= 0) { + ThrowMsg( + StringException::InvalidASCIICharacter, + "invalid character code " << static_cast<int>(aCharacter) + << " from string [" << + m_TestedString + << "] passed as ASCII"); + } } const iconv_t gc_IconvOperError = reinterpret_cast<iconv_t>(-1); const size_t gc_IconvConvertError = static_cast<size_t>(-1); } // namespace anonymous -String FromUTF8String(const std::string& aIn) +String FromUTF8String(const std::string &aIn) { - if (aIn.empty()) { - return String(); - } - - size_t inbytes = aIn.size(); - - // Default iconv UTF-32 module adds BOM (4 bytes) in from of string - // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32 - // newsize = oldsize * 4 + end + bom - // newsize - bytes for UTF-32 string - // oldsize - letters in UTF-8 string - // end - end character for UTF-32 (\0) - // bom - Unicode header in front of string (0xfeff) - size_t outbytes = sizeof(wchar_t) * (inbytes + 2); - std::vector<wchar_t> output(inbytes + 2, 0); - - size_t outbytesleft = outbytes; - char* inbuf = const_cast<char*>(aIn.c_str()); - - // vector is used to provide buffer for iconv which expects char* buffer - // but during conversion from UTF32 uses internaly wchar_t - char* outbuf = reinterpret_cast<char*>(&output[0]); - - iconv_t iconvHandle = iconv_open("UTF-32", "UTF-8"); - - if (gc_IconvOperError == iconvHandle) { - int error = errno; - - ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32, - "iconv_open failed for " << "UTF-32 <- UTF-8" << - "error: " << GetErrnoString(error)); - } - - size_t iconvRet = iconv(iconvHandle, - &inbuf, - &inbytes, - &outbuf, - &outbytesleft); - - iconv_close(iconvHandle); - - if (gc_IconvConvertError == iconvRet) { - ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32, - "iconv failed for " << "UTF-32 <- UTF-8" << "error: " - << GetErrnoString()); - } - - // Ignore BOM in front of UTF-32 - return &output[1]; + if (aIn.empty()) { + return String(); + } + + size_t inbytes = aIn.size(); + // Default iconv UTF-32 module adds BOM (4 bytes) in from of string + // The worst case is when 8bit UTF-8 char converts to 32bit UTF-32 + // newsize = oldsize * 4 + end + bom + // newsize - bytes for UTF-32 string + // oldsize - letters in UTF-8 string + // end - end character for UTF-32 (\0) + // bom - Unicode header in front of string (0xfeff) + size_t outbytes = sizeof(wchar_t) * (inbytes + 2); + std::vector<wchar_t> output(inbytes + 2, 0); + size_t outbytesleft = outbytes; + char *inbuf = const_cast<char *>(aIn.c_str()); + // vector is used to provide buffer for iconv which expects char* buffer + // but during conversion from UTF32 uses internaly wchar_t + char *outbuf = reinterpret_cast<char *>(&output[0]); + iconv_t iconvHandle = iconv_open("UTF-32", "UTF-8"); + + if (gc_IconvOperError == iconvHandle) { + int error = errno; + ThrowMsg(StringException::IconvInitErrorUTF8ToUTF32, + "iconv_open failed for " << "UTF-32 <- UTF-8" << + "error: " << GetErrnoString(error)); + } + + size_t iconvRet = iconv(iconvHandle, + &inbuf, + &inbytes, + &outbuf, + &outbytesleft); + iconv_close(iconvHandle); + + if (gc_IconvConvertError == iconvRet) { + ThrowMsg(StringException::IconvConvertErrorUTF8ToUTF32, + "iconv failed for " << "UTF-32 <- UTF-8" << "error: " + << GetErrnoString()); + } + + // Ignore BOM in front of UTF-32 + return &output[1]; } -std::string ToUTF8String(const CCHECKER::String& aIn) +std::string ToUTF8String(const CCHECKER::String &aIn) { - if (aIn.empty()) { - return std::string(); - } - - size_t inbytes = aIn.size() * sizeof(wchar_t); - size_t outbytes = inbytes + sizeof(char); - - // wstring returns wchar_t but iconv expects char* - // iconv internally is processing input as wchar_t - char* inbuf = reinterpret_cast<char*>(const_cast<wchar_t*>(aIn.c_str())); - std::vector<char> output(inbytes, 0); - char* outbuf = &output[0]; - - size_t outbytesleft = outbytes; - - iconv_t iconvHandle = iconv_open("UTF-8", "UTF-32"); - - if (gc_IconvOperError == iconvHandle) { - ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8, - "iconv_open failed for " << "UTF-8 <- UTF-32" - << "error: " << GetErrnoString()); - } - - size_t iconvRet = iconv(iconvHandle, - &inbuf, - &inbytes, - &outbuf, - &outbytesleft); - - iconv_close(iconvHandle); - - if (gc_IconvConvertError == iconvRet) { - ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8, - "iconv failed for " << "UTF-8 <- UTF-32" - << "error: " << GetErrnoString()); - } - - return &output[0]; + if (aIn.empty()) { + return std::string(); + } + + size_t inbytes = aIn.size() * sizeof(wchar_t); + size_t outbytes = inbytes + sizeof(char); + // wstring returns wchar_t but iconv expects char* + // iconv internally is processing input as wchar_t + char *inbuf = reinterpret_cast<char *>(const_cast<wchar_t *>(aIn.c_str())); + std::vector<char> output(inbytes, 0); + char *outbuf = &output[0]; + size_t outbytesleft = outbytes; + iconv_t iconvHandle = iconv_open("UTF-8", "UTF-32"); + + if (gc_IconvOperError == iconvHandle) { + ThrowMsg(StringException::IconvInitErrorUTF32ToUTF8, + "iconv_open failed for " << "UTF-8 <- UTF-32" + << "error: " << GetErrnoString()); + } + + size_t iconvRet = iconv(iconvHandle, + &inbuf, + &inbytes, + &outbuf, + &outbytesleft); + iconv_close(iconvHandle); + + if (gc_IconvConvertError == iconvRet) { + ThrowMsg(StringException::IconvConvertErrorUTF32ToUTF8, + "iconv failed for " << "UTF-8 <- UTF-32" + << "error: " << GetErrnoString()); + } + + return &output[0]; } -String FromASCIIString(const std::string& aString) +String FromASCIIString(const std::string &aString) { - String output; - - std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString)); - std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output)); - - return output; + String output; + std::for_each(aString.begin(), aString.end(), ASCIIValidator(aString)); + std::copy(aString.begin(), aString.end(), std::back_inserter<String>(output)); + return output; } -String FromUTF32String(const std::wstring& aString) +String FromUTF32String(const std::wstring &aString) { - return String(&aString[0]); + return String(&aString[0]); } static UChar *ConvertToICU(const String &inputString) { - ScopedArray<UChar> outputString; - int32_t size = 0; - int32_t convertedSize = 0; - UErrorCode error = U_ZERO_ERROR; - - // Calculate size of output string - ::u_strFromWCS(NULL, - 0, - &size, - inputString.c_str(), - -1, - &error); - - if (error == U_ZERO_ERROR || - error == U_BUFFER_OVERFLOW_ERROR) - { - // What buffer size is ok ? - LogDebug("ICU: Output buffer size: " << size); - } else { - ThrowMsg(StringException::ICUInvalidCharacterFound, - "ICU: Failed to retrieve output string size. Error: " - << error); - } - - // Allocate proper buffer - outputString.Reset(new UChar[size + 1]); - ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1)); - - error = U_ZERO_ERROR; - - // Do conversion - ::u_strFromWCS(outputString.Get(), - size + 1, - &convertedSize, - inputString.c_str(), - -1, - &error); - - if (!U_SUCCESS(error)) { - ThrowMsg(StringException::ICUInvalidCharacterFound, - "ICU: Failed to convert string. Error: " << error); - } - - // Done - return outputString.Release(); + ScopedArray<UChar> outputString; + int32_t size = 0; + int32_t convertedSize = 0; + UErrorCode error = U_ZERO_ERROR; + // Calculate size of output string + ::u_strFromWCS(NULL, + 0, + &size, + inputString.c_str(), + -1, + &error); + + if (error == U_ZERO_ERROR || + error == U_BUFFER_OVERFLOW_ERROR) { + // What buffer size is ok ? + LogDebug("ICU: Output buffer size: " << size); + } else { + ThrowMsg(StringException::ICUInvalidCharacterFound, + "ICU: Failed to retrieve output string size. Error: " + << error); + } + + // Allocate proper buffer + outputString.Reset(new UChar[size + 1]); + ::memset(outputString.Get(), 0, sizeof(UChar) * (size + 1)); + error = U_ZERO_ERROR; + // Do conversion + ::u_strFromWCS(outputString.Get(), + size + 1, + &convertedSize, + inputString.c_str(), + -1, + &error); + + if (!U_SUCCESS(error)) { + ThrowMsg(StringException::ICUInvalidCharacterFound, + "ICU: Failed to convert string. Error: " << error); + } + + // Done + return outputString.Release(); } int StringCompare(const String &left, - const String &right, - bool caseInsensitive) + const String &right, + bool caseInsensitive) { - // Convert input strings - ScopedArray<UChar> leftICU(ConvertToICU(left)); - ScopedArray<UChar> rightICU(ConvertToICU(right)); - - if (caseInsensitive) { - return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0)); - } else { - return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get())); - } + // Convert input strings + ScopedArray<UChar> leftICU(ConvertToICU(left)); + ScopedArray<UChar> rightICU(ConvertToICU(right)); + + if (caseInsensitive) { + return static_cast<int>(u_strcasecmp(leftICU.Get(), rightICU.Get(), 0)); + } else { + return static_cast<int>(u_strcmp(leftICU.Get(), rightICU.Get())); + } } } //namespace CCHECKER -std::ostream& operator<<(std::ostream& aStream, const CCHECKER::String& aString) +std::ostream &operator<<(std::ostream &aStream, const CCHECKER::String &aString) { - return aStream << CCHECKER::ToUTF8String(aString); + return aStream << CCHECKER::ToUTF8String(aString); } |