xorp

element_factory.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/element_factory.hh,v 1.9 2008/10/02 21:58:06 bms Exp $
00022 
00023 #ifndef __POLICY_COMMON_ELEMENT_FACTORY_HH__
00024 #define __POLICY_COMMON_ELEMENT_FACTORY_HH__
00025 
00026 
00027 
00028 
00029 #include "register_elements.hh"
00030 #include "element_base.hh"
00031 #include "elem_set.hh"
00032 #include "policy_exception.hh"
00033 
00048 class ElementFactory {
00049 public:
00050     // Function called to create element
00051     typedef Element* (*Callback)(const char*);
00052     
00053     // Container which maps a key to a callback. May consider using a hash table.
00054     typedef map<string,Callback> Map;
00055 
00056     ElementFactory();
00057 
00064     class UnknownElement : public PolicyException {
00065     public:
00066     UnknownElement(const char* file, size_t line, 
00067                const string& init_why = "")   
00068             : PolicyException("UnknownElement", file, line, 
00069                   "ElementFactory: unable to create unknown element: " + init_why) {}  
00070     };
00071 
00078     void add(const string& key, Callback cb); 
00079 
00087     Element* create(const string& key, const char* arg);
00088 
00095     static bool can_create(const string& key);
00096 
00097 private:
00104     static Map _map;
00105 
00112     static RegisterElements _regelems;
00113 };
00114 
00115 #endif // __POLICY_COMMON_ELEMENT_FACTORY_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations