Replace stringify template to macro 25/111925/5
authorKyungwook Tak <k.tak@samsung.com>
Wed, 25 Jan 2017 04:12:44 +0000 (13:12 +0900)
committerkyungwook tak <k.tak@samsung.com>
Fri, 3 Feb 2017 06:39:08 +0000 (22:39 -0800)
Change-Id: Ifc6e0d65d903ec17c2669ddfa32c3b3b23a7bcb0
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/manager/common/exception.h
src/manager/common/stringify.h
src/manager/service/file-lock.cpp

index 380675f..874fbd0 100644 (file)
@@ -74,24 +74,21 @@ public:
 
 template <
        int Error = 0,
-       typename Stringify = StringifyAvoid,
+       bool IsDebug = true,
        typename Before = DefaultExceptionLogger,
-       typename After = DefaultExceptionLogger >
+       typename After = DefaultExceptionLogger>
 class COMMON_API DefineException : public Exception {
 public:
-       template<typename... Args>
        DefineException(const char *path, const char *function, int line,
-                                       const Args &... args)
-               : Exception(path, function, line, Stringify::Merge(args...))
+                                       const std::string &message)
+               : Exception(path, function, line, IsDebug ? std::string() : message)
        {
-               Before(m_path, m_function, m_line,
-                          DefineException<Error, Stringify, Before, After>::error(), m_message);
+               Before(m_path, m_function, m_line, Error, m_message);
        }
 
        ~DefineException() noexcept
        {
-               After(m_path, m_function, m_line,
-                         DefineException<Error, Stringify, Before, After>::error(), m_message);
+               After(m_path, m_function, m_line, Error, m_message);
        }
 
        virtual int error(void) const
@@ -116,29 +113,27 @@ public:
 };
 
 using InternalError =
-       DefineException<CKM_API_ERROR_SERVER_ERROR, Stringify, PrintError>;
+       DefineException<CKM_API_ERROR_SERVER_ERROR, false, PrintError>;
 using DatabaseLocked =
-       DefineException<CKM_API_ERROR_DB_LOCKED, Stringify, PrintError>;
+       DefineException<CKM_API_ERROR_DB_LOCKED, false, PrintError>;
 using DatabaseFailed =
-       DefineException<CKM_API_ERROR_DB_ERROR, StringifyError, PrintError>;
+       DefineException<CKM_API_ERROR_DB_ERROR, false, PrintError>;
 using FileSystemFailed =
-       DefineException<CKM_API_ERROR_FILE_SYSTEM, Stringify, PrintError>;
+       DefineException<CKM_API_ERROR_FILE_SYSTEM, false, PrintError>;
 using InputParam =
-       DefineException<CKM_API_ERROR_INPUT_PARAM, StringifyDebug, PrintDebug>;
+       DefineException<CKM_API_ERROR_INPUT_PARAM, true, PrintDebug>;
 using AuthenticationFailed =
-       DefineException<CKM_API_ERROR_AUTHENTICATION_FAILED, StringifyDebug, PrintDebug>;
+       DefineException<CKM_API_ERROR_AUTHENTICATION_FAILED, true, PrintDebug>;
 
 
 struct TransactionFailed : public DatabaseFailed {
-       template<typename... Args>
        TransactionFailed(const char *path, const char *function, int line,
-                                         const Args &... args)
-               : DatabaseFailed(path, function, line, args...) {}
+                                         const std::string &message)
+               : DatabaseFailed(path, function, line, message) {}
 };
 
 } // namespace Exc
 } // namespace CKM
 
 #define ThrowErr(name, ...) \
-       throw name(__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
-
+       throw name(__FILE__, __FUNCTION__, __LINE__, Stringify(__VA_ARGS__))
index 64edfa6..c8b32d2 100644 (file)
@@ -17,6 +17,7 @@
  * @file       stringify.h
  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
  * @author     Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @author     Kyungwook Tak (k.tak@samsung.com)
  * @version    1.0
  */
 #pragma once
 #include <sstream>
 #include <string>
 
-namespace CKM {
+#define PP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) N
+#define PP_RSEQ_N() 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
+#define PP_COMMASEQ_N 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0
 
-template <bool Mode>
-class StringifyBasic;
+#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
+#define PP_HASCOMMA(...) PP_NARG_(__VA_ARGS__, PP_COMMASEQ_N())
 
-template <>
-class StringifyBasic<false> {
-       StringifyBasic() = delete;
+#define PP_NARG_HELPER3_11(N)    N
+#define PP_NARG_HELPER3_00(N)    1
+#define PP_NARG_HELPER3_01(N)    0
+#define PP_NARG_HELPER2(a, b, N) PP_NARG_HELPER3_ ## a ## b(N)
+#define PP_NARG_HELPER1(a, b, N) PP_NARG_HELPER2(a, b, N)
+#define PP_HASARG() ,
 
-public:
-       static std::string Merge()
-       {
-               return std::string();
-       }
+#define PP_NARG(...)                           \
+    PP_NARG_HELPER1(                           \
+        PP_HASCOMMA(__VA_ARGS__),              \
+        PP_HASCOMMA(PP_HASARG __VA_ARGS__ ()), \
+        PP_NARG_(__VA_ARGS__, PP_RSEQ_N()))
 
-       template <typename... Args>
-       static std::string Merge(const Args &...)
-       {
-               return std::string();
-       }
-};
+#define CONCAT_(arg1, arg2) arg1 ## arg2
+#define CONCAT(arg1, arg2) CONCAT_(arg1, arg2)
 
-template <>
-class StringifyBasic<true> {
-       StringifyBasic() = delete;
+#define STRINGIFY_0() << ""
+#define STRINGIFY_1(msg) << msg
+#define STRINGIFY_2(msg, ...) << msg STRINGIFY_1(__VA_ARGS__)
+#define STRINGIFY_3(msg, ...) << msg STRINGIFY_2(__VA_ARGS__)
+#define STRINGIFY_4(msg, ...) << msg STRINGIFY_3(__VA_ARGS__)
+#define STRINGIFY_5(msg, ...) << msg STRINGIFY_4(__VA_ARGS__)
+#define STRINGIFY_6(msg, ...) << msg STRINGIFY_5(__VA_ARGS__)
+#define STRINGIFY_7(msg, ...) << msg STRINGIFY_6(__VA_ARGS__)
+#define STRINGIFY_8(msg, ...) << msg STRINGIFY_7(__VA_ARGS__)
+#define STRINGIFY_9(msg, ...) << msg STRINGIFY_8(__VA_ARGS__)
+#define STRINGIFY_10(msg, ...) << msg STRINGIFY_9(__VA_ARGS__)
+#define STRINGIFY_11(msg, ...) << msg STRINGIFY_10(__VA_ARGS__)
+#define STRINGIFY_12(msg, ...) << msg STRINGIFY_11(__VA_ARGS__)
+#define STRINGIFY_13(msg, ...) << msg STRINGIFY_12(__VA_ARGS__)
+#define STRINGIFY_14(msg, ...) << msg STRINGIFY_13(__VA_ARGS__)
+#define STRINGIFY_15(msg, ...) << msg STRINGIFY_14(__VA_ARGS__)
+#define STRINGIFY_(N, ...) CONCAT(STRINGIFY_, N)(__VA_ARGS__)
 
-       static void Concatenate(std::ostringstream &) {}
+#define Stringify(...)                                        \
+    (static_cast<std::ostringstream &>(std::ostringstream()   \
+        STRINGIFY_(PP_NARG(__VA_ARGS__), __VA_ARGS__))).str()
 
-       template <typename t, typename... Args>
-       static void Concatenate(std::ostringstream &stream, const t &arg1,
-                                                       const Args &... args)
-       {
-               stream << arg1;
-               Concatenate(stream, args...);
-       }
-
-public:
-       static std::string Merge()
-       {
-               return std::string();
-       }
-
-       template <typename T, typename... Args>
-       static std::string Merge(const T &arg1, const Args &... args)
-       {
-               std::ostringstream stream;
-               Concatenate(stream, arg1, args...);
-               return stream.str();
-       }
-};
+#define StringifyAvoid(...) std::string()
+#define StringifyError(...) Stringify(__VA_ARGS__)
 
 #ifdef DEBUG
-#define DEBUG_STATUS true
+#define StringifyDebug(...) Stringify(__VA_ARGS__)
 #else
-#define DEBUG_STATUS false
+#define StringifyDebug(...) std::string()
 #endif
-
-using Stringify = StringifyBasic<true>;
-using StringifyAvoid = StringifyBasic<false>;
-using StringifyError = StringifyBasic<true>;
-using StringifyDebug = StringifyBasic<DEBUG_STATUS>;
-
-#undef DEBUG_STATUS
-
-} // namespace CKM
index e7e4032..1c5f107 100644 (file)
 #include <string>
 #include <sstream>
 
-#include <stringify.h>
+#include <exception.h>
 #include <dpl/errno_string.h>
 
 namespace CKM {
 
-namespace {
-
-// TODO replace it with custom exception when they are implemented
-template <typename... Args>
-std::runtime_error io_exception(const Args &... args)
-{
-       return std::runtime_error(Stringify::Merge(args...));
-};
-
-} // namespace anonymous
-
 FileLock::FileLock(const char *const file)
 {
        // Open lock file
        m_lockFd = TEMP_FAILURE_RETRY(creat(file, 0644));
 
        if (m_lockFd == -1)
-               throw io_exception("Cannot open lock file. Errno: ", GetErrnoString());
+               ThrowErr(Exc::FileSystemFailed,
+                       "Cannot open lock file. errno: ", GetErrnoString());
 
        if (-1 == lockf(m_lockFd, F_TLOCK, 0)) {
                if (errno == EACCES || errno == EAGAIN)
-                       throw io_exception("Can't acquire lock. Another instance must be running.");
+                       ThrowErr(Exc::FileSystemFailed,
+                               "Can't acquire lock. Another instance is running");
                else
-                       throw io_exception("Can't acquire lock. Errno: ", GetErrnoString());
+                       ThrowErr(Exc::FileSystemFailed,
+                               "Can't acquire lock. errno: ", GetErrnoString());
        }
 
        std::string pid = std::to_string(getpid());
@@ -67,12 +59,13 @@ FileLock::FileLock(const char *const file)
        ssize_t written = TEMP_FAILURE_RETRY(write(m_lockFd, pid.c_str(), pid.size()));
 
        if (-1 == written || static_cast<ssize_t>(pid.size()) > written)
-               throw io_exception("Can't write file lock. Errno: ", GetErrnoString());
+               ThrowErr(Exc::FileSystemFailed,
+                       "Can't write file lock. errno: ", GetErrnoString());
 
        int ret = fsync(m_lockFd);
 
        if (-1 == ret)
-               throw io_exception("Fsync failed. Errno: ", GetErrnoString());
+               ThrowErr(Exc::FileSystemFailed, "Fsync failed. errno: ", GetErrnoString());
 }
 
 FileLock::~FileLock()