00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/packer/Primitive.hpp $ 00003 // $Id: Primitive.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the Primitive class. 00018 00019 #ifndef TORC_PACKER_PRIMITIVE_HPP 00020 #define TORC_PACKER_PRIMITIVE_HPP 00021 00022 #include "torc/packer/Component.hpp" 00023 #include "torc/packer/Element.hpp" 00024 00025 00026 #include <string> 00027 00028 namespace torc { 00029 namespace physical { 00030 00031 /// \brief primitive. 00032 00033 class Primitive : public Component { 00034 00035 // friends 00036 /// \brief The Factory class has direct access to our internals. 00037 friend class RcFactory; 00038 00039 protected: 00040 // types 00041 /// \brief Imported type name. 00042 typedef std::string string; 00043 // members 00044 /// \brief Vector of element shared pointers. 00045 ElementSharedPtrVector mElements; 00046 00047 // constructors 00048 Primitive(const string& inName) : Component(inName){} 00049 public: 00050 00051 // types 00052 /// \brief Constant iterator for Element shared pointers. 00053 typedef ElementSharedPtrVector::const_iterator ElementSharedPtrConstIterator; 00054 /// \brief Non-constant iterator for Element shared pointers. 00055 typedef ElementSharedPtrVector::iterator ElementSharedPtrIterator; 00056 // functions 00057 /// \brief Find a primitive element by name. 00058 /// \param inName The element name to look for. 00059 /// \returns an iterator for the specified element, or elementsEnd() if the name was not 00060 /// found. 00061 ElementSharedPtrIterator findElement(const string& inName) { 00062 NameComparator predicate(inName); 00063 return std::find_if(elementsBegin(), elementsEnd(), predicate); 00064 } 00065 /// \brief Add a element to the primitive. 00066 /// \param inElementPtr The element to add. 00067 /// \returns true if the element was added, or false if a element with the same name already 00068 /// exists in the primitive. 00069 bool addElement(ElementSharedPtr& inElementPtr) { 00070 /// \todo Acquire mutex. 00071 ElementSharedPtrVector::iterator e = mElements.end(); 00072 ElementSharedPtrVector::iterator result = findElement(inElementPtr->getName()); 00073 if(result == e) mElements.push_back(inElementPtr); 00074 return result == e; // return true if we added the element 00075 /// \todo Release mutex. 00076 } 00077 /// \brief Remove a element from the primitive. 00078 /// \param inElementPtr The element to remove. 00079 /// \returns true if the element was remove, or false if the element did not exist. 00080 bool removeElement(ElementSharedPtr& inElementPtr) { 00081 /// \todo Acquire mutex. 00082 ElementSharedPtrVector::iterator e = mElements.end(); 00083 ElementSharedPtrVector::iterator result = std::find(mElements.begin(), e, inElementPtr); 00084 if(result == e) return false; 00085 00086 //adjust connections 00087 00088 ConnectionPinVector sinkPins; 00089 ConnectionPinVector sourcePins; 00090 ConnectionSharedPtrVector sourceConnections; 00091 ConnectionSharedPtrVector sinkConnections; 00092 ElementSharedPtrVector sourceElements; 00093 ElementSharedPtrVector sinkElements; 00094 00095 Connection::ConnectionPinSharedPtrIterator pinIt, pinIt2; 00096 Element::ConnectionSharedPtrIterator cnIt, cnIt2; 00097 00098 ConnectionPin pin; 00099 00100 ConnectionSharedPtr cnPtr; 00101 ElementSharedPtr elemPtr; 00102 for( cnIt = inElementPtr->connectionsBegin(); cnIt != inElementPtr->connectionsEnd(); ++cnIt){ 00103 if( (*cnIt)->getSource()->getElementName() == inElementPtr->getName() ){ 00104 pin = *(*cnIt)->getSink(); 00105 sinkPins.push_back(pin); 00106 elemPtr = *findElement(pin.getElementName()); 00107 sinkElements.push_back(elemPtr); 00108 for(cnIt2 = elemPtr->connectionsBegin(); cnIt2 != elemPtr->connectionsEnd(); ++cnIt2){ 00109 if( (*cnIt2)->getSink()->getPinName() == pin.getPinName()){ 00110 sinkConnections.push_back(*cnIt2); 00111 elemPtr->removeConnection(cnIt2); 00112 break; 00113 } 00114 } 00115 } 00116 else{ 00117 pin = *(*cnIt)->getSource(); 00118 sourcePins.push_back(pin); 00119 elemPtr = *findElement(pin.getElementName()); 00120 sourceElements.push_back(elemPtr); 00121 for(cnIt2 = elemPtr->connectionsBegin(); cnIt2 != elemPtr->connectionsEnd(); ++cnIt2){ 00122 if( (*cnIt2)->getSource()->getPinName() == pin.getPinName()){ 00123 sourceConnections.push_back(*cnIt2); 00124 elemPtr->removeConnection(cnIt2); 00125 break; 00126 } 00127 } 00128 } 00129 } 00130 for(size_t i=0; i<sourcePins.size(); ++i){ 00131 for(size_t j=0; j<sinkPins.size(); ++j){ 00132 ConnectionSharedPtr connectionPtr(new Connection(sourcePins[i].getPinName())); 00133 ConnectionSharedPtr connectionPtr1(new Connection(sourcePins[i].getPinName())); 00134 connectionPtr->addConnectionPin(sourcePins[i]); 00135 connectionPtr->addConnectionPin(sinkPins[j]); 00136 sinkElements[j]->addConnection(connectionPtr); 00137 00138 connectionPtr1->addConnectionPin(sourcePins[i]); 00139 connectionPtr1->addConnectionPin(sinkPins[j]); 00140 sourceElements[i]->addConnection(connectionPtr1); 00141 00142 } 00143 } 00144 mElements.erase(result); 00145 return true; 00146 } 00147 00148 // operators 00149 /// \brief Equality operator. 00150 /// \details This function deems elements equal if their names are identical. 00151 /// \param rhs The element to compare against. 00152 /// \returns true if both element names are identical, or false otherwise. 00153 bool operator ==(const Primitive& rhs) const { return mName == rhs.mName; } 00154 00155 00156 // accessors 00157 00158 size_t getElementCount(void) const { return mElements.size(); } 00159 // iterators 00160 /// \brief Returns the begin constant iterator for elements. 00161 ElementSharedPtrConstIterator elementsBegin(void) const { return mElements.begin(); } 00162 /// \brief Returns the end constant iterator for elements. 00163 ElementSharedPtrConstIterator elementsEnd(void) const { return mElements.end(); } 00164 /// \brief Returns the begin non-constant iterator for elements. 00165 ElementSharedPtrIterator elementsBegin(void) { return mElements.begin(); } 00166 /// \brief Returns the end non-constant iterator for elements. 00167 ElementSharedPtrIterator elementsEnd(void) { return mElements.end(); } 00168 }; 00169 00170 /// \brief Shared pointer encapsulation of a Primitive. 00171 typedef boost::shared_ptr<Primitive> PrimitiveSharedPtr; 00172 00173 /// \brief Vector of Primitive shared pointers. 00174 typedef std::vector<PrimitiveSharedPtr> PrimitiveSharedPtrVector; 00175 00176 00177 00178 } // namespace physical 00179 } // namespace torc 00180 00181 #endif // TORC_PACKER_PRIMITIVE_HPP