[icinga-checkins] icinga.org: icinga2/feature/box-5024: Implement boxing/unboxing support.

git at icinga.org git at icinga.org
Thu Nov 7 18:22:49 CET 2013


Module: icinga2
Branch: feature/box-5024
Commit: e94aa59f2b459a839600ffa6c607f28666997f90
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=e94aa59f2b459a839600ffa6c607f28666997f90

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Thu Nov  7 17:28:29 2013 +0100

Implement boxing/unboxing support.

---

 lib/base/box.h      |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/CMakeLists.txt |   10 +++++----
 test/base-box.cpp   |   53 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 4 deletions(-)

diff --git a/lib/base/box.h b/lib/base/box.h
new file mode 100644
index 0000000..68125f4
--- /dev/null
+++ b/lib/base/box.h
@@ -0,0 +1,61 @@
+#ifndef BOX_H
+#define BOX_H
+
+#include "base/object.h"
+
+namespace icinga
+{
+
+class BoxedValue : public Object
+{
+public:
+	DECLARE_PTR_TYPEDEFS(BoxedValue);
+
+	virtual const void *GetPointer(void) const = 0;
+	virtual const std::type_info& GetType(void) const = 0;
+};
+
+template<typename T>
+class BoxedValueImpl : public BoxedValue
+{
+public:
+	DECLARE_PTR_TYPEDEFS(BoxedValueImpl<T>);
+
+	BoxedValueImpl(const T& value)
+		: m_Value(value)
+	{ }
+
+	virtual const void *GetPointer(void) const
+	{
+		return &m_Value;
+	}
+
+	virtual const std::type_info& GetType(void) const
+	{
+		return typeid(T);
+	}
+
+private:
+	T m_Value;
+};
+
+template<typename T>
+Object::Ptr Box(const T& value)
+{
+	return make_shared<BoxedValueImpl<T> >(value);
+}
+
+template<typename T>
+T Unbox(const Object::Ptr& object)
+{
+	BoxedValue::Ptr box = dynamic_pointer_cast<BoxedValue>(object);
+
+	if (!box || typeid(T) != box->GetType())
+		throw std::bad_cast();
+
+	return *reinterpret_cast<const T *>(box->GetPointer());
+}
+
+}
+
+#endif /* BOX_H */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 0d2088a..01029e9 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -18,10 +18,10 @@
 include(BoostTestTargets)
 
 add_boost_test(base
-  SOURCES base-array.cpp base-convert.cpp base-dictionary.cpp base-fifo.cpp
-          base-match.cpp base-netstring.cpp base-object.cpp base-shellescape.cpp
-          base-stacktrace.cpp base-string.cpp base-timer.cpp base-value.cpp
-          icinga-perfdata.cpp test.cpp
+  SOURCES base-array.cpp base-box.cpp base-convert.cpp base-dictionary.cpp
+          base-fifo.cpp base-match.cpp base-netstring.cpp base-object.cpp
+          base-shellescape.cpp base-stacktrace.cpp base-string.cpp
+          base-timer.cpp base-value.cpp icinga-perfdata.cpp test.cpp
   LIBRARIES base config icinga
   TESTS base_array/construct
         base_array/getset
@@ -29,6 +29,8 @@ add_boost_test(base
         base_array/foreach
         base_array/clone
         base_array/serialize
+        base_box/simple
+        base_box/strukt
         base_convert/tolong
         base_convert/todouble
         base_convert/tostring
diff --git a/test/base-box.cpp b/test/base-box.cpp
new file mode 100644
index 0000000..8288a7e
--- /dev/null
+++ b/test/base-box.cpp
@@ -0,0 +1,53 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "base/box.h"
+#include "base/qstring.h"
+#include <boost/test/unit_test.hpp>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_box)
+
+BOOST_AUTO_TEST_CASE(simple)
+{
+	Object::Ptr obj = Box(10);
+	int num = Unbox<int>(obj);
+	BOOST_CHECK(num == 10);
+}
+
+struct test_struct
+{
+	String str;
+	int num;
+};
+
+BOOST_AUTO_TEST_CASE(strukt)
+{
+	test_struct s = { "Hello World", 7 };
+
+	Object::Ptr obj = Box(s);
+
+	test_struct s2 = Unbox<test_struct>(obj);
+
+	BOOST_CHECK(s2.str == "Hello World");
+	BOOST_CHECK(s2.num == 7);
+}
+
+BOOST_AUTO_TEST_SUITE_END()





More information about the icinga-checkins mailing list