Savarese Software Research Corporation
PrependToContainer.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 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_PREPEND_TO_CONTAINER_H
00024 #define __SSRC_WSPR_UTILITY_PREPEND_TO_CONTAINER_H
00025 
00026 #include <ssrc/wispers-packages.h>
00027 
00028 __BEGIN_NS_SSRC_WSPR_UTILITY
00029 
00030 template<typename container_type>
00031 struct has_pushfront {
00032   // Common case is to use sequences, so we use a default value of true.
00033   // That way, we'll get a compile-time error for any container that
00034   // does not implement push_front alerting the user to define a specialization.
00035   static const bool value = true;
00036 };
00037 
00038 #define WSPR_HAS_NOT_PUSHFRONT(container) \
00039   __BEGIN_NS_SSRC_WSPR_UTILITY \
00040   template<> struct has_pushfront<container >{static const bool value = false;}; \
00041   __END_NS_SSRC_WSPR_UTILITY
00042 
00043 // TODO: Instead of PrependToContainer, we should really be using an insertion
00044 // iterator in calling functions instead of the container (e.g., in
00045 // database query for_each functions).
00046 
00047 template<typename container_type,
00048          bool has_pushfront = has_pushfront<container_type>::value>
00049 class PrependToContainer;
00050 
00051 template<typename container_type>
00052 class PrependToContainer<container_type, false> {
00053   container_type & _container;
00054 
00055 public:
00056   typedef typename container_type::value_type value_type;
00057 
00058   PrependToContainer(container_type & container) : _container(container) { }
00059 
00060   void operator()(const value_type & value) const {
00061     _container.insert(_container.begin(), value);
00062   }
00063 
00064   void operator()(value_type && value) const {
00065     _container.insert(_container.begin(), std::forward<value_type>(value));
00066   }
00067 };
00068 
00069 template<typename container_type>
00070 class PrependToContainer<container_type, true> {
00071   container_type & _container;
00072 
00073 public:
00074   typedef typename container_type::value_type value_type;
00075 
00076   PrependToContainer(container_type & container) : _container(container) { }
00077 
00078   void operator()(const value_type & value) const {
00079     _container.push_front(value);
00080   }
00081 
00082   void operator()(value_type && value) const {
00083     _container.push_front(std::forward<value_type>(value));
00084   }
00085 };
00086 
00087 __END_NS_SSRC_WSPR_UTILITY
00088 
00089 #endif

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