OSDN Git Service

boost.type_erasureを使う実装に変更
authorcaprice <caprice@users.sourceforge.jp>
Sun, 4 May 2014 14:56:42 +0000 (23:56 +0900)
committercaprice <caprice@users.sourceforge.jp>
Sun, 4 May 2014 14:56:42 +0000 (23:56 +0900)
include/communication/message.hpp

index 1dbb69a..8a1650f 100644 (file)
@@ -1,57 +1,59 @@
 
-#ifndef GIKOMONA_CORE_MESSAGE_HPP
-#define GIKOMONA_CORE_MESSAGE_HPP
+#ifndef GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP
+#define GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP
 
 #include <sstream>
 
+#include <boost/type_erasure/any.hpp>
+#include <boost/type_erasure/constructible.hpp>
+#include <boost/type_erasure/member.hpp>
+#include <boost/mpl/vector.hpp>
+
 #include "string.hpp"
-#include "communication.hpp"
 
 namespace monazilla { namespace GikoMona { namespace core { namespace communication {
     
-class message_concept {
-public:
-    message_concept(const mona_string& str);
-    ~message_concept() = default;
+BOOST_TYPE_ERASURE_MEMBER(has_to_raw, to_raw, 0)
+BOOST_TYPE_ERASURE_MEMBER(has_msg_type, get_type, 0)
+
+typedef mona_string raw_message;
     
-    static mona_string to_string() const {}
+enum class message_type {
+    FAILURE,
+    REQUEST,
+    SUCCEED
 };
 
-template <typename T>
-class is_satisfied_with_message_concept {
-    const static bool value = false;
-};
+typedef
+    boost::mpl::vector<
+        has_to_raw<raw_message()>,
+        has_msg_type<message_type()>
+    > message_requirements
+    ;
     
-template <typename T>
-constexpr bool is_satisfied_with_message_concept_v() {
-    return is_satisfied_message_concept<T>::value;
-}
-
-#define DEFINE_MESSAGE(type, name, message)         \
-    class name {                                    \
-    public:                                         \
-        explicit                                    \
-        name (const mona_string& str) : msg(str) {} \
-        ~name() = default;                          \
-        mona_string to_string() const {             \
-            std::ostringstream str_builder;         \
-            str_builder << #type << ":"             \
-                        << message << "("           \
-                        << msg  << ").";            \
-            return convert_from(str_builder.str()); \
-        }                                           \
-    private:                                        \
-        const mona_string msg;                      \
-    };
-    
-template <typename T>
-bool is_same_mail(mona_string src, T&& obj) {
-    static_assert(is_satisfied_with_message_concept_v<T>(),
-                  "`T' does not satisfy the `message_concept'");
-    
-    return (src == obj.to_string());
-}
+typedef boost::type_erasure::any<message_requirements> message;
+
+#define DEFINE_MESSAGE(type_, name, message)            \
+    class name {                                        \
+    public:                                             \
+        explicit                                        \
+        name (const mona_string& str) : detail(str) {}  \
+        ~name() = default;                              \
+        raw_message to_raw() const {                    \
+            std::ostringstream str_builder;             \
+            str_builder << #type_ << ":"                \
+                        << message << "("               \
+                        << detail  << ").";             \
+            return static_cast<raw_message>(            \
+                convert_from(str_builder.str())         \
+            );                                          \
+        }                                               \
+        message_type get_type() { return type; }        \
+    private:                                            \
+        const mona_string detail;                       \
+        const message_type type = message_type::type_;  \
+};
 
 } } } }
 
-#endif // GIKOMONA_CORE_MESSAGE_HPP
+#endif // GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP