Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

gapeunitgroup.cpp

00001 //GapeUnitGroup.cpp
00002 //Dave Chisholm dkc@ccrma.stanford.edu
00003 //11/20/00
00004 
00005 #include "gapeunitgroup.h"
00006 
00007 GapeUnitGroup::GapeUnitGroup(GapeController* c, int capacity)
00008 : GapeUnit() {
00009     units = new GapeUnit* [capacity];
00010     for (int i = 0; i < capacity; i++) {
00011         units[i] = NULL;
00012     }
00013     currentCapacity = capacity;
00014     numUnits = 0;
00015 }
00016 
00017 
00018 GapeUnitGroup::~GapeUnitGroup()    {
00019     for (int i = 0; i < numUnits; i++) {
00020         delete units[i];
00021         units[i] = NULL;
00022     }
00023 }
00024 
00025 
00026 void GapeUnitGroup::addUnit(GapeUnit* u) {
00027     if (u == NULL) return;
00028     updateMutex.lock();
00029     if (numUnits == currentCapacity) {
00030         units = (GapeUnit**) GapeConsts::expandArray((void**)units, &currentCapacity);
00031     }
00032 
00033 
00034     //rewire old connections if necesary
00035     if (numUnits != 0) {
00036         disconnect(units[numUnits-1], SIGNAL(emitTick(const GapeFloat*,int)), this, SIGNAL(emitTick(const GapeFloat*,int)));
00037         connect(units[numUnits-1], SIGNAL(emitTick(const GapeFloat*,int)), u, SLOT(receiveTick(const GapeFloat*,int)));
00038     }
00039 
00040     //put the new unit at the end of our chain
00041     units[numUnits] = u;
00042     connect(u, SIGNAL(emitTick(const GapeFloat*,int)), this, SIGNAL(emitTick(const GapeFloat*,int)));//wire the new units output to ours
00043     numUnits++;
00044 
00045     for (int i = 0; i < numControllers; i++) {
00046         u->addController(controllers[i]);
00047     }
00048     updateMutex.unlock();
00049 }
00050 
00051 
00052 void GapeUnitGroup::removeUnit(GapeUnit* u) {
00053     int i;
00054     if (u == NULL) return;// false;
00055 
00056     for (i = 0;i < numUnits; i++) {
00057         if (units[i] == u) break;//find the unit
00058     }
00059 
00060     if (i == numUnits) {
00061         GapeConsts::reportError("Tried to remove unit from unit group that wasn't there! GapeUnitGroup.cpp\n");
00062         return;// false;//we don't actually have the unit!
00063     }
00064     updateMutex.lock();
00065     if (i != 0) {//if this is not our first unit, we disconnect the unit before it
00066         disconnect(units[i-1], SIGNAL(emitTick(const GapeFloat*,int)), units[i], SLOT(receiveTick(const GapeFloat*,int)));
00067 
00068         if (i < (numUnits - 1)) {//and if its not our last unit, we disconnect the following unit
00069             disconnect(units[i], SIGNAL(emitTick(const GapeFloat*,int)), units[i+1], SLOT(receiveTick(const GapeFloat*,int)));
00070             //then connect the unit before to the unit after
00071             connect(units[i-1], SIGNAL(emitTick(const GapeFloat*,int)), units[i+1], SLOT(receiveTick(const GapeFloat*,int)));
00072 
00073         } else {//ie this is our last unit and not the first, we disconnect from our unitGroup's emit signal
00074             disconnect(units[i], SIGNAL(emitTick(const GapeFloat*,int)), this, SIGNAL(emitTick(const GapeFloat*,int)));
00075             //and connect the previous unit to the emitTick signal
00076             connect(units[i-1], SIGNAL(emitTick(const GapeFloat*,int)), this, SIGNAL(emitTick(const GapeFloat*,int)));
00077         }
00078 
00079 
00080     } else {//ie this is our first unit
00081         if (i < (numUnits - 1)) {//and if its not also our last unit, we disconnect the following unit
00082             disconnect(units[i], SIGNAL(emitTick(const GapeFloat*,int)), units[i+1], SLOT(receiveTick(const GapeFloat*,int)));
00083 
00084         } else {//ie this is our first and last unit, we disconnect from our unitGroup's emit signal(ie numUnits ==1)
00085             disconnect(units[i], SIGNAL(emitTick(const GapeFloat*,int)), this, SIGNAL(emitTick(const GapeFloat*,int)));
00086         }
00087     }
00088 
00089     //now we shift everything after the deleted unit back one in the array
00090     for (int j = i + 1; j < numUnits; j++) {
00091         units[j-1] = units[j];
00092     }
00093 
00094     for (i = 0; i < numControllers; i++) {
00095         u->removeController(controllers[i]);
00096     }
00097 
00098     units[numUnits-1] = NULL;
00099     numUnits--;
00100     updateMutex.unlock();
00101     return;// true;
00102 }
00103 
00104 
00105 /*void** expandArray(void** array, int* oldCapacity, int factor) {
00106     void** temp = new void* [oldSize*factor];
00107     memcpy(temp, array,(*oldSize) * sizeof(void*));
00108     (*oldCapacity) *= 2;
00109     delete array
00110     return temp;
00111 }
00112 
00113 //this function increases the capacity of our group, by allocating an array twice as large and then coping the contents of the old into the new
00114 void GapeUnitGroup::expandUnit() {
00115     GapeUnit** temp = new GapeUnit* [currentCapacity * 2];
00116     memcpy(temp, units, numUnits * sizeof(GapeUnit*));
00117     currentCapacity *= 2;
00118     delete[] units;
00119     units = temp;
00120 }
00121 */
00122 
00123 
00124 void GapeUnitGroup::addController(GapeController* c) {
00125     GapeUnit::addController(c);
00126     for (int i = 0; i < numUnits; i++) {
00127         units[i]->addController(c);
00128     }
00129 }
00130 
00131 
00132 void GapeUnitGroup::removeController(GapeController* c) {
00133     GapeUnit::removeController(c);
00134     for (int i = 0; i < numUnits; i++) {
00135         units[i]->removeController(c);
00136     }
00137 }
00138 
00139 
00140 GapeUnit* GapeUnitGroup::getUnit(int index) {
00141     if (index >= numUnits || index < 0) {
00142         return NULL;
00143     } else {
00144         return units[index];
00145     }
00146 }
00147 
00148 
00149 void GapeUnitGroup::requestSolo(GapeUnit* requestingUnit) {
00150     for (int i = 0; i < numUnits; i++) {
00151         if (units[i] != requestingUnit) {
00152             units[i]->setMute(true);
00153         }
00154     }
00155 }
00156 
00157 
00158 void GapeUnitGroup::endSolo(GapeUnit* requestingUnit) {
00159     for (int i = 0; i < numUnits; i++) {
00160         if (units[i] != requestingUnit) {
00161             units[i]->setMute(false);
00162         }
00163     }
00164 }

Generated at Thu Jun 21 13:28:50 2001 for GAPE by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001