Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/lua - map.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 16 16 100.0 %
Date: 2012-04-09 Functions: 1 1 100.0 %
Branches: 13 14 92.9 %

           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 This header defines conversion functions from C++ associative
      19                 :            :  * containers to Lua tables.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_LUA_MAP_H
      23                 :            : #define __SSRC_WSPR_LUA_MAP_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/lua/lua.h>
      26                 :            : 
      27                 :            : __BEGIN_NS_SSRC_WSPR_LUA
      28                 :            : 
      29                 :            : template<typename container_type>
      30                 :          1 : void map_to_table(lua_State *state, const container_type & map) {
      31                 :            :   typedef typename container_type::const_iterator const_iterator;
      32                 :            : 
      33                 :          1 :   lua_newtable(state);
      34                 :            : 
      35         [ +  + ]:          5 :   for(const_iterator && it = map.begin(), && end = map.end(); it != end;) {
      36                 :          3 :     const_iterator value = it++;
      37                 :            : 
      38   [ +  +  +  +  :          3 :     if(it != end && value->first == it->first) {
                   +  + ]
      39                 :          1 :       int pos = 0;
      40                 :            : 
      41                 :          1 :       push_value(state, value->first);
      42                 :          1 :       lua_newtable(state);
      43                 :            : 
      44                 :            :       // We assume equivalent keys are contiguous.
      45   [ +  -  +  +  :          6 :       do {
                   +  + ]
      46                 :          3 :         push_value(state, value->second);
      47                 :          3 :         lua_rawseti(state, -2, ++pos);
      48                 :          6 :         ++value;
      49                 :            :       } while(value != end && value->first == it->first);
      50                 :            : 
      51                 :          1 :       lua_settable(state, -3);
      52                 :          1 :       it = value;
      53                 :            :     } else {
      54                 :          2 :       set_field(state, -1, value->second, value->first);
      55                 :            :     }
      56                 :            :   }
      57                 :          1 : }
      58                 :            : 
      59                 :            : __END_NS_SSRC_WSPR_LUA
      60                 :            : 
      61                 :            : #endif