Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - NumberVisitor.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 21 36 58.3 %
Date: 2012-04-09 Functions: 13 41 31.7 %
Branches: 7 14 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 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 NumberVisitor class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_NUMBER_VISITOR_H
      23                 :            : #define __SSRC_WSPR_UTILITY_NUMBER_VISITOR_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/utility/Properties.h>
      26                 :            : 
      27                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      28                 :            : 
      29                 :            : /**
      30                 :            :  * NumberVisitor extracts numerical values from a Properties
      31                 :            :  * instance, cast to the desired numerical type.  This class
      32                 :            :  * is necessary because you don't always know the exact type a
      33                 :            :  * numerical value has (e.g., when a Properties instance is
      34                 :            :  * loaded from a Lua table).
      35                 :            :  */
      36                 :            : 
      37                 :            : template<typename T>
      38                 :          7 : class NumberVisitor : public boost::static_visitor<void> {
      39                 :            :   mutable T _value;
      40                 :            : 
      41                 :            : public:
      42                 :            : 
      43                 :          7 :   NumberVisitor(T value) : _value(value) { }
      44                 :            : 
      45                 :          5 :   const T get() const {
      46                 :          5 :     return _value;
      47                 :            :   }
      48                 :            : 
      49                 :          0 :   void operator()(bool v) const {
      50                 :          0 :     _value = v;
      51                 :          0 :   }
      52                 :            : 
      53                 :          1 :   void operator()(std::int32_t v) const {
      54                 :          1 :     _value = v;
      55                 :          1 :   }
      56                 :            : 
      57                 :          0 :   void operator()(std::uint32_t v) const {
      58                 :          0 :     _value = v;
      59                 :          0 :   }
      60                 :            : 
      61                 :          0 :   void operator()(std::int64_t v) const {
      62                 :          0 :     _value = v;
      63                 :          0 :   }
      64                 :            : 
      65                 :          0 :   void operator()(std::uint64_t v) const {
      66                 :          0 :     _value = v;
      67                 :          0 :   }
      68                 :            : 
      69                 :          6 :   void operator()(double v) const {
      70                 :          6 :     _value = v;
      71                 :          6 :   }
      72                 :            : 
      73                 :            :   template<typename V>
      74                 :          0 :   void operator()(V & v) const { }
      75                 :            : };
      76                 :            : 
      77                 :            : template<typename T, typename V>
      78                 :          5 : const T get_number(const V & variant,
      79                 :            :                    const T & default_value = T())
      80                 :            : {
      81                 :         10 :   NumberVisitor<T> nv(default_value);
      82   [ +  -  +  - ]:          5 :   boost::apply_visitor(nv, variant);
      83                 :         10 :   return nv.get();
      84                 :            : }
      85                 :            : 
      86                 :            : template<typename T>
      87                 :          3 : const T get_number(const Properties & properties,
      88                 :            :                    const T & default_value = T())
      89                 :            : {
      90         [ -  + ]:          3 :   if(properties.is_null())
      91                 :          0 :     return default_value;
      92                 :          3 :   return get_number(*properties.value(), default_value);
      93                 :            : }
      94                 :            : 
      95                 :            : template<typename T, typename... P>
      96                 :          3 : const T get_number(const Properties & properties,
      97                 :            :                    const T & default_value,
      98                 :            :                    const P & ...p)
      99                 :            : {
     100   [ +  -  +  -  :          3 :   const Properties *node = properties.find(p...);
                   +  - ]
     101                 :            : 
     102         [ +  - ]:          3 :   if(node != 0)
     103                 :          3 :     return get_number(*node, default_value);
     104                 :            : 
     105                 :          0 :   return default_value;
     106                 :            : }
     107                 :            : 
     108                 :            : __END_NS_SSRC_WSPR_UTILITY
     109                 :            : 
     110                 :            : #endif