00001 #include "resource.h" 00002 00003 Resource::Resource(){ 00004 numOfOwners = 0; 00005 numOfWaiters = 0; 00006 for(int x=0;x<2;x++){ 00007 waitingTrains[x] = NULL; 00008 } 00009 } 00010 00011 Resource::~Resource(){ 00012 } 00013 00014 BOOL Resource::setOwner(Train* train){ 00015 BOOL returning = FALSE; 00016 if(numOfOwners == 0){ 00017 numOfOwners++; 00018 ownerTrains[0] = train; 00019 returning = TRUE; 00020 ownersDirection = train->getDirection(); 00021 } 00022 return returning; 00023 } 00024 00025 BOOL Resource::moveToOwner(){ 00026 BOOL returning = FALSE; 00027 int count = numOfWaiters; 00028 for(int x=0;x<numOfWaiters;x++){ 00029 if(waitingTrains[x]){ 00030 if(x == 0){ 00031 if(setOwner(waitingTrains[0])){ 00032 waitingTrains[0]->stopWaiting(); 00033 removeFromWaiting(waitingTrains[0]); 00034 } 00035 } 00036 else if(x == 2) 00037 waitingTrains[0] = waitingTrains[1]; 00038 waitingTrains[x] = NULL; 00039 count--; 00040 } 00041 returning = TRUE; 00042 } 00043 numOfWaiters = count; 00044 return returning; 00045 } 00046 00047 00048 BOOL Resource::removeOwner(Train* train){ 00049 BOOL returning = FALSE; 00050 int count = numOfOwners; 00051 for(int x=0;x<numOfOwners;x++){ 00052 if(ownerTrains[x] == train){ 00053 if(x == 2) 00054 ownerTrains[0] = waitingTrains[1]; 00055 ownerTrains[x] = NULL; 00056 count--; 00057 returning = TRUE; 00058 } 00059 } 00060 numOfOwners = count; 00061 return returning; 00062 } 00063 00064 int Resource::numberOfOwners(){ 00065 return numOfOwners; 00066 } 00067 00068 BOOL Resource::addToWaiting(Train* train){ 00069 BOOL returning = FALSE; 00070 if(numOfWaiters >= 2) { 00071 returning = FALSE; 00072 } else{ 00073 waitingTrains[numOfWaiters] = train; 00074 numOfWaiters++; 00075 returning = TRUE; 00076 } 00077 return returning; 00078 } 00079 00080 BOOL Resource::removeFromWaiting(Train* train){ 00081 BOOL returning = FALSE; 00082 int count = numOfWaiters; 00083 for(int x=0;x<numOfWaiters;x++){ 00084 if(waitingTrains[x] == train){ 00085 if(x == 2) 00086 waitingTrains[0] = waitingTrains[1]; 00087 waitingTrains[x] = NULL; 00088 count--; 00089 returning = TRUE; 00090 } 00091 } 00092 numOfWaiters = count; 00093 return returning; 00094 } 00095 00096 int Resource::numberOfWaiters(){ 00097 return numOfWaiters; 00098 } 00099 00100 BOOL Resource::isActive(){ 00101 return resourceActive; 00102 } 00103 00104 void Resource::activate(BOOL resourceActive){ 00105 this->resourceActive = resourceActive; 00106 } 00107