degate  0.1.2
ObjectIDRewriter.h
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 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __OBJECTIDREWRITER_H__
00023 #define __OBJECTIDREWRITER_H__
00024 
00025 #include "globals.h"
00026 
00027 #include <stdexcept>
00028 #include <sstream>
00029 #include <map>
00030 
00031 namespace degate {
00032 
00033 /**
00034  * This class is used to defragment object IDs from a logic model.
00035  *
00036  * Each logic model object has a uniquie identifier. The logic model keeps track which
00037  * object IDs are in use and which are free. Internally there are cases where we want to
00038  * reorganize the object IDs. The ObjectIDRewriter is used therefore.
00039  *
00040  */
00041 
00042 class ObjectIDRewriter {
00043 
00044 private:
00045 
00046   std::map<object_id_t, object_id_t> table;
00047   object_id_t oid_counter;
00048 
00049   bool enable_id_rewrite;
00050 
00051 public:
00052 
00053   /**
00054    * Construct a new Rewriter.
00055    * @param _enable_id_rewrite If this parameter is set to false, then there is no object ID rewriting.
00056    */
00057   ObjectIDRewriter(bool _enable_id_rewrite = true) :
00058     oid_counter(2342),
00059     enable_id_rewrite(_enable_id_rewrite) {};
00060 
00061   /**
00062    * The destructor.
00063    */
00064 
00065   virtual ~ObjectIDRewriter() {};
00066 
00067   /**
00068    * Get an object ID replacement.
00069    * If you called the ctor with 'false', then you will get the the same object ID back. This is somehow
00070    * a pass through mode.
00071    * @param old_id For this object ID yoy will get a defragmented object ID.
00072    * @return Returns another object ID.
00073    */
00074   object_id_t get_new_object_id(object_id_t old_id) {
00075     if(!enable_id_rewrite) return old_id;
00076     else {
00077       if(table.find(old_id) == table.end()) {
00078         table[old_id] = oid_counter;
00079         return oid_counter++;
00080       }
00081       else return table[old_id];
00082     }
00083   }
00084 
00085 };
00086 
00087 typedef std::shared_ptr<ObjectIDRewriter> ObjectIDRewriter_shptr;
00088 
00089 }
00090 
00091 #endif