custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
sam_grove
Date:
Tue Jan 27 23:41:34 2015 +0000
Revision:
2:853f9ecc12df
Parent:
0:b438482ebbfc
Child:
5:a929d65eb385
Use auto-format on code and add markup to render class documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:b438482ebbfc 1 /**
ansond 0:b438482ebbfc 2 * @file InstancePointerTable.h
ansond 0:b438482ebbfc 3 * @brief instance pointer table header
ansond 0:b438482ebbfc 4 * @author Doug Anson/Chris Paola
ansond 0:b438482ebbfc 5 * @version 1.0
sam_grove 2:853f9ecc12df 6 * @see
ansond 0:b438482ebbfc 7 *
ansond 0:b438482ebbfc 8 * Copyright (c) 2014
ansond 0:b438482ebbfc 9 *
ansond 0:b438482ebbfc 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:b438482ebbfc 11 * you may not use this file except in compliance with the License.
ansond 0:b438482ebbfc 12 * You may obtain a copy of the License at
ansond 0:b438482ebbfc 13 *
ansond 0:b438482ebbfc 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:b438482ebbfc 15 *
ansond 0:b438482ebbfc 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:b438482ebbfc 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:b438482ebbfc 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:b438482ebbfc 19 * See the License for the specific language governing permissions and
ansond 0:b438482ebbfc 20 * limitations under the License.
ansond 0:b438482ebbfc 21 */
sam_grove 2:853f9ecc12df 22
sam_grove 2:853f9ecc12df 23 #include "InstancePointerTable.h"
sam_grove 2:853f9ecc12df 24
sam_grove 2:853f9ecc12df 25 // constructor
sam_grove 2:853f9ecc12df 26 InstancePointerTable::InstancePointerTable(const Logger *logger)
sam_grove 2:853f9ecc12df 27 {
sam_grove 2:853f9ecc12df 28 this->m_logger = (Logger *)logger;
sam_grove 2:853f9ecc12df 29 }
sam_grove 2:853f9ecc12df 30
sam_grove 2:853f9ecc12df 31 // destructor
sam_grove 2:853f9ecc12df 32 InstancePointerTable::~InstancePointerTable()
sam_grove 2:853f9ecc12df 33 {
sam_grove 2:853f9ecc12df 34 }
sam_grove 2:853f9ecc12df 35
sam_grove 2:853f9ecc12df 36 // add to the table
sam_grove 2:853f9ecc12df 37 void InstancePointerTable::add(string key,void *instance)
sam_grove 2:853f9ecc12df 38 {
sam_grove 2:853f9ecc12df 39 // get our index
sam_grove 2:853f9ecc12df 40 int index = this->indexFromKey(key);
sam_grove 2:853f9ecc12df 41 if (index >= 0) {
sam_grove 2:853f9ecc12df 42 // overwrite the existing reference we have...
sam_grove 2:853f9ecc12df 43 std::printf("InstancePointerTable: ADD(overwrite) instance: key[%s] index=%d\r\n",key.c_str(),index);
sam_grove 2:853f9ecc12df 44 this->m_instance_pointer_table[index].instance = instance;
sam_grove 2:853f9ecc12df 45 } else {
ansond 0:b438482ebbfc 46 // get the next empty slot
ansond 0:b438482ebbfc 47 index = this->nextEmptySlot();
sam_grove 2:853f9ecc12df 48 if (index >= 0) {
ansond 0:b438482ebbfc 49 // set the table entry
ansond 0:b438482ebbfc 50 this->m_instance_pointer_table[index].key = key;
ansond 0:b438482ebbfc 51 this->m_instance_pointer_table[index].instance = instance;
ansond 0:b438482ebbfc 52 std::printf("InstancePointerTable: ADD instance: key[%s] index=%d\r\n",key.c_str(),index);
sam_grove 2:853f9ecc12df 53 } else {
ansond 0:b438482ebbfc 54 // table is FULL! Error...
ansond 0:b438482ebbfc 55 std::printf("ERROR: InstancePointerTable is FULL... please expand table\r\n");
ansond 0:b438482ebbfc 56 }
ansond 0:b438482ebbfc 57 }
sam_grove 2:853f9ecc12df 58 }
sam_grove 2:853f9ecc12df 59
sam_grove 2:853f9ecc12df 60 // get from the table
sam_grove 2:853f9ecc12df 61 void *InstancePointerTable::get(string key)
sam_grove 2:853f9ecc12df 62 {
ansond 0:b438482ebbfc 63 // get our index
ansond 0:b438482ebbfc 64 int index = this->indexFromKey(key);
ansond 0:b438482ebbfc 65 if (index >= 0) {
ansond 0:b438482ebbfc 66 std::printf("InstancePointerTable: GET instance: key[%s] index=%d\r\n",key.c_str(),index);
ansond 0:b438482ebbfc 67 return this->m_instance_pointer_table[index].instance;
ansond 0:b438482ebbfc 68 }
ansond 0:b438482ebbfc 69 return NULL;
sam_grove 2:853f9ecc12df 70 }
sam_grove 2:853f9ecc12df 71
sam_grove 2:853f9ecc12df 72 // find next empty slot
sam_grove 2:853f9ecc12df 73 int InstancePointerTable::nextEmptySlot()
sam_grove 2:853f9ecc12df 74 {
sam_grove 2:853f9ecc12df 75 return this->indexFromKey("");
sam_grove 2:853f9ecc12df 76 }
sam_grove 2:853f9ecc12df 77
sam_grove 2:853f9ecc12df 78 // lookup into the table
sam_grove 2:853f9ecc12df 79 int InstancePointerTable::indexFromKey(string key)
sam_grove 2:853f9ecc12df 80 {
sam_grove 2:853f9ecc12df 81 int index = -1;
sam_grove 2:853f9ecc12df 82 bool found = false;
sam_grove 2:853f9ecc12df 83
sam_grove 2:853f9ecc12df 84 for(int i=0; i<IPT_MAX_ENTRIES && !found; ++i) {
ansond 0:b438482ebbfc 85 if (this->m_instance_pointer_table[i].key.compare(key) == 0) {
ansond 0:b438482ebbfc 86 found = true;
ansond 0:b438482ebbfc 87 index = i;
ansond 0:b438482ebbfc 88 }
sam_grove 2:853f9ecc12df 89 }
sam_grove 2:853f9ecc12df 90
sam_grove 2:853f9ecc12df 91 return index;
sam_grove 2:853f9ecc12df 92 }
sam_grove 2:853f9ecc12df 93
sam_grove 2:853f9ecc12df 94 // initialize
sam_grove 2:853f9ecc12df 95 void InstancePointerTable::init()
sam_grove 2:853f9ecc12df 96 {
sam_grove 2:853f9ecc12df 97 for(int i=0; i<IPT_MAX_ENTRIES; ++i) {
sam_grove 2:853f9ecc12df 98 this->m_instance_pointer_table[i].key = "";
sam_grove 2:853f9ecc12df 99 this->m_instance_pointer_table[i].instance = NULL;
sam_grove 2:853f9ecc12df 100 }
sam_grove 2:853f9ecc12df 101 }
sam_grove 2:853f9ecc12df 102
sam_grove 2:853f9ecc12df 103 // set our logger
sam_grove 2:853f9ecc12df 104 void InstancePointerTable::setLogger(const Logger *logger)
sam_grove 2:853f9ecc12df 105 {
sam_grove 2:853f9ecc12df 106 this->m_logger = (Logger *)logger;
sam_grove 2:853f9ecc12df 107 }
sam_grove 2:853f9ecc12df 108
sam_grove 2:853f9ecc12df 109 // get our logger
sam_grove 2:853f9ecc12df 110 Logger *InstancePointerTable::logger()
sam_grove 2:853f9ecc12df 111 {
sam_grove 2:853f9ecc12df 112 return this->m_logger;
sam_grove 2:853f9ecc12df 113 }