Now you can download a copy of these docs so you can use them offline! Download now
Resource.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "Resource.h" 00008 #include "WPIErrors.h" 00009 #include "ErrorBase.h" 00010 00011 Resource *Resource::m_resourceList = NULL; 00012 00018 Resource::Resource(UINT32 elements) 00019 { 00020 m_size = elements; 00021 m_isAllocated = new bool[m_size]; 00022 for (UINT32 i=0; i < m_size; i++) 00023 m_isAllocated[i] = false; 00024 m_nextResource = m_resourceList; 00025 m_resourceList = this; 00026 } 00027 00028 /*static*/ void Resource::CreateResourceObject(Resource **r, UINT32 elements) 00029 { 00030 if (*r == NULL) 00031 *r = new Resource(elements); 00032 } 00033 00038 Resource::~Resource() 00039 { 00040 delete[] m_isAllocated; 00041 } 00042 00048 UINT32 Resource::Allocate(const char *resourceDesc) 00049 { 00050 for (UINT32 i=0; i < m_size; i++) 00051 { 00052 if (!m_isAllocated[i]) 00053 { 00054 m_isAllocated[i] = true; 00055 return i; 00056 } 00057 } 00058 wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc); 00059 return ~0ul; 00060 } 00061 00067 UINT32 Resource::Allocate(UINT32 index, const char *resourceDesc) 00068 { 00069 if (index >= m_size) 00070 { 00071 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); 00072 return ~0ul; 00073 } 00074 if ( m_isAllocated[index] ) 00075 { 00076 wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc); 00077 return ~0ul; 00078 } 00079 m_isAllocated[index] = true; 00080 return index; 00081 } 00082 00083 00089 void Resource::Free(UINT32 index) 00090 { 00091 if (index == ~0ul) return; 00092 if (index >= m_size) 00093 { 00094 wpi_setWPIError(NotAllocated); 00095 return; 00096 } 00097 if (!m_isAllocated[index]) 00098 { 00099 wpi_setWPIError(NotAllocated); 00100 return; 00101 } 00102 m_isAllocated[index] = false; 00103 }
Generated on Thu Jan 12 2012 22:35:23 for WPILibC++ by
1.7.1