torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogicElement.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
18 
19 namespace torc {
20 namespace generic {
21 
22 /**
23  * Create a logic element.
24  *
25  * @param[in] inType Type of the logic value.
26  * @param[in] inLogicValue Pointer to the logic value [optional],
27  * needed for single logic element creation.
28  * @param[in] inParentLogicElement Pointer to parent logic element [optional].
29  *
30  * @return Pointer to created permutable.
31  */
33  const LogicValueSharedPtr& inLogicValue, const LogicElementSharedPtr& inParentLogicElement)
34  throw (Error) {
35  try {
36  LogicElementSharedPtr newLogicElement;
37  create(newLogicElement);
38  newLogicElement->setType(inType);
39  if(inLogicValue) {
40  newLogicElement->setName(inLogicValue->getName());
41  }
42  if(inParentLogicElement) {
43  inParentLogicElement->addChildLogicElement(newLogicElement);
44  }
45  return newLogicElement;
46  } catch(Error& e) {
47  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
48  throw;
49  }
50 }
51 
52 /**
53  * Set the Logic element type
54  *
55  * @param[in] inSource Logic element type
56  */
57 void LogicElement::setType(const Type& inSource) {
58  mType = inSource;
59 }
60 
61 /**
62  * Set the nested logic elements.
63  *
64  * @param[in] inSource Vector containing logic elements.
65  * @exception Error Could not add child logic elements because
66  * pointer to the logic elements does not exist
67  */
68 void LogicElement::setChildren(const std::vector<LogicElementSharedPtr>& inSource) {
69  std::vector<LogicElementSharedPtr>::const_iterator entry = inSource.begin();
70  std::vector<LogicElementSharedPtr>::const_iterator end = inSource.end();
71  for(; entry != end; ++entry) {
72  try {
73  addChildLogicElement(*entry);
74  } catch(Error& e) {
75  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
76  throw;
77  }
78  }
79 }
80 
81 /**
82  * Add a logic element to parent logic element
83  *
84  * @param[in] inChildLogicElement Child logic element to be added to parent
85  */
87  if(!inChildLogicElement) {
88  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
89  e.saveContextData("Pointer to the logic element object does not exist",
90  inChildLogicElement);
91  throw e;
92  } else {
93  mChildren.push_back(inChildLogicElement);
94  return true;
95  }
96 }
97 
98 void LogicElement::accept(BaseVisitor& visitor) throw (Error) {
99  try {
100  runVisitor(*this, visitor);
101  } catch(Error& e) {
102  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
103  throw;
104  }
105 }
106 
107 /**
108  * Set the relation type.
109  *
110  * @param[in] inSource RelationType
111  */
113  mRelationType = inSource;
114 }
115 
116 /**
117  * Get the total number of bits of the composition
118  * @return Number of bits
119  */
120 size_t LogicElement::getSize() const {
121  size_t size = 0;
122  size_t pSize = 0;
123  size_t cSize = 0;
125 
126  std::vector<LogicElementSharedPtr> outLogicElements;
127  getChildren(outLogicElements);
128 
129  std::vector<LogicElementSharedPtr>::iterator logicElemIt = outLogicElements.begin();
131  return 1;
132  }
133 
135  if(!outLogicElements.empty()) {
136  size += (*logicElemIt)->getSize();
137  } else {
138  size = 0;
139  }
140  return size;
141  }
142 
144  if(!outLogicElements.empty()) {
145  size += (*logicElemIt)->getSize();
146  } else {
147  size = 0;
148  }
149  return size;
150  }
151 
152  if(LogicElement::eRelationTypeParent == rType) {
153  if(!outLogicElements.empty()) {
154  pSize += (*logicElemIt)->getSize();
155  } else {
156  pSize = 0;
157  }
158  } else {
159  if(!outLogicElements.empty()) {
160  for(; logicElemIt != outLogicElements.end(); logicElemIt++) {
161  cSize += (*logicElemIt)->getSize();
162  }
163  }
164  }
165  size = pSize + cSize;
166  return size;
167 }
168 
169 LogicElement::LogicElement() : LogicValue(), mType(), mRelationType(), mChildren() {}
170 
172 
173 } // namespace generic
174 } // namespace torc
boost::shared_ptr< LogicValue > LogicValueSharedPtr
boost::shared_ptr< LogicElement > LogicElementSharedPtr
virtual void accept(BaseVisitor &visitor)
Receive a visitor to this class. The visit method of the visitor is called and a reference to this ob...
void setRelationType(const RelationType &inSource)
void setChildren(const std::vector< LogicElementSharedPtr > &inSource)
const RelationType getRelationType() const
This class is used within simulationInfo construct to define a logic value to use for modeling in the...
Definition: LogicValue.hpp:42
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
bool addChildLogicElement(const LogicElementSharedPtr &inChildLogicElement)
A base class for Visitor.
Definition: VisitorType.hpp:31
void setType(const Type &inSource)
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void getChildren(std::vector< LogicElementSharedPtr > &outLogicElements) const
std::vector< LogicElementSharedPtr > mChildren
virtual LogicElementSharedPtr newLogicElementPtr(const LogicElement::Type &inType, const LogicValueSharedPtr &inLogicValue=LogicValueSharedPtr(), const LogicElementSharedPtr &inParentLogicElement=LogicElementSharedPtr())
const Type getType() const
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73