GikoMonaのソフトウェアの基幹部分を集めて1つのライブラリに集約したものです。gikomona/pnutsのコンパイルにはこのライブラリが必須です。
Revision | 769e387502aced37dde41f56105fe293f1fb4ad7 (tree) |
---|---|
Time | 2014-05-04 23:56:42 |
Author | caprice <caprice@user...> |
Commiter | caprice |
boost.type_erasureを使う実装に変更
@@ -1,57 +1,59 @@ | ||
1 | 1 | |
2 | -#ifndef GIKOMONA_CORE_MESSAGE_HPP | |
3 | -#define GIKOMONA_CORE_MESSAGE_HPP | |
2 | +#ifndef GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP | |
3 | +#define GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP | |
4 | 4 | |
5 | 5 | #include <sstream> |
6 | 6 | |
7 | +#include <boost/type_erasure/any.hpp> | |
8 | +#include <boost/type_erasure/constructible.hpp> | |
9 | +#include <boost/type_erasure/member.hpp> | |
10 | +#include <boost/mpl/vector.hpp> | |
11 | + | |
7 | 12 | #include "string.hpp" |
8 | -#include "communication.hpp" | |
9 | 13 | |
10 | 14 | namespace monazilla { namespace GikoMona { namespace core { namespace communication { |
11 | 15 | |
12 | -class message_concept { | |
13 | -public: | |
14 | - message_concept(const mona_string& str); | |
15 | - ~message_concept() = default; | |
16 | +BOOST_TYPE_ERASURE_MEMBER(has_to_raw, to_raw, 0) | |
17 | +BOOST_TYPE_ERASURE_MEMBER(has_msg_type, get_type, 0) | |
18 | + | |
19 | +typedef mona_string raw_message; | |
16 | 20 | |
17 | - static mona_string to_string() const {} | |
21 | +enum class message_type { | |
22 | + FAILURE, | |
23 | + REQUEST, | |
24 | + SUCCEED | |
18 | 25 | }; |
19 | 26 | |
20 | -template <typename T> | |
21 | -class is_satisfied_with_message_concept { | |
22 | - const static bool value = false; | |
23 | -}; | |
27 | +typedef | |
28 | + boost::mpl::vector< | |
29 | + has_to_raw<raw_message()>, | |
30 | + has_msg_type<message_type()> | |
31 | + > message_requirements | |
32 | + ; | |
24 | 33 | |
25 | -template <typename T> | |
26 | -constexpr bool is_satisfied_with_message_concept_v() { | |
27 | - return is_satisfied_message_concept<T>::value; | |
28 | -} | |
29 | - | |
30 | -#define DEFINE_MESSAGE(type, name, message) \ | |
31 | - class name { \ | |
32 | - public: \ | |
33 | - explicit \ | |
34 | - name (const mona_string& str) : msg(str) {} \ | |
35 | - ~name() = default; \ | |
36 | - mona_string to_string() const { \ | |
37 | - std::ostringstream str_builder; \ | |
38 | - str_builder << #type << ":" \ | |
39 | - << message << "(" \ | |
40 | - << msg << ")."; \ | |
41 | - return convert_from(str_builder.str()); \ | |
42 | - } \ | |
43 | - private: \ | |
44 | - const mona_string msg; \ | |
45 | - }; | |
46 | - | |
47 | -template <typename T> | |
48 | -bool is_same_mail(mona_string src, T&& obj) { | |
49 | - static_assert(is_satisfied_with_message_concept_v<T>(), | |
50 | - "`T' does not satisfy the `message_concept'"); | |
51 | - | |
52 | - return (src == obj.to_string()); | |
53 | -} | |
34 | +typedef boost::type_erasure::any<message_requirements> message; | |
35 | + | |
36 | +#define DEFINE_MESSAGE(type_, name, message) \ | |
37 | + class name { \ | |
38 | + public: \ | |
39 | + explicit \ | |
40 | + name (const mona_string& str) : detail(str) {} \ | |
41 | + ~name() = default; \ | |
42 | + raw_message to_raw() const { \ | |
43 | + std::ostringstream str_builder; \ | |
44 | + str_builder << #type_ << ":" \ | |
45 | + << message << "(" \ | |
46 | + << detail << ")."; \ | |
47 | + return static_cast<raw_message>( \ | |
48 | + convert_from(str_builder.str()) \ | |
49 | + ); \ | |
50 | + } \ | |
51 | + message_type get_type() { return type; } \ | |
52 | + private: \ | |
53 | + const mona_string detail; \ | |
54 | + const message_type type = message_type::type_; \ | |
55 | +}; | |
54 | 56 | |
55 | 57 | } } } } |
56 | 58 | |
57 | -#endif // GIKOMONA_CORE_MESSAGE_HPP | |
59 | +#endif // GIKOMONA_CORE_COMMUNICATION_MESSAGE_HPP |