00001 /** 00002 \file Robots/LoBot/util/LoDebug.H 00003 \brief Debug functions. 00004 */ 00005 00006 // //////////////////////////////////////////////////////////////////// // 00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00008 // by the University of Southern California (USC) and the iLab at USC. // 00009 // See http://iLab.usc.edu for information about this project. // 00010 // //////////////////////////////////////////////////////////////////// // 00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00013 // in Visual Environments, and Applications'' by Christof Koch and // 00014 // Laurent Itti, California Institute of Technology, 2001 (patent // 00015 // pending; application number 09/912,225 filed July 23, 2001; see // 00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00017 // //////////////////////////////////////////////////////////////////// // 00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00019 // // 00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00021 // redistribute it and/or modify it under the terms of the GNU General // 00022 // Public License as published by the Free Software Foundation; either // 00023 // version 2 of the License, or (at your option) any later version. // 00024 // // 00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00028 // PURPOSE. See the GNU General Public License for more details. // 00029 // // 00030 // You should have received a copy of the GNU General Public License // 00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00033 // Boston, MA 02111-1307 USA. // 00034 // //////////////////////////////////////////////////////////////////// // 00035 // 00036 // Primary maintainer for this file: mviswana usc edu 00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/util/LoDebug.H $ 00038 // $Id: LoDebug.H 13037 2010-03-23 01:00:53Z mviswana $ 00039 // 00040 00041 #ifndef LOBOT_DEBUG_UTILITIES_DOT_H 00042 #define LOBOT_DEBUG_UTILITIES_DOT_H 00043 00044 //------------------------------ HEADERS -------------------------------- 00045 00046 // INVT utilities 00047 #include "Util/log.H" 00048 00049 // Standard C++ headers 00050 #include <sstream> 00051 #include <ostream> 00052 #include <string> 00053 #include <algorithm> 00054 #include <map> 00055 #include <vector> 00056 #include <iterator> 00057 #include <utility> 00058 00059 //----------------------------- NAMESPACE ------------------------------- 00060 00061 namespace lobot { 00062 00063 //--------------------------- DEBUG SUPPORT ----------------------------- 00064 00065 /// Debugging support: dump an STL container using LERROR 00066 /// 00067 /// DEVNOTE: Mostly suitable for small containers, e.g., vector of 10 00068 /// numbers or something like that. 00069 template<typename container> 00070 void dump(const container& C, const std::string& caller, 00071 const std::string& container_name = "container") 00072 { 00073 std::ostringstream str ; 00074 std::copy(C.begin(), C.end(), 00075 std::ostream_iterator<typename container::value_type>(str, " ")) ; 00076 LERROR("from %s ==> %s[0x%lx]: size = %d, contents = %s", 00077 caller.c_str(), container_name.c_str(), 00078 reinterpret_cast<long int>(&C), static_cast<int>(C.size()), 00079 str.str().c_str()) ; 00080 } 00081 00082 /// Dump an array of type T given pointers to its first and last elements 00083 template<typename T> 00084 void dump(const T* begin, const T* end, const std::string& caller, 00085 const std::string& container_name = "array") 00086 { 00087 std::vector<T> vec(begin, end) ; 00088 dump(vec, caller, container_name) ; 00089 } 00090 00091 /// Function object to dump an std::pair to the given output stream 00092 template<typename T1, typename T2> 00093 class dump_pair { 00094 std::ostream& os ; 00095 public: 00096 dump_pair(std::ostream& s) : os(s) {} 00097 void operator()(const std::pair<T1, T2>& p) const { 00098 os << '[' << p.first << ' ' << p.second << "] " ; 00099 } 00100 } ; 00101 00102 /// The earlier version of the dump() function for dumping containers 00103 /// will only work with containers of built-in/standard types such as 00104 /// vector<int>, list<float>, etc. because it uses std::ostream_iterator, 00105 /// which, annoyingly enough, doesn't work with user-defined types. 00106 /// Therefore, the earlier dump() function won't dump std::map containers 00107 /// (because their value type is an std::pair). 00108 /// 00109 /// We work around this problem by providing this overloaded version of 00110 /// dump() especially for std::maps. 00111 template<typename key_type, typename value_type> 00112 void dump(const std::map<key_type, value_type>& C, 00113 const std::string& caller, 00114 const std::string& container_name = "container") 00115 { 00116 std::ostringstream str ; 00117 std::for_each(C.begin(), C.end(), 00118 dump_pair<key_type, value_type>(str)) ; 00119 LERROR("from %s ==> %s[0x%lx]: size = %d, contents = %s", 00120 caller.c_str(), container_name.c_str(), 00121 reinterpret_cast<long int>(&C), static_cast<int>(C.size()), 00122 str.str().c_str()) ; 00123 } 00124 00125 //----------------------------------------------------------------------- 00126 00127 } // end of namespace encapsulating this file's definitions 00128 00129 #endif 00130 00131 /* So things look consistent in everyone's emacs... */ 00132 /* Local Variables: */ 00133 /* indent-tabs-mode: nil */ 00134 /* End: */