added max-age and content-format
Fork of mbedConnectorInterface by
api/InstancePointerTable.cpp@52:822611156e35, 2015-04-14 (annotated)
- Committer:
- michaeljkoster
- Date:
- Tue Apr 14 04:00:01 2015 +0000
- Revision:
- 52:822611156e35
- Parent:
- 5:a929d65eb385
added interfaces for max-age and content-format
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 |
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 | this->m_instance_pointer_table[index].instance = instance; |
sam_grove | 2:853f9ecc12df | 44 | } else { |
ansond | 0:b438482ebbfc | 45 | // get the next empty slot |
ansond | 0:b438482ebbfc | 46 | index = this->nextEmptySlot(); |
sam_grove | 2:853f9ecc12df | 47 | if (index >= 0) { |
ansond | 0:b438482ebbfc | 48 | // set the table entry |
ansond | 0:b438482ebbfc | 49 | this->m_instance_pointer_table[index].key = key; |
ansond | 0:b438482ebbfc | 50 | this->m_instance_pointer_table[index].instance = instance; |
sam_grove | 2:853f9ecc12df | 51 | } else { |
ansond | 0:b438482ebbfc | 52 | // table is FULL! Error... |
ansond | 5:a929d65eb385 | 53 | this->logger()->log("ERROR: InstancePointerTable is FULL... please expand table"); |
ansond | 0:b438482ebbfc | 54 | } |
ansond | 0:b438482ebbfc | 55 | } |
sam_grove | 2:853f9ecc12df | 56 | } |
sam_grove | 2:853f9ecc12df | 57 | |
sam_grove | 2:853f9ecc12df | 58 | // get from the table |
sam_grove | 2:853f9ecc12df | 59 | void *InstancePointerTable::get(string key) |
sam_grove | 2:853f9ecc12df | 60 | { |
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 | return this->m_instance_pointer_table[index].instance; |
ansond | 0:b438482ebbfc | 65 | } |
ansond | 0:b438482ebbfc | 66 | return NULL; |
sam_grove | 2:853f9ecc12df | 67 | } |
sam_grove | 2:853f9ecc12df | 68 | |
sam_grove | 2:853f9ecc12df | 69 | // find next empty slot |
sam_grove | 2:853f9ecc12df | 70 | int InstancePointerTable::nextEmptySlot() |
sam_grove | 2:853f9ecc12df | 71 | { |
sam_grove | 2:853f9ecc12df | 72 | return this->indexFromKey(""); |
sam_grove | 2:853f9ecc12df | 73 | } |
sam_grove | 2:853f9ecc12df | 74 | |
sam_grove | 2:853f9ecc12df | 75 | // lookup into the table |
sam_grove | 2:853f9ecc12df | 76 | int InstancePointerTable::indexFromKey(string key) |
sam_grove | 2:853f9ecc12df | 77 | { |
sam_grove | 2:853f9ecc12df | 78 | int index = -1; |
sam_grove | 2:853f9ecc12df | 79 | bool found = false; |
sam_grove | 2:853f9ecc12df | 80 | |
sam_grove | 2:853f9ecc12df | 81 | for(int i=0; i<IPT_MAX_ENTRIES && !found; ++i) { |
ansond | 0:b438482ebbfc | 82 | if (this->m_instance_pointer_table[i].key.compare(key) == 0) { |
ansond | 0:b438482ebbfc | 83 | found = true; |
ansond | 0:b438482ebbfc | 84 | index = i; |
ansond | 0:b438482ebbfc | 85 | } |
sam_grove | 2:853f9ecc12df | 86 | } |
sam_grove | 2:853f9ecc12df | 87 | |
sam_grove | 2:853f9ecc12df | 88 | return index; |
sam_grove | 2:853f9ecc12df | 89 | } |
sam_grove | 2:853f9ecc12df | 90 | |
sam_grove | 2:853f9ecc12df | 91 | // initialize |
sam_grove | 2:853f9ecc12df | 92 | void InstancePointerTable::init() |
sam_grove | 2:853f9ecc12df | 93 | { |
sam_grove | 2:853f9ecc12df | 94 | for(int i=0; i<IPT_MAX_ENTRIES; ++i) { |
sam_grove | 2:853f9ecc12df | 95 | this->m_instance_pointer_table[i].key = ""; |
sam_grove | 2:853f9ecc12df | 96 | this->m_instance_pointer_table[i].instance = NULL; |
sam_grove | 2:853f9ecc12df | 97 | } |
sam_grove | 2:853f9ecc12df | 98 | } |
sam_grove | 2:853f9ecc12df | 99 | |
sam_grove | 2:853f9ecc12df | 100 | // set our logger |
sam_grove | 2:853f9ecc12df | 101 | void InstancePointerTable::setLogger(const Logger *logger) |
sam_grove | 2:853f9ecc12df | 102 | { |
sam_grove | 2:853f9ecc12df | 103 | this->m_logger = (Logger *)logger; |
sam_grove | 2:853f9ecc12df | 104 | } |
sam_grove | 2:853f9ecc12df | 105 | |
sam_grove | 2:853f9ecc12df | 106 | // get our logger |
sam_grove | 2:853f9ecc12df | 107 | Logger *InstancePointerTable::logger() |
sam_grove | 2:853f9ecc12df | 108 | { |
sam_grove | 2:853f9ecc12df | 109 | return this->m_logger; |
sam_grove | 2:853f9ecc12df | 110 | } |