1 /* 2 * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz 3 * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include "Row.h" 9 10 #include "ALMLayout.h" 11 #include "OperatorType.h" 12 #include "YTab.h" 13 14 #include <SupportDefs.h> 15 16 17 /** 18 * The top boundary of the row. 19 */ 20 YTab* 21 Row::Top() const 22 { 23 return fTop; 24 } 25 26 27 /** 28 * The bottom boundary of the row. 29 */ 30 YTab* 31 Row::Bottom() const 32 { 33 return fBottom; 34 } 35 36 37 /** 38 * Gets the row directly above this row. 39 */ 40 Row* 41 Row::Previous() const 42 { 43 return fPrevious; 44 } 45 46 47 /** 48 * Sets the row directly above this row. 49 * May be null. 50 */ 51 void 52 Row::SetPrevious(Row* value) 53 { 54 // if there should be no row directly above this row, then we have to 55 // separate any such row and can remove any constraint that was used 56 // to glue this row to it 57 if (value == NULL) { 58 if (fPrevious == NULL) 59 return; 60 fPrevious->fNext = NULL; 61 fPrevious->fNextGlue = NULL; 62 fPrevious = NULL; 63 delete fPreviousGlue; 64 fPreviousGlue = NULL; 65 return; 66 } 67 68 // otherwise we have to set up the pointers and the glue constraint accordingly 69 if (value->fNext != NULL) 70 value->SetNext(NULL); 71 if (fPrevious != NULL) 72 SetPrevious(NULL); 73 74 fPrevious = value; 75 fPrevious->fNext = this; 76 value->fNextGlue = value->fBottom->IsEqual(fTop); 77 fPreviousGlue = value->fNextGlue; 78 } 79 80 81 /** 82 * Gets the row directly below this row. 83 */ 84 Row* 85 Row::Next() const 86 { 87 return fNext; 88 } 89 90 91 /** 92 * Sets the row directly below this row. 93 * May be null. 94 */ 95 void 96 Row::SetNext(Row* value) 97 { 98 // if there should be no row directly below this row, then we have to 99 // separate any such row and can remove any constraint that was used 100 // to glue this row to it 101 if (value == NULL) { 102 if (fNext == NULL) 103 return; 104 fNext->fPrevious = NULL; 105 fNext->fPreviousGlue = NULL; 106 fNext = NULL; 107 delete fNextGlue; 108 fNextGlue = NULL; 109 return; 110 } 111 112 // otherwise we have to set up the pointers and the glue constraint accordingly 113 if (value->fPrevious != NULL) 114 value->SetPrevious(NULL); 115 if (fNext != NULL) 116 SetNext(NULL); 117 118 fNext = value; 119 fNext->fPrevious = this; 120 value->fPreviousGlue = fBottom->IsEqual(value->fTop); 121 fNextGlue = value->fPreviousGlue; 122 } 123 124 125 /** 126 * Inserts the given row directly above this row. 127 * 128 * @param row the row to insert 129 */ 130 void 131 Row::InsertBefore(Row* row) 132 { 133 SetPrevious(row->fPrevious); 134 SetNext(row); 135 } 136 137 138 /** 139 * Inserts the given row directly below this row. 140 * 141 * @param row the row to insert 142 */ 143 void 144 Row::InsertAfter(Row* row) 145 { 146 SetNext(row->fNext); 147 SetPrevious(row); 148 } 149 150 151 /** 152 * Constrains this row to have the same height as the given row. 153 * 154 * @param row the row that should have the same height 155 * @return the resulting same-height constraint 156 */ 157 Constraint* 158 Row::HasSameHeightAs(Row* row) 159 { 160 Constraint* constraint = fLS->AddConstraint( 161 -1.0, fTop, 1.0, fBottom, 1.0, row->fTop, -1.0, row->fBottom, 162 OperatorType(EQ), 0.0); 163 fConstraints.AddItem(constraint); 164 return constraint; 165 } 166 167 168 /** 169 * Gets the constraints. 170 */ 171 ConstraintList* 172 Row::Constraints() const 173 { 174 return const_cast<ConstraintList*>(&fConstraints); 175 } 176 177 178 /** 179 * Destructor. 180 * Removes the row from the specification. 181 */ 182 Row::~Row() 183 { 184 if (fPrevious != NULL) 185 fPrevious->SetNext(fNext); 186 for (int32 i = 0; i < fConstraints.CountItems(); i++) 187 delete (Constraint*)fConstraints.ItemAt(i); 188 delete fTop; 189 delete fBottom; 190 } 191 192 193 /** 194 * Constructor. 195 */ 196 Row::Row(LinearSpec* ls) 197 { 198 fLS = ls; 199 fTop = new YTab(ls); 200 fBottom = new YTab(ls); 201 } 202 203