Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - AppendToContainer.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 4 4 100.0 %
Date: 2012-04-09 Functions: 10 10 100.0 %
Branches: 0 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 AppendToContainer template class and its
      20                 :            :  * specializations.
      21                 :            :  */
      22                 :            : 
      23                 :            : #ifndef __SSRC_WSPR_UTILITY_APPEND_TO_CONTAINER_H
      24                 :            : #define __SSRC_WSPR_UTILITY_APPEND_TO_CONTAINER_H
      25                 :            : 
      26                 :            : #include <ssrc/wispers-packages.h>
      27                 :            : 
      28                 :            : #include <utility>
      29                 :            : 
      30                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      31                 :            : 
      32                 :            : template<typename container_type>
      33                 :            : struct has_pushback {
      34                 :            :   // Common case is to use sequences, so we use a default value of true.
      35                 :            :   // That way, we'll get a compile-time error for any container that
      36                 :            :   // does not implement push_back alerting the user to define a specialization.
      37                 :            :   static const bool value = true;
      38                 :            : };
      39                 :            : 
      40                 :            : #define WSPR_HAS_NOT_PUSHBACK(container) \
      41                 :            :   __BEGIN_NS_SSRC_WSPR_UTILITY \
      42                 :            :   template<> struct has_pushback<container >{static const bool value = false;}; \
      43                 :            :   __END_NS_SSRC_WSPR_UTILITY
      44                 :            : 
      45                 :            : // TODO: Instead of AppendToContainer, we should really be using an insertion
      46                 :            : // iterator in calling functions instead of the container (e.g., in
      47                 :            : // database query for_each functions).  We can't use back_inserter
      48                 :            : // because it is implemented only with push_back and we want to use
      49                 :            : // push_back when available, falling back to insert(container.end(), value).
      50                 :            : 
      51                 :            : template<typename container_type,
      52                 :            :          bool has_pushback = has_pushback<container_type>::value>
      53                 :            : class AppendToContainer;
      54                 :            : 
      55                 :            : template<typename container_type>
      56                 :            : class AppendToContainer<container_type, false> {
      57                 :            :   container_type & _container;
      58                 :            : 
      59                 :            : public:
      60                 :            :   typedef typename container_type::value_type value_type;
      61                 :            : 
      62                 :            :   AppendToContainer(container_type & container) : _container(container) { }
      63                 :            : 
      64                 :            :   void operator()(const value_type & value) const {
      65                 :            :     _container.insert(_container.end(), value);
      66                 :            :   }
      67                 :            : 
      68                 :            :   void operator()(value_type && value) const {
      69                 :            :     _container.insert(_container.end(), std::forward<value_type>(value));
      70                 :            :   }
      71                 :            : };
      72                 :            : 
      73                 :            : template<typename container_type>
      74                 :            : class AppendToContainer<container_type, true> {
      75                 :            :   container_type & _container;
      76                 :            : 
      77                 :            : public:
      78                 :            :   typedef typename container_type::value_type value_type;
      79                 :            : 
      80                 :         35 :   AppendToContainer(container_type & container) : _container(container) { }
      81                 :            : 
      82                 :            :   void operator()(const value_type & value) const {
      83                 :            :     _container.push_back(value);
      84                 :            :   }
      85                 :            : 
      86                 :         69 :   void operator()(value_type && value) const {
      87                 :         69 :     _container.push_back(std::forward<value_type>(value));
      88                 :         69 :   }
      89                 :            : };
      90                 :            : 
      91                 :            : template<typename container_type>
      92                 :            : class InsertToContainer {
      93                 :            :   container_type & _container;
      94                 :            : 
      95                 :            : public:
      96                 :            :   typedef typename container_type::value_type value_type;
      97                 :            : 
      98                 :            :   InsertToContainer(container_type & container) : _container(container) { }
      99                 :            : 
     100                 :            :   void operator()(const value_type & value) const {
     101                 :            :     _container.insert(value);
     102                 :            :   }
     103                 :            : 
     104                 :            :   void operator()(value_type && value) const {
     105                 :            :     _container.insert(std::forward<value_type>(value));
     106                 :            :   }
     107                 :            : };
     108                 :            : 
     109                 :            : __END_NS_SSRC_WSPR_UTILITY
     110                 :            : 
     111                 :            : #endif