safecopy.H

Go to the documentation of this file.
00001 /*!@file Util/safecopy.H Provides a "safe" replacement for memcpy:
00002   when INVT_MEM_DEBUG is not defined, we just delegate to memcpy(),
00003   however when INVT_MEM_DEBUG is defined we use a generic iterator
00004   version, which allows the use of a range-checked iterator (such as
00005   CheckedIterator) for tracking down memory-access related bugs.
00006  */
00007 
00008 // //////////////////////////////////////////////////////////////////// //
00009 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00010 // University of Southern California (USC) and the iLab at USC.         //
00011 // See http://iLab.usc.edu for information about this project.          //
00012 // //////////////////////////////////////////////////////////////////// //
00013 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00014 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00015 // in Visual Environments, and Applications'' by Christof Koch and      //
00016 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00017 // pending; application number 09/912,225 filed July 23, 2001; see      //
00018 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00019 // //////////////////////////////////////////////////////////////////// //
00020 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00023 // redistribute it and/or modify it under the terms of the GNU General  //
00024 // Public License as published by the Free Software Foundation; either  //
00025 // version 2 of the License, or (at your option) any later version.     //
00026 //                                                                      //
00027 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00028 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00029 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00030 // PURPOSE.  See the GNU General Public License for more details.       //
00031 //                                                                      //
00032 // You should have received a copy of the GNU General Public License    //
00033 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00034 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00035 // Boston, MA 02111-1307 USA.                                           //
00036 // //////////////////////////////////////////////////////////////////// //
00037 //
00038 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu>
00039 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/safecopy.H $
00040 // $Id: safecopy.H 5736 2005-10-18 17:30:29Z rjpeters $
00041 //
00042 
00043 #ifndef SAFECOPY_H_DEFINED
00044 #define SAFECOPY_H_DEFINED
00045 
00046 #include <cstring>
00047 
00048 #include "Util/TypeTraits.H"
00049 
00050 //! Element-by-element copy a series forward iterators.
00051 template <class Iter, class ConstIter>
00052 inline void genericcopy(Iter dest, ConstIter src, unsigned int count)
00053 {
00054   for (unsigned int i = 0; i < count; ++i)
00055     *dest++ = *src++;
00056 }
00057 
00058 //! A generic version of safecopy() for iterators.
00059 template <class Iter, class ConstIter>
00060 inline void safecopy(Iter dest, ConstIter src, unsigned int count)
00061 {
00062   genericcopy(dest, src, count);
00063 }
00064 
00065 //! Helper template struct for pointer version of safecopy().
00066 template <typename T, bool isTrivial = TypeTraits<T>::isTrivial>
00067 struct SafeCopyHelper
00068 {
00069   static void copy(T* dest, const T* src, unsigned int n)
00070   { genericcopy(dest, src, n); }
00071 };
00072 
00073 //! Specialized implementation for trivial types.
00074 template <typename T>
00075 struct SafeCopyHelper<T, true>
00076 {
00077   static void copy(T* dest, const T* src, unsigned int n)
00078   { memcpy(dest, src, n * sizeof(T)); }
00079 };
00080 
00081 //! A specialized version of safecopy() for pointers.
00082 /*! This version uses memcpy() internally for maximum efficiency if T is a
00083     "trivial" type, otherwise it uses genericcopy(). <b>Note however</b>
00084     that safecopy's third parameter is number-of-elements, <b>NOT</b>
00085     number of bytes (i.e., unlike memcpy). */
00086 template <class T>
00087 inline void safecopy(T* dest, const T* src, unsigned int count)
00088 {
00089   SafeCopyHelper<T>::copy(dest, src, count);
00090 }
00091 
00092 #endif // !SAFECOPY_H_DEFINED
00093 
00094 // ######################################################################
00095 /* So things look consistent in everyone's emacs... */
00096 /* Local Variables: */
00097 /* indent-tabs-mode: nil */
00098 /* End: */
Generated on Sun May 8 08:42:28 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3