Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/index - DictionaryProtocol.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 34 34 100.0 %
Date: 2012-04-09 Functions: 45 51 88.2 %
Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the DictionaryProtocol class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_INDEX_DICTIONARY_PROTOCOL_H
      23                 :            : #define __SSRC_WSPR_INDEX_DICTIONARY_PROTOCOL_H
      24                 :            : 
      25                 :            : #include <boost/serialization/vector.hpp>
      26                 :            : 
      27                 :            : #include <ssrc/wispers/protocol.h>
      28                 :            : 
      29                 :            : __BEGIN_NS_SSRC_WSPR_INDEX
      30                 :            : 
      31                 :            : using NS_SSRC_WISP_PROTOCOL::wisp_message_id;
      32                 :            : 
      33                 :            : template<template <wisp_message_id id> class MessageBase,
      34                 :            :          typename MapType, typename IndexScheme>
      35                 :            : struct DictionaryProtocol :
      36                 :            :   public protocol::ServiceProtocol<MessageBase<0>::protocol>
      37                 :            : {
      38                 :            :   typedef protocol::ServiceProtocol<MessageBase<0>::protocol> super;
      39                 :            : 
      40                 :            :   WISP_IMPORT_T(super, caller_type);
      41                 :            : 
      42                 :            :   // Note that Insert will replace an existing entry if present.
      43                 :            :   enum {
      44                 :            :     Insert, Erase, QueryResult, QueryAll, QueryByDefault, SingleQueryResult,
      45                 :            :     SingleQueryByDefault
      46                 :            :   };
      47                 :            : 
      48                 :            :   // Custom message ids must be greater than this value.
      49                 :            :   enum {
      50                 :            :     MaxDictionaryMessageID = SingleQueryByDefault
      51                 :            :   };
      52                 :            : 
      53                 :            :   typedef MapType map_type;
      54                 :            :   typedef IndexScheme index_scheme;
      55                 :            : 
      56                 :            :   static const index_scheme ByDefault = static_cast<index_scheme>(0);
      57                 :            : 
      58                 :            :   typedef
      59                 :            :   typename map_type::template nth_index<ByDefault>::type index_by_default;
      60                 :            : 
      61                 :            :   typedef MessageBase<QueryAll> MessageQueryAll;
      62                 :            : 
      63                 :            :   enum { protocol = MessageQueryAll::protocol };
      64                 :            : 
      65                 :         10 :   struct MessageInsert : public MessageBase<Insert> {
      66                 :            :     std::vector<typename map_type::value_type> values;
      67                 :            : 
      68                 :            :     template<class Archive>
      69                 :          6 :     void serialize(Archive & ar, const unsigned int) {
      70                 :          6 :       ar & values;
      71                 :          6 :     }
      72                 :            :   };
      73                 :            : 
      74                 :         11 :   struct MessageQueryResult : public MessageBase<QueryResult> {
      75                 :            :     map_type result;
      76                 :            : 
      77   [ +  -  +  - ]:          7 :     MessageQueryResult() { }
      78                 :            : 
      79                 :          4 :     explicit MessageQueryResult(const map_type & result) :
      80                 :          4 :       result(result)
      81                 :          4 :     { }
      82                 :            : 
      83                 :            :     template<class Archive>
      84                 :         10 :     void serialize(Archive & ar, const unsigned int) {
      85                 :         10 :       ar & result;
      86                 :         10 :     }
      87                 :            :   };
      88                 :            : 
      89                 :            :   template<wisp_message_id message_id, index_scheme scheme>
      90                 :         12 :   struct MessageQuery : public MessageBase<message_id> {
      91                 :            :     typedef typename map_type::template nth_index<scheme>::type index;
      92                 :            :     std::vector<typename index::key_type> keys;
      93                 :            : 
      94                 :            :     template<class Archive>
      95                 :          6 :     void serialize(Archive & ar, const unsigned int) {
      96                 :          6 :       ar & keys;
      97                 :          6 :     }
      98                 :            :   };
      99                 :            : 
     100                 :            :   typedef MessageQuery<QueryByDefault, ByDefault> MessageQueryByDefault;
     101                 :            :   typedef MessageQuery<Erase, ByDefault> MessageErase;
     102                 :            : 
     103                 :         12 :   struct MessageSingleQueryResult : public MessageBase<QueryResult> {
     104                 :            :     typedef typename map_type::value_type value_type;
     105                 :            :     bool found;
     106                 :            :     value_type result;
     107                 :            : 
     108                 :          7 :     MessageSingleQueryResult() :
     109                 :          7 :       found(false), result()
     110                 :          7 :     { }
     111                 :            : 
     112                 :          5 :     explicit MessageSingleQueryResult(const value_type & result) :
     113                 :          5 :       found(true), result(result)
     114                 :          5 :     { }
     115                 :            : 
     116                 :            :     template<class Archive>
     117                 :         16 :     void serialize(Archive & ar, const unsigned int) {
     118                 :         16 :       ar & found & result;
     119                 :         16 :     }
     120                 :            :   };
     121                 :            : 
     122                 :            :   template<wisp_message_id message_id, index_scheme scheme>
     123                 :          2 :   struct MessageSingleQuery : public MessageBase<message_id> {
     124                 :            :     typedef typename map_type::template nth_index<scheme>::type index;
     125                 :            :     typename index::key_type key;
     126                 :            : 
     127                 :          1 :     MessageSingleQuery() { }
     128                 :            : 
     129                 :          1 :     explicit MessageSingleQuery(const typename index::key_type & key) :
     130                 :          1 :       key(key)
     131                 :          1 :     { }
     132                 :            : 
     133                 :            :     template<class Archive>
     134                 :          2 :     void serialize(Archive & ar, const unsigned int) {
     135                 :          2 :       ar & key;
     136                 :          2 :     }
     137                 :            :   };
     138                 :            : 
     139                 :            :   typedef
     140                 :            :   MessageSingleQuery<SingleQueryByDefault, ByDefault>
     141                 :            :   MessageSingleQueryByDefault;
     142                 :            : 
     143                 :            :   WISP_ONE_WAY_CALL(caller_type, Insert);
     144                 :            :   WISP_ONE_WAY_CALL(caller_type, Erase);
     145                 :            :   WISP_ONE_WAY_CALL(caller_type, QueryResult);
     146                 :            :   WISP_ONE_WAY_CALL(caller_type, SingleQueryResult);
     147                 :            :   WISP_TWO_WAY_CALL(caller_type, QueryAll, QueryResult);
     148                 :            :   WISP_TWO_WAY_CALL(caller_type, QueryByDefault, QueryResult);
     149                 :            :   WISP_TWO_WAY_CALL(caller_type, SingleQueryByDefault, SingleQueryResult);
     150                 :            : };
     151                 :            : 
     152                 :            : __END_NS_SSRC_WSPR_INDEX
     153                 :            : 
     154                 :            : #endif