/var/homes/greensocs/public_html/autodocs/greencontrol/core/helpfunctions.h

Go to the documentation of this file.
00001 //   GreenControl framework
00002 //
00003 // LICENSETEXT
00004 //
00005 //   Copyright (C) 2007 : GreenSocs Ltd
00006 //       http://www.greensocs.com/ , email: info@greensocs.com
00007 //
00008 //   Developed by :
00009 //   
00010 //   Christian Schroeder <schroeder@eis.cs.tu-bs.de>,
00011 //   Wolfgang Klingauf <klingauf@eis.cs.tu-bs.de>
00012 //     Technical University of Braunschweig, Dept. E.I.S.
00013 //     http://www.eis.cs.tu-bs.de
00014 //
00015 //
00016 //   This program is free software.
00017 // 
00018 //   If you have no applicable agreement with GreenSocs Ltd, this software
00019 //   is licensed to you, and you can redistribute it and/or modify
00020 //   it under the terms of the GNU General Public License as published by
00021 //   the Free Software Foundation; either version 2 of the License, or
00022 //   (at your option) any later version.
00023 // 
00024 //   If you have a applicable agreement with GreenSocs Ltd, the terms of that
00025 //   agreement prevail.
00026 // 
00027 //   This program is distributed in the hope that it will be useful,
00028 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00029 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00030 //   GNU General Public License for more details.
00031 // 
00032 //   You should have received a copy of the GNU General Public License
00033 //   along with this program; if not, write to the Free Software
00034 //   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00035 //   02110-1301  USA 
00036 // 
00037 // ENDLICENSETEXT
00038 
00039 #ifndef __helpfunctions_h__
00040 #define __helpfunctions_h__
00041 
00042 #include <vector>
00043 #include <string>
00044 #include <sstream>
00045 #include <ctype.h>
00046 
00047 
00048 namespace gs {
00049 namespace ctr {
00050 
00051 using std::vector;
00052 
00053   
00055 template<class T>
00056 static void show_vector(std::vector<T> vec) {
00057   std::cout << "vector: size="<< vec.size() << std::endl;
00058   typename std::vector<T>::iterator iter;
00059   for( iter = vec.begin(); iter != vec.end(); iter++ ) {
00060     std::cout << *iter << std::endl;
00061   }
00062 }
00063 
00065 template<class T, class P>
00066 static void show_vector_gc_trace(std::vector<T> vec1, std::vector<P> vec2) {
00067   typename std::vector<T>::iterator iter1;
00068   typename std::vector<P>::iterator iter2;
00069   iter2 = vec2.begin();
00070   for( iter1 = vec1.begin() ; iter1 != vec1.end(); iter1++) {
00071     std::cout << *iter1 << " [" << *iter2 << "] ";
00072     iter2++;
00073   }
00074   std::cout << std::endl;
00075 }
00076 
00078 template<class T>
00079 static void show_vector(const std::vector<T> vec, const unsigned int size) {
00080   std::cout << "  vector: size="<< vec.size()<<", show "<< size<<" items" << std::endl << "  ";
00081   assert(vec.size() >= size);
00082   typename std::vector<T>::iterator iter;
00083   for (unsigned int i = 0; i<size; i++) {
00084     std::cout << vec[i] << ", ";
00085   }
00086   std::cout << std::endl;
00087 }
00088 
00090 template<class T>
00091 static void show_vector_nosep(const std::vector<T> vec, const unsigned int size) {
00092   std::cout << "  vector: size="<< vec.size()<<", show "<< size<<" items" << std::endl << "  ";
00093   assert(vec.size() >= size);
00094   show_pure_vector(vec, size);
00095   std::cout << std::endl;
00096 }
00097 
00099 template<class T>
00100 static void show_pure_vector(const std::vector<T> vec, const unsigned int size) {
00101   assert(vec.size() >= size);
00102   typename vector<T>::iterator iter;
00103   for (unsigned int i = 0; i<size; i++) {
00104     std::cout << vec[i] ;
00105   }
00106 }
00107 
00109 class letter_digit_iterator
00110 {
00111 public:
00112   char count;
00113 
00114   letter_digit_iterator()
00115   {
00116     count = 122;
00117   }
00118 
00119   char next() {
00120     count++;
00121     if (count == 123) count = 48;
00122     else if (count == 91) count = 97;
00123     else if (count == 58) count = 65;
00124     return count;
00125   }
00126 
00127   void reset() {
00128     count = 122;
00129   }
00130 
00131 };
00132 
00134 
00139 template <class T>
00140 static bool search_for_objects(sc_core::sc_object *node, const sc_core::sc_object* but_not_childs_of)
00141 {
00142   // read out all childs and scan them
00143   {
00144     const std::vector<sc_core::sc_object *> *childs_stack = NULL;
00145     if (node) {
00146       // get child vector
00147 #if SYSTEMC_API == 210
00148       // SystemC2.1 cannot get childs of an object, so only modules!
00149       sc_core::sc_module* node_as_module = dynamic_cast< sc_core::sc_module* >(node);
00150       if (node_as_module) {
00151         //sc_assert(node_as_module);
00152         if (node_as_module)
00153           childs_stack = &(node_as_module->get_child_objects());
00154       }
00155 #else
00156       childs_stack = &node->get_child_objects();
00157 #endif
00158     } else { // If NULL, then top level
00159       // get child vector of sim context
00160 #if SYSTEMC_API == 210
00161       sc_core::sc_simcontext *sim; // deprecated with SystemC-2.2
00162       sim = sc_core::sc_get_curr_simcontext(); // deprecated with SystemC-2.2
00163       childs_stack = &(sim->get_child_objects());
00164 #else
00165       childs_stack = &sc_core::sc_get_top_level_objects();
00166 #endif
00167     }
00168     // If node can be casted to T, return true
00169     T *type = dynamic_cast<T*>(node);
00170     if (type) {
00171       return true;
00172     }
00173     
00174     // go through childs
00175     if (childs_stack) {
00176       for (unsigned int i = 0; i < childs_stack->size(); i++) {
00177         sc_core::sc_object *chnode = childs_stack->at(i);
00178         if (chnode != but_not_childs_of) { // do not search within but_not_childs_of
00179           if (search_for_objects<T>(chnode, but_not_childs_of))
00180             return true; // break search if one found
00181         }
00182       }
00183     }
00184   }
00185   return false;
00186 }
00187   
00189 
00200   inline std::string convert_to_sc_name(std::string name) {
00201     std::stringstream ssret;
00202     std::string::iterator my_iter;
00203     const std::string match = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00204     for(my_iter = name.begin(); my_iter != name.end(); my_iter++)
00205     {
00206       size_t found = match.find_first_of(*my_iter);
00207       //if (isalnum(*my_iter) == 1)
00208       if (found != std::string::npos)
00209         ssret << *my_iter;
00210       else 
00211         ssret << "_";
00212     }
00213     return ssret.str();
00214   }
00215   
00216 /*
00217 // ////// Trim functions for std::string
00218 
00219 #include <iostream>
00220 #include <string>
00221 #include <cstdlib>
00222 #include <cctype>
00223 //#include <algorithm>
00224 //#include <boost/algorithm/string.hpp>
00225      
00227 //static std::string TrimLeft(const std::string s)
00228 //{
00229 //  return std::string(std::find_if(s.begin(), s.end(), std::isgraph), s.end());
00230 //}
00231 
00233 //static std::string TrimRight(const std::string s)
00234 //{
00235 //  return std::string(s.begin(), std::find_if(s.rbegin(), s.rend(), isgraph).base());
00236 //}
00237 
00239 //static std::string TrimString(const std::string s)
00240 //{
00241   //return boost::algorithm::trim_copy(s);
00242   //return TrimLeft(TrimRight(s));
00243 //}
00244 */
00245 
00246 } // end namespace ctr
00247 } // end namespace gs
00248 
00249 #endif

Generated on Thu Jan 8 00:30:54 2009 for GreenSocs GreenControl (detailed API reference) by  doxygen 1.5.1