xorp

policy_utils.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/policy/common/policy_utils.hh,v 1.11 2008/10/02 21:58:07 bms Exp $
00022 
00023 #ifndef __POLICY_COMMON_POLICY_UTILS_HH__
00024 #define __POLICY_COMMON_POLICY_UTILS_HH__
00025 
00026 
00027 
00028 #include "policy_exception.hh"
00029 
00030 
00034 namespace policy_utils {
00035 
00039 class PolicyUtilsErr : public PolicyException {
00040 public:
00041     PolicyUtilsErr(const char* file, size_t line, const string& init_why = "")   
00042         : PolicyException("PolicyUtilsErr", file, line, init_why) {}
00043 };
00044 
00045 
00053 template <class T>
00054 void 
00055 delete_vector(vector<T*>* v) {
00056     if(!v)
00057     return;
00058 
00059     for(typename vector<T*>::iterator i = v->begin();
00060     i != v->end(); ++i) {
00061 
00062     if(*i)
00063         delete *i;
00064     }
00065     delete v;
00066 }
00067 
00076 template <class T>
00077 void 
00078 clear_container(T& l) {
00079     for(typename T::iterator i = l.begin();
00080     i != l.end(); ++i) {
00081 
00082     if(*i)
00083         delete *i;
00084     }
00085 
00086     l.clear();  
00087 }
00088 
00089 
00096 template <class A, class T>
00097 void 
00098 clear_map(map<A,T*>& m) {
00099     for(typename map<A,T*>::iterator i = m.begin();
00100     i != m.end(); ++i)  {
00101 
00102     if((*i).second)
00103         delete (*i).second;
00104     }   
00105     m.clear();
00106 }
00107 
00114 template <class T>
00115 void 
00116 clear_map_container(T& m) {
00117     for(typename T::iterator i = m.begin();
00118     i != m.end(); ++i)  {
00119 
00120     if((*i).second)
00121         delete (*i).second;
00122     }   
00123     m.clear();
00124 }
00125 
00132 void str_to_list(const string& in, list<string>& out); 
00133 
00140 void str_to_set(const string& in, set<string>& out); 
00141 
00142 
00149 template <class T>
00150 string
00151 to_str(T x) {
00152     ostringstream oss;
00153     oss << x;
00154     return oss.str();
00155 }
00156 
00157 
00165 void read_file(const string& fname, string& out);
00166 
00173 unsigned count_nl(const char* x);
00174 
00182 bool regex(const string& str, const string& reg);
00183 
00184 };
00185 
00186 
00187 #endif // __POLICY_COMMON_POLICY_UTILS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations