Savarese Software Research Corporation
AppendToContainer.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 
00023 #ifndef __SSRC_WSPR_UTILITY_APPEND_TO_CONTAINER_H
00024 #define __SSRC_WSPR_UTILITY_APPEND_TO_CONTAINER_H
00025 
00026 #include <ssrc/wispers-packages.h>
00027 
00028 #include <utility>
00029 
00030 __BEGIN_NS_SSRC_WSPR_UTILITY
00031 
00032 template<typename container_type>
00033 struct has_pushback {
00034   // Common case is to use sequences, so we use a default value of true.
00035   // That way, we'll get a compile-time error for any container that
00036   // does not implement push_back alerting the user to define a specialization.
00037   static const bool value = true;
00038 };
00039 
00040 #define WSPR_HAS_NOT_PUSHBACK(container) \
00041   __BEGIN_NS_SSRC_WSPR_UTILITY \
00042   template<> struct has_pushback<container >{static const bool value = false;}; \
00043   __END_NS_SSRC_WSPR_UTILITY
00044 
00045 // TODO: Instead of AppendToContainer, we should really be using an insertion
00046 // iterator in calling functions instead of the container (e.g., in
00047 // database query for_each functions).  We can't use back_inserter
00048 // because it is implemented only with push_back and we want to use
00049 // push_back when available, falling back to insert(container.end(), value).
00050 
00051 template<typename container_type,
00052          bool has_pushback = has_pushback<container_type>::value>
00053 class AppendToContainer;
00054 
00055 template<typename container_type>
00056 class AppendToContainer<container_type, false> {
00057   container_type & _container;
00058 
00059 public:
00060   typedef typename container_type::value_type value_type;
00061 
00062   AppendToContainer(container_type & container) : _container(container) { }
00063 
00064   void operator()(const value_type & value) const {
00065     _container.insert(_container.end(), value);
00066   }
00067 
00068   void operator()(value_type && value) const {
00069     _container.insert(_container.end(), std::forward<value_type>(value));
00070   }
00071 };
00072 
00073 template<typename container_type>
00074 class AppendToContainer<container_type, true> {
00075   container_type & _container;
00076 
00077 public:
00078   typedef typename container_type::value_type value_type;
00079 
00080   AppendToContainer(container_type & container) : _container(container) { }
00081 
00082   void operator()(const value_type & value) const {
00083     _container.push_back(value);
00084   }
00085 
00086   void operator()(value_type && value) const {
00087     _container.push_back(std::forward<value_type>(value));
00088   }
00089 };
00090 
00091 template<typename container_type>
00092 class InsertToContainer {
00093   container_type & _container;
00094 
00095 public:
00096   typedef typename container_type::value_type value_type;
00097 
00098   InsertToContainer(container_type & container) : _container(container) { }
00099 
00100   void operator()(const value_type & value) const {
00101     _container.insert(value);
00102   }
00103 
00104   void operator()(value_type && value) const {
00105     _container.insert(std::forward<value_type>(value));
00106   }
00107 };
00108 
00109 __END_NS_SSRC_WSPR_UTILITY
00110 
00111 #endif

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