custom for >5 resources
Fork of mbedConnectorInterface by
Diff: api/InstancePointerTable.cpp
- Revision:
- 2:853f9ecc12df
- Parent:
- 0:b438482ebbfc
- Child:
- 5:a929d65eb385
diff -r cabdd0350707 -r 853f9ecc12df api/InstancePointerTable.cpp --- a/api/InstancePointerTable.cpp Tue Jan 27 22:52:25 2015 +0000 +++ b/api/InstancePointerTable.cpp Tue Jan 27 23:41:34 2015 +0000 @@ -3,7 +3,7 @@ * @brief instance pointer table header * @author Doug Anson/Chris Paola * @version 1.0 - * @see + * @see * * Copyright (c) 2014 * @@ -19,45 +19,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - #include "InstancePointerTable.h" - - // constructor - InstancePointerTable::InstancePointerTable(const Logger *logger) { - this->m_logger = (Logger *)logger; - } - - // destructor - InstancePointerTable::~InstancePointerTable() { - } - - // add to the table - void InstancePointerTable::add(string key,void *instance) { - // get our index - int index = this->indexFromKey(key); - if (index >= 0) { - // overwrite the existing reference we have... - std::printf("InstancePointerTable: ADD(overwrite) instance: key[%s] index=%d\r\n",key.c_str(),index); - this->m_instance_pointer_table[index].instance = instance; - } - else { + +#include "InstancePointerTable.h" + +// constructor +InstancePointerTable::InstancePointerTable(const Logger *logger) +{ + this->m_logger = (Logger *)logger; +} + +// destructor +InstancePointerTable::~InstancePointerTable() +{ +} + +// add to the table +void InstancePointerTable::add(string key,void *instance) +{ + // get our index + int index = this->indexFromKey(key); + if (index >= 0) { + // overwrite the existing reference we have... + std::printf("InstancePointerTable: ADD(overwrite) instance: key[%s] index=%d\r\n",key.c_str(),index); + this->m_instance_pointer_table[index].instance = instance; + } else { // get the next empty slot index = this->nextEmptySlot(); - if (index >= 0) { + if (index >= 0) { // set the table entry this->m_instance_pointer_table[index].key = key; this->m_instance_pointer_table[index].instance = instance; std::printf("InstancePointerTable: ADD instance: key[%s] index=%d\r\n",key.c_str(),index); - } - else { + } else { // table is FULL! Error... std::printf("ERROR: InstancePointerTable is FULL... please expand table\r\n"); } } - } - - // get from the table - void *InstancePointerTable::get(string key) { +} + +// get from the table +void *InstancePointerTable::get(string key) +{ // get our index int index = this->indexFromKey(key); if (index >= 0) { @@ -65,36 +67,47 @@ return this->m_instance_pointer_table[index].instance; } return NULL; - } - - // find next empty slot - int InstancePointerTable::nextEmptySlot() { return this->indexFromKey(""); } - - // lookup into the table - int InstancePointerTable::indexFromKey(string key) { - int index = -1; - bool found = false; - - for(int i=0;i<IPT_MAX_ENTRIES && !found;++i) { +} + +// find next empty slot +int InstancePointerTable::nextEmptySlot() +{ + return this->indexFromKey(""); +} + +// lookup into the table +int InstancePointerTable::indexFromKey(string key) +{ + int index = -1; + bool found = false; + + for(int i=0; i<IPT_MAX_ENTRIES && !found; ++i) { if (this->m_instance_pointer_table[i].key.compare(key) == 0) { found = true; index = i; } - } - - return index; - } - - // initialize - void InstancePointerTable::init() { - for(int i=0;i<IPT_MAX_ENTRIES;++i) { - this->m_instance_pointer_table[i].key = ""; - this->m_instance_pointer_table[i].instance = NULL; - } - } - - // set our logger - void InstancePointerTable::setLogger(const Logger *logger) { this->m_logger = (Logger *)logger; } - - // get our logger - Logger *InstancePointerTable::logger() { return this->m_logger; } \ No newline at end of file + } + + return index; +} + +// initialize +void InstancePointerTable::init() +{ + for(int i=0; i<IPT_MAX_ENTRIES; ++i) { + this->m_instance_pointer_table[i].key = ""; + this->m_instance_pointer_table[i].instance = NULL; + } +} + +// set our logger +void InstancePointerTable::setLogger(const Logger *logger) +{ + this->m_logger = (Logger *)logger; +} + +// get our logger +Logger *InstancePointerTable::logger() +{ + return this->m_logger; +}