Savarese Software Research Corporation
DictionaryProtocol.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 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_WSPR_INDEX_DICTIONARY_PROTOCOL_H
00023 #define __SSRC_WSPR_INDEX_DICTIONARY_PROTOCOL_H
00024 
00025 #include <boost/serialization/vector.hpp>
00026 
00027 #include <ssrc/wispers/protocol.h>
00028 
00029 __BEGIN_NS_SSRC_WSPR_INDEX
00030 
00031 using NS_SSRC_WISP_PROTOCOL::wisp_message_id;
00032 
00033 template<template <wisp_message_id id> class MessageBase,
00034          typename MapType, typename IndexScheme>
00035 struct DictionaryProtocol :
00036   public protocol::ServiceProtocol<MessageBase<0>::protocol>
00037 {
00038   typedef protocol::ServiceProtocol<MessageBase<0>::protocol> super;
00039 
00040   WISP_IMPORT_T(super, caller_type);
00041 
00042   // Note that Insert will replace an existing entry if present.
00043   enum {
00044     Insert, Erase, QueryResult, QueryAll, QueryByDefault, SingleQueryResult,
00045     SingleQueryByDefault
00046   };
00047 
00048   // Custom message ids must be greater than this value.
00049   enum {
00050     MaxDictionaryMessageID = SingleQueryByDefault
00051   };
00052 
00053   typedef MapType map_type;
00054   typedef IndexScheme index_scheme;
00055 
00056   static const index_scheme ByDefault = static_cast<index_scheme>(0);
00057 
00058   typedef
00059   typename map_type::template nth_index<ByDefault>::type index_by_default;
00060 
00061   typedef MessageBase<QueryAll> MessageQueryAll;
00062 
00063   enum { protocol = MessageQueryAll::protocol };
00064 
00065   struct MessageInsert : public MessageBase<Insert> {
00066     std::vector<typename map_type::value_type> values;
00067 
00068     template<class Archive>
00069     void serialize(Archive & ar, const unsigned int) {
00070       ar & values;
00071     }
00072   };
00073 
00074   struct MessageQueryResult : public MessageBase<QueryResult> {
00075     map_type result;
00076 
00077     MessageQueryResult() { }
00078 
00079     explicit MessageQueryResult(const map_type & result) :
00080       result(result)
00081     { }
00082 
00083     template<class Archive>
00084     void serialize(Archive & ar, const unsigned int) {
00085       ar & result;
00086     }
00087   };
00088 
00089   template<wisp_message_id message_id, index_scheme scheme>
00090   struct MessageQuery : public MessageBase<message_id> {
00091     typedef typename map_type::template nth_index<scheme>::type index;
00092     std::vector<typename index::key_type> keys;
00093 
00094     template<class Archive>
00095     void serialize(Archive & ar, const unsigned int) {
00096       ar & keys;
00097     }
00098   };
00099 
00100   typedef MessageQuery<QueryByDefault, ByDefault> MessageQueryByDefault;
00101   typedef MessageQuery<Erase, ByDefault> MessageErase;
00102 
00103   struct MessageSingleQueryResult : public MessageBase<QueryResult> {
00104     typedef typename map_type::value_type value_type;
00105     bool found;
00106     value_type result;
00107 
00108     MessageSingleQueryResult() :
00109       found(false), result()
00110     { }
00111 
00112     explicit MessageSingleQueryResult(const value_type & result) :
00113       found(true), result(result)
00114     { }
00115 
00116     template<class Archive>
00117     void serialize(Archive & ar, const unsigned int) {
00118       ar & found & result;
00119     }
00120   };
00121 
00122   template<wisp_message_id message_id, index_scheme scheme>
00123   struct MessageSingleQuery : public MessageBase<message_id> {
00124     typedef typename map_type::template nth_index<scheme>::type index;
00125     typename index::key_type key;
00126 
00127     MessageSingleQuery() { }
00128 
00129     explicit MessageSingleQuery(const typename index::key_type & key) :
00130       key(key)
00131     { }
00132 
00133     template<class Archive>
00134     void serialize(Archive & ar, const unsigned int) {
00135       ar & key;
00136     }
00137   };
00138 
00139   typedef
00140   MessageSingleQuery<SingleQueryByDefault, ByDefault>
00141   MessageSingleQueryByDefault;
00142 
00143   WISP_ONE_WAY_CALL(caller_type, Insert);
00144   WISP_ONE_WAY_CALL(caller_type, Erase);
00145   WISP_ONE_WAY_CALL(caller_type, QueryResult);
00146   WISP_ONE_WAY_CALL(caller_type, SingleQueryResult);
00147   WISP_TWO_WAY_CALL(caller_type, QueryAll, QueryResult);
00148   WISP_TWO_WAY_CALL(caller_type, QueryByDefault, QueryResult);
00149   WISP_TWO_WAY_CALL(caller_type, SingleQueryByDefault, SingleQueryResult);
00150 };
00151 
00152 __END_NS_SSRC_WSPR_INDEX
00153 
00154 #endif

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