degate  0.1.2
GateLibrary.cc
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006  Copyright 2012 Robert Nitsch
00007 
00008  Degate is free software: you can redistribute it and/or modify
00009  it under the terms of the GNU General Public License as published by
00010  the Free Software Foundation, either version 3 of the License, or
00011  any later version.
00012 
00013  Degate is distributed in the hope that it will be useful,
00014  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  GNU General Public License for more details.
00017 
00018  You should have received a copy of the GNU General Public License
00019  along with degate. If not, see <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #include <degate.h>
00024 #include <GateLibrary.h>
00025 
00026 using namespace degate;
00027 
00028 GateLibrary::GateLibrary() {
00029 }
00030 
00031 GateLibrary::~GateLibrary() {
00032 }
00033 
00034 DeepCopyable_shptr GateLibrary::cloneShallow() const {
00035   return std::make_shared<GateLibrary>();
00036 }
00037 
00038 void GateLibrary::cloneDeepInto(DeepCopyable_shptr dest, oldnew_t *oldnew) const {
00039   auto clone = std::dynamic_pointer_cast<GateLibrary>(dest);
00040   std::for_each(templates.begin(), templates.end(), [&](const gate_lib_collection_t::value_type &v) {
00041     clone->templates[v.first] = std::dynamic_pointer_cast<GateTemplate>(v.second->cloneDeep(oldnew));
00042   });
00043 }
00044 
00045 void GateLibrary::remove_template(GateTemplate_shptr gate_template) {
00046   templates.erase(gate_template->get_object_id());
00047 }
00048 
00049 void GateLibrary::add_template(GateTemplate_shptr gate_template) {
00050 
00051   if(gate_template == NULL) throw InvalidPointerException();
00052   if(!gate_template->has_valid_object_id())
00053     throw InvalidObjectIDException("Can't add a gate template to the gate library, "
00054                                    "if the template has no valid object ID.");
00055   else
00056     templates[gate_template->get_object_id()] = gate_template;
00057 }
00058 
00059 bool GateLibrary::exists_template(object_id_t id) const {
00060   return templates.find(id) != templates.end();
00061 }
00062 
00063 
00064 GateTemplate_shptr GateLibrary::get_template(object_id_t id) {
00065 
00066   if(id == 0)
00067     throw InvalidObjectIDException("Error in get_template(): Can't lookup template with id == 0");
00068 
00069   template_iterator found = templates.find(id);
00070   if(found == templates.end()) {
00071     boost::format f("Error in get_template(): Can't lookup gate template with ID %1%");
00072     f % id;
00073     throw CollectionLookupException(f.str());
00074   }
00075   return found->second;
00076 }
00077 
00078 
00079 bool GateLibrary::is_name_in_use(std::string const & name) const {
00080 
00081   for(const_template_iterator iter = begin(); iter != end(); ++iter) {
00082     if((*iter).second->has_name() && (*iter).second->get_name() == name)
00083       return true;
00084   }
00085 
00086   return false;
00087 }
00088 
00089 bool GateLibrary::exists_template_port(object_id_t port_id) {
00090 
00091   for(template_iterator iter = begin(); iter != end(); ++iter) {
00092 
00093     GateTemplate_shptr tmpl((*iter).second);
00094 
00095     for(GateTemplate::port_iterator piter = tmpl->ports_begin();
00096         piter != tmpl->ports_end();
00097         piter++) {
00098       if((*piter)->get_object_id() == port_id) return true;
00099     }
00100   }
00101 
00102   return false;
00103 }
00104 
00105 
00106 GateTemplatePort_shptr GateLibrary::get_template_port(object_id_t port_id) {
00107 
00108   for(template_iterator iter = begin(); iter != end(); ++iter) {
00109 
00110     GateTemplate_shptr tmpl((*iter).second);
00111 
00112     for(GateTemplate::port_iterator piter = tmpl->ports_begin();
00113         piter != tmpl->ports_end();
00114         piter++) {
00115       if((*piter)->get_object_id() == port_id) return *piter;
00116     }
00117   }
00118 
00119   std::ostringstream stm;
00120   stm << "There is no template port with ID. " << port_id << " in the gate library.";
00121   throw CollectionLookupException(stm.str());
00122 }
00123 
00124 GateLibrary::template_iterator GateLibrary::begin() {
00125   return templates.begin();
00126 }
00127 
00128 
00129 GateLibrary::template_iterator GateLibrary::end() {
00130   return templates.end();
00131 }
00132 
00133 GateLibrary::const_template_iterator GateLibrary::begin() const {
00134   return templates.begin();
00135 }
00136 
00137 
00138 GateLibrary::const_template_iterator GateLibrary::end() const {
00139   return templates.end();
00140 }
00141 
00142 
00143 void GateLibrary::print(std::ostream & os) {
00144   for(template_iterator iter = begin(); iter != end(); ++iter) {
00145     GateTemplate_shptr tmpl = (*iter).second;
00146 
00147     tmpl->print(os);
00148     os << std::endl;
00149 
00150   }
00151 }