xorp

dependency.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/dependency.hh,v 1.4 2008/10/02 21:57:58 bms Exp $
00022 
00023 #ifndef __POLICY_DEPENDENCY_HH__
00024 #define __POLICY_DEPENDENCY_HH__
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 #include "policy/common/policy_exception.hh"
00035 
00050 template <class T>
00051 class Dependency :
00052     public NONCOPYABLE
00053 {
00054 public:
00055     // things that depend on object
00056     typedef list<string>        DependencyList;
00057 
00058     // object dependency pair.
00059     typedef pair<T*,DependencyList> Pair;
00060 
00061     // object of this name has these dependencies
00062     typedef map<string,Pair*>       Map;
00063 
00064     typedef set<string>         KEYS;
00065 
00066     struct ObjPair {
00067     const string& name;
00068     const T& object;
00069 
00070     ObjPair(const string& n, const T& o) : name(n), object(o) {}
00071     };
00072    
00078     class DependencyError : public PolicyException {
00079     public:
00080         DependencyError(const char* file, size_t line, 
00081             const string& init_why = "")
00082     : PolicyException("DependencyError", file, line, init_why) {} 
00083     };
00084 
00085     Dependency();
00086     ~Dependency();
00087 
00088     void clear();
00089 
00096     bool exists(const string& objectname) const;
00097 
00109     bool create(const string& objectname, T* object);
00110 
00119     void remove(const string& objectname);
00120 
00130     void add_dependency(const string& objectname, const string& dep);
00131 
00140     void del_dependency(const string& objectname, const string& dep);
00141 
00148     T& find(const string& objectname) const;
00149 
00156     T* find_ptr(const string& objectname) const;
00157 
00166     void get_deps(const string& objectname, set<string>& deps) const;
00167 
00177     void update_object(const string& objectname,T* obj);
00178 
00179     // XXX: this interface has to be re-done...
00185     typename Map::const_iterator get_iterator() const;
00186 
00193     bool has_next(const typename Map::const_iterator& i) const;
00194 
00203     ObjPair next(typename Map::const_iterator& i) const;
00204 
00205     void keys(KEYS& out) const;
00206 
00207 private:
00208     Map _map;
00209 
00210     Pair* findDepend(const string& objectname) const;
00211 };
00212 
00213 #endif // __POLICY_DEPENDENCY_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations