degate  0.1.2
GateTemplatePort.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  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 #ifndef __GATETEMPLATEPORT_H__
00024 #define __GATETEMPLATEPORT_H__
00025 
00026 #include "LogicModelObjectBase.h"
00027 #include "Circle.h"
00028 #include "Point.h"
00029 
00030 namespace degate {
00031 
00032   /**
00033    * This class represents a port of a gate template.
00034    */
00035   class GateTemplatePort : public LogicModelObjectBase, public ColoredObject, public DeepCopyable {
00036 
00037   public:
00038 
00039     /**
00040      * Enum to define type of ports.
00041      */
00042     enum PORT_TYPE {
00043       PORT_TYPE_UNDEFINED = 0,
00044       PORT_TYPE_IN = 1,
00045       PORT_TYPE_OUT = 2,
00046       PORT_TYPE_INOUT = 3
00047     };
00048 
00049   private:
00050 
00051     Point point;
00052     PORT_TYPE port_type;
00053 
00054     bool position_defined;
00055 
00056 
00057   public:
00058 
00059     /**
00060      * Create a template port.
00061      */
00062 
00063     GateTemplatePort(int _x, int _y, PORT_TYPE _port_type = PORT_TYPE_UNDEFINED) :
00064       point(_x, _y), port_type(_port_type), position_defined( true) {
00065     }
00066 
00067     /**
00068      * Create a template port.
00069      */
00070 
00071     GateTemplatePort(PORT_TYPE _port_type = PORT_TYPE_UNDEFINED) :
00072       point(0, 0), port_type(_port_type), position_defined(false) {
00073     }
00074 
00075 
00076     virtual ~GateTemplatePort() {}
00077 
00078     //@{
00079     DeepCopyable_shptr cloneShallow() const {
00080       auto clone = std::make_shared<GateTemplatePort>(port_type);
00081       if (is_position_defined()) {
00082         clone->set_point(point);
00083       }
00084       return clone;
00085     };
00086     
00087     void cloneDeepInto(DeepCopyable_shptr dest, oldnew_t *oldnew) const {
00088       ColoredObject::cloneDeepInto(dest, oldnew);
00089       LogicModelObjectBase::cloneDeepInto(dest, oldnew);
00090     };
00091     //@}
00092 
00093     /**
00094      * Check if a position is defined for the port.
00095      */
00096 
00097     virtual bool is_position_defined() const { return position_defined; }
00098 
00099 
00100     /**
00101      * Set the port position within the gate template.
00102      * The position is in relative coordinates
00103      *   related to the left upper corner of the gate template.
00104      */
00105 
00106     virtual void set_point(Point p) {
00107       position_defined = true;
00108       point = p;
00109     }
00110 
00111     /**
00112      * Check if a port is of type input port or an in-out-port.
00113      */
00114 
00115     virtual bool is_inport() const { return port_type == PORT_TYPE_IN || is_inoutport(); }
00116 
00117     /**
00118      * Check if a port is of type output port or an in-out-port.
00119      */
00120 
00121     virtual bool is_outport() const { return port_type == PORT_TYPE_OUT || is_inoutport(); }
00122 
00123     /**
00124      * Check if a port is of type tristate.
00125      */
00126 
00127     virtual bool is_inoutport() const { return port_type == PORT_TYPE_INOUT; }
00128 
00129     /**
00130      * Check if a port is of type is undefined.
00131      */
00132 
00133     virtual bool has_undefined_port_type() const { return port_type == PORT_TYPE_UNDEFINED; }
00134 
00135     /**
00136      * Set the port type.
00137      */
00138 
00139     virtual void set_port_type(PORT_TYPE _port_type) { port_type = _port_type; }
00140 
00141     /**
00142      * Get the port type.
00143      */
00144 
00145     virtual PORT_TYPE get_port_type() const { return port_type; }
00146 
00147     /**
00148      * Get a human readable string, that describes the port type.
00149      * That is "in", "out" or "tristate".
00150      */
00151 
00152     virtual const std::string get_port_type_as_string() const {
00153       switch(port_type) {
00154       case PORT_TYPE_IN: return std::string("in");
00155       case PORT_TYPE_OUT: return std::string("out");
00156       case PORT_TYPE_INOUT: return std::string("inout");
00157 
00158       case PORT_TYPE_UNDEFINED:
00159       default: return std::string("undefined");
00160       }
00161     }
00162 
00163     /**
00164      * Get the port position within the gate template.
00165      * @return Returns the position in relative coordinates
00166      *   related to the left upper corner of the gate template.
00167      */
00168 
00169     virtual Point const& get_point() const { return point; }
00170 
00171     /**
00172      * Get the x position for a port.
00173      * @return Returns the postion relative to the left upper corner of
00174      *   the gate template.
00175      */
00176 
00177     virtual int get_x() const { return point.get_x(); }
00178 
00179     /**
00180      * Get the y position for a port.
00181      * @return Returns the postion relative to the left upper corner of
00182      *   the gate template.
00183      */
00184 
00185     virtual int get_y() const { return point.get_y(); }
00186 
00187 
00188     /**
00189      * Parse a port type type indicating string.
00190      * @exception DegateRuntimeException This exception is thrown if the string
00191      *   cannot be parsed.
00192      */
00193 
00194     static PORT_TYPE get_port_type_from_string(std::string const& port_type_str) {
00195 
00196       if(port_type_str == "undefined") return GateTemplatePort::PORT_TYPE_UNDEFINED;
00197       else if(port_type_str == "in") return  GateTemplatePort::PORT_TYPE_IN;
00198       else if(port_type_str == "out") return GateTemplatePort::PORT_TYPE_OUT;
00199       else if(port_type_str == "inout") return GateTemplatePort::PORT_TYPE_INOUT;
00200       else throw DegateRuntimeException("Can't parse port type.");
00201     }
00202 
00203   };
00204 
00205   typedef std::shared_ptr<GateTemplatePort> GateTemplatePort_shptr;
00206 
00207 }
00208 
00209 #endif