added max-age and content-format
Fork of mbedConnectorInterface by
api/InstancePointerTable.cpp@0:b438482ebbfc, 2015-01-27 (annotated)
- Committer:
- ansond
- Date:
- Tue Jan 27 22:23:51 2015 +0000
- Revision:
- 0:b438482ebbfc
- Child:
- 2:853f9ecc12df
initial check in
Who changed what in which revision?
User | Revision | Line number | New 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 |
ansond | 0:b438482ebbfc | 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 | */ |
ansond | 0:b438482ebbfc | 22 | |
ansond | 0:b438482ebbfc | 23 | #include "InstancePointerTable.h" |
ansond | 0:b438482ebbfc | 24 | |
ansond | 0:b438482ebbfc | 25 | // constructor |
ansond | 0:b438482ebbfc | 26 | InstancePointerTable::InstancePointerTable(const Logger *logger) { |
ansond | 0:b438482ebbfc | 27 | this->m_logger = (Logger *)logger; |
ansond | 0:b438482ebbfc | 28 | } |
ansond | 0:b438482ebbfc | 29 | |
ansond | 0:b438482ebbfc | 30 | // destructor |
ansond | 0:b438482ebbfc | 31 | InstancePointerTable::~InstancePointerTable() { |
ansond | 0:b438482ebbfc | 32 | } |
ansond | 0:b438482ebbfc | 33 | |
ansond | 0:b438482ebbfc | 34 | // add to the table |
ansond | 0:b438482ebbfc | 35 | void InstancePointerTable::add(string key,void *instance) { |
ansond | 0:b438482ebbfc | 36 | // get our index |
ansond | 0:b438482ebbfc | 37 | int index = this->indexFromKey(key); |
ansond | 0:b438482ebbfc | 38 | if (index >= 0) { |
ansond | 0:b438482ebbfc | 39 | // overwrite the existing reference we have... |
ansond | 0:b438482ebbfc | 40 | std::printf("InstancePointerTable: ADD(overwrite) instance: key[%s] index=%d\r\n",key.c_str(),index); |
ansond | 0:b438482ebbfc | 41 | this->m_instance_pointer_table[index].instance = instance; |
ansond | 0:b438482ebbfc | 42 | } |
ansond | 0:b438482ebbfc | 43 | else { |
ansond | 0:b438482ebbfc | 44 | // get the next empty slot |
ansond | 0:b438482ebbfc | 45 | index = this->nextEmptySlot(); |
ansond | 0:b438482ebbfc | 46 | if (index >= 0) { |
ansond | 0:b438482ebbfc | 47 | // set the table entry |
ansond | 0:b438482ebbfc | 48 | this->m_instance_pointer_table[index].key = key; |
ansond | 0:b438482ebbfc | 49 | this->m_instance_pointer_table[index].instance = instance; |
ansond | 0:b438482ebbfc | 50 | std::printf("InstancePointerTable: ADD instance: key[%s] index=%d\r\n",key.c_str(),index); |
ansond | 0:b438482ebbfc | 51 | } |
ansond | 0:b438482ebbfc | 52 | else { |
ansond | 0:b438482ebbfc | 53 | // table is FULL! Error... |
ansond | 0:b438482ebbfc | 54 | std::printf("ERROR: InstancePointerTable is FULL... please expand table\r\n"); |
ansond | 0:b438482ebbfc | 55 | } |
ansond | 0:b438482ebbfc | 56 | } |
ansond | 0:b438482ebbfc | 57 | } |
ansond | 0:b438482ebbfc | 58 | |
ansond | 0:b438482ebbfc | 59 | // get from the table |
ansond | 0:b438482ebbfc | 60 | void *InstancePointerTable::get(string key) { |
ansond | 0:b438482ebbfc | 61 | // get our index |
ansond | 0:b438482ebbfc | 62 | int index = this->indexFromKey(key); |
ansond | 0:b438482ebbfc | 63 | if (index >= 0) { |
ansond | 0:b438482ebbfc | 64 | std::printf("InstancePointerTable: GET instance: key[%s] index=%d\r\n",key.c_str(),index); |
ansond | 0:b438482ebbfc | 65 | return this->m_instance_pointer_table[index].instance; |
ansond | 0:b438482ebbfc | 66 | } |
ansond | 0:b438482ebbfc | 67 | return NULL; |
ansond | 0:b438482ebbfc | 68 | } |
ansond | 0:b438482ebbfc | 69 | |
ansond | 0:b438482ebbfc | 70 | // find next empty slot |
ansond | 0:b438482ebbfc | 71 | int InstancePointerTable::nextEmptySlot() { return this->indexFromKey(""); } |
ansond | 0:b438482ebbfc | 72 | |
ansond | 0:b438482ebbfc | 73 | // lookup into the table |
ansond | 0:b438482ebbfc | 74 | int InstancePointerTable::indexFromKey(string key) { |
ansond | 0:b438482ebbfc | 75 | int index = -1; |
ansond | 0:b438482ebbfc | 76 | bool found = false; |
ansond | 0:b438482ebbfc | 77 | |
ansond | 0:b438482ebbfc | 78 | for(int i=0;i<IPT_MAX_ENTRIES && !found;++i) { |
ansond | 0:b438482ebbfc | 79 | if (this->m_instance_pointer_table[i].key.compare(key) == 0) { |
ansond | 0:b438482ebbfc | 80 | found = true; |
ansond | 0:b438482ebbfc | 81 | index = i; |
ansond | 0:b438482ebbfc | 82 | } |
ansond | 0:b438482ebbfc | 83 | } |
ansond | 0:b438482ebbfc | 84 | |
ansond | 0:b438482ebbfc | 85 | return index; |
ansond | 0:b438482ebbfc | 86 | } |
ansond | 0:b438482ebbfc | 87 | |
ansond | 0:b438482ebbfc | 88 | // initialize |
ansond | 0:b438482ebbfc | 89 | void InstancePointerTable::init() { |
ansond | 0:b438482ebbfc | 90 | for(int i=0;i<IPT_MAX_ENTRIES;++i) { |
ansond | 0:b438482ebbfc | 91 | this->m_instance_pointer_table[i].key = ""; |
ansond | 0:b438482ebbfc | 92 | this->m_instance_pointer_table[i].instance = NULL; |
ansond | 0:b438482ebbfc | 93 | } |
ansond | 0:b438482ebbfc | 94 | } |
ansond | 0:b438482ebbfc | 95 | |
ansond | 0:b438482ebbfc | 96 | // set our logger |
ansond | 0:b438482ebbfc | 97 | void InstancePointerTable::setLogger(const Logger *logger) { this->m_logger = (Logger *)logger; } |
ansond | 0:b438482ebbfc | 98 | |
ansond | 0:b438482ebbfc | 99 | // get our logger |
ansond | 0:b438482ebbfc | 100 | Logger *InstancePointerTable::logger() { return this->m_logger; } |