Savarese Software Research Corporation

ServiceProtocol.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WISP_PROTOCOL_SERVICE_PROTOCOL_H
00023 #define __SSRC_WISP_PROTOCOL_SERVICE_PROTOCOL_H
00024 
00025 #include <ssrc/wisp/serialization.h>
00026 #include <ssrc/wisp/utility/wisp_struct.h>
00027 
00028 #include <ssrc/spread/Message.h>
00029 
00030 #include <string>
00031 #include <cstdint>
00032 
00033 __BEGIN_NS_SSRC_WISP_PROTOCOL
00034 
00035 /* We use Spread message type as our protocol indicator and
00036  * encode the message type inside the message itself.  This way, we
00037  * don't have to worry about running out of id numbers.
00038  */
00039 typedef NS_SSRC_SPREAD::Message::message_type wisp_message_protocol;
00040 typedef std::uint16_t wisp_message_id;
00041 
00042 template<typename PT> class Caller;
00043 
00044 template<wisp_message_protocol protocol_, typename PT = BinaryPackingTraits>
00045 struct ServiceProtocol {
00046   enum { protocol = protocol_ };
00047 
00048   typedef PT packing_traits;
00049   typedef Caller<packing_traits> caller_type;
00050 };
00051 
00052 #define WISP_DEFINE_PROTOCOL(protocol_base) \
00053   template<NS_SSRC_WISP_PROTOCOL::wisp_message_id _id> \
00054   struct Message ## protocol_base : public NS_SSRC_WISP_UTILITY::wisp_struct { \
00055     enum { protocol = protocol_base }; \
00056     enum { id = _id }; \
00057     template<class Archive> \
00058     void serialize(Archive & ar, const unsigned int) { } \
00059   }
00060 
00061 // These are convenience macros for pulling protocol structures into
00062 // a class scope, where 'using' is disallowed.
00063 #define WISP_IMPORT(a,b) typedef a::b b
00064 #define WISP_IMPORT_T(a,b) typedef typename a::b b
00065 
00066 // Convenience macros for defining message types.
00067 
00068 #define __WISP_PROTOCOL_MESSAGE(in_template, call, base, members)      \
00069   struct BOOST_PP_CAT(Message, call) : public base<call> {             \
00070     BOOST_PP_SEQ_FOR_EACH(__WISP_STRUCT_MEMBER, _, members)            \
00071     BOOST_PP_CAT(Message, call)() :                                    \
00072     BOOST_PP_SEQ_FOR_EACH_I(__WISP_STRUCT_DEFAULT_INIT_MEMBER, _, members) { } \
00073       explicit BOOST_PP_CAT(Message, call)(BOOST_PP_SEQ_FOR_EACH_I(__WISP_STRUCT_PARAM, in_template, members)) : \
00074     BOOST_PP_SEQ_FOR_EACH_I(__WISP_STRUCT_INIT_MEMBER, _, members) { } \
00075     __WISP_STRUCT_SERIALIZE_MEMBERS(members)                           \
00076     __WISP_STRUCT_VISIT(members)                                       \
00077   }
00078 
00079 #define __WISP_PROTOCOL_MESSAGE_WITH_INIT(in_template, call, base, members, initializers) \
00080   struct BOOST_PP_CAT(Message, call) : public base<call> {             \
00081     BOOST_PP_SEQ_FOR_EACH(__WISP_STRUCT_MEMBER, _, members)            \
00082       explicit BOOST_PP_CAT(Message, call)(BOOST_PP_SEQ_FOR_EACH_I(__WISP_STRUCT_PARAM_INIT, BOOST_PP_ARRAY_PUSH_FRONT(BOOST_PP_ARRAY_PUSH_FRONT(initializers, BOOST_PP_SUB(BOOST_PP_SEQ_SIZE(members), BOOST_PP_ARRAY_SIZE(initializers))), in_template), members)) : \
00083     BOOST_PP_SEQ_FOR_EACH_I(__WISP_STRUCT_INIT_MEMBER, _, members) { } \
00084     __WISP_STRUCT_SERIALIZE_MEMBERS(members)                           \
00085     __WISP_STRUCT_VISIT(members)                                       \
00086   }
00087 
00088 #define WISP_PROTOCOL_MESSAGE(call, base, members) \
00089   __WISP_PROTOCOL_MESSAGE(0, call, base, members)
00090 
00091 #define WISP_PROTOCOL_MESSAGE_T(call, base, members) \
00092   __WISP_PROTOCOL_MESSAGE(1, call, base, members)
00093 
00094 #define WISP_PROTOCOL_MESSAGE_WITH_INIT(call, base, members, initializers) \
00095   __WISP_PROTOCOL_MESSAGE_WITH_INIT(0, call, base, members, BOOST_PP_SEQ_TO_ARRAY(initializers))
00096 
00097 #define WISP_PROTOCOL_MESSAGE_WITH_INIT_T(call, base, members, initializers) \
00098   __WISP_PROTOCOL_MESSAGE_WITH_INIT(1, call, base, members, BOOST_PP_SEQ_TO_ARRAY(initializers))
00099 
00100 __END_NS_SSRC_WISP_PROTOCOL
00101 
00102 #endif

Savarese Software Research Corporation
Copyright © 2006-2009 Savarese Software Research Corporation. All rights reserved.