Helper functions for log/exception message concatenation
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 29 Oct 2014 09:48:22 +0000 (10:48 +0100)
committerMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Tue, 17 Feb 2015 10:00:04 +0000 (11:00 +0100)
Change-Id: I0c7d1146009924f7765af5f3602b46e2e8c8a094

src/manager/common/stringify.h [new file with mode: 0644]

diff --git a/src/manager/common/stringify.h b/src/manager/common/stringify.h
new file mode 100644 (file)
index 0000000..58d6fda
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file       stringify.h
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#pragma once
+
+#include <sstream>
+#include <string>
+
+namespace CKM {
+
+/*
+ * Helper functions for easy argument concatenation. Can be used in logs and exceptions.
+ * Ex)
+ * template <typename... Args>
+ * std::runtime_error my_exception(const Args&... args)
+ * {
+ *     return std::runtime_error(stringify(args...));
+ * };
+ *
+ * throw my_exception("Function foo has failed. Status: ", status, " error code: ", error);
+ */
+
+std::string stringify() {
+    return std::string();
+}
+
+void concatenate(std::ostringstream&) {}
+
+template <typename T, typename... Args>
+void concatenate(std::ostringstream& stream, const T& arg1, const Args&... args) {
+    stream << arg1;
+    concatenate(stream, args...);
+}
+
+template <typename T, typename... Args>
+std::string stringify(const T& arg1, const Args&... args){
+    std::ostringstream stream;
+    concatenate(stream, arg1, args...);
+    return stream.str();
+}
+
+} // namespace CKM