xorp

var_map.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/var_map.hh,v 1.12 2008/10/02 21:58:01 bms Exp $
00022 
00023 #ifndef __POLICY_VAR_MAP_HH__
00024 #define __POLICY_VAR_MAP_HH__
00025 
00026 
00027 
00028 
00029 
00030 
00031 #include "policy/common/policy_exception.hh"
00032 #include "policy/common/varrw.hh"
00033 
00034 #include "process_watch.hh"
00035 
00045 class VarMap :
00046     public NONCOPYABLE
00047 {
00048 public:
00052     class VarMapErr : public PolicyException {
00053     public:
00054         VarMapErr(const char* file, size_t line, const string& init_why = "")   
00055             : PolicyException("VarMapErr", file, line, init_why) {} 
00056     };
00057 
00061     enum Access {
00062     READ,
00063     READ_WRITE,
00064     WRITE
00065     };
00066 
00070     struct Variable {
00071     Access access;
00072     string name;
00073     string type;
00074     VarRW::Id id;
00075 
00076     Variable(const string& n, const string& t, Access a,
00077          VarRW::Id i) :
00078             access(a), name(n), type(t), id(i)
00079     {
00080     }
00081 
00082     Variable(const Variable& v)
00083     {
00084         access = v.access;
00085         name = v.name;
00086         type = v.type;
00087         id = v.id;
00088     }
00089 
00090     bool writable() const 
00091     { 
00092         return access == READ_WRITE || access == WRITE; 
00093     }
00094     
00095     bool operator==(const Variable& other) const {
00096         return ((access == other.access)
00097             && (name == other.name)
00098             && (type == other.type)
00099             && (id == other.id));
00100     }
00101     };
00102 
00103     typedef map<VarRW::Id,Variable*> VariableMap;
00104     typedef map<string,VariableMap*> ProtoMap;
00105 
00117     const Variable& variable(const string& protocol, 
00118                  const VarRW::Id& varname) const;
00119 
00120     VarRW::Id var2id(const string& protocol, const string& varname) const;
00121 
00129     VarMap(ProcessWatchBase& pw);
00130     ~VarMap();
00131 
00136     bool protocol_known(const string& protocol);
00137 
00144     void add_protocol_variable(const string& protocol, Variable* var);
00145 
00146     
00152     string str(); 
00153 
00154 private:
00160     void add_metavariable(Variable *var);
00161 
00168     void add_variable(VariableMap& vm, Variable* var);
00169 
00179     const VariableMap& variablemap(const string& protocol) const;
00180 
00181     ProtoMap _protocols;
00182     ProcessWatchBase& _process_watch;
00183 
00184     typedef VariableMap MetaVarContainer;
00185     MetaVarContainer _metavars;
00186 };
00187 
00188 #endif // __POLICY_VAR_MAP_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations