observe updates

Fork of mbedConnectorInterface by Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InstancePointerTable.cpp Source File

InstancePointerTable.cpp

00001 /**
00002  * @file    InstancePointerTable.h
00003  * @brief   instance pointer table header
00004  * @author  Doug Anson/Chris Paola
00005  * @version 1.0
00006  * @see
00007  *
00008  * Copyright (c) 2014
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022 
00023 #include "InstancePointerTable.h"
00024 
00025 // constructor
00026 InstancePointerTable::InstancePointerTable(const Logger *logger)
00027 {
00028     this->m_logger = (Logger *)logger;
00029 }
00030 
00031 // destructor
00032 InstancePointerTable::~InstancePointerTable()
00033 {
00034 }
00035 
00036 // add to the table
00037 void InstancePointerTable::add(string key,void *instance)
00038 {
00039     // get our index
00040     int index = this->indexFromKey(key);
00041     if (index >= 0) {
00042         // overwrite the existing reference we have...
00043         this->m_instance_pointer_table[index].instance = instance;
00044     } else {
00045         // get the next empty slot
00046         index = this->nextEmptySlot();
00047         if (index >= 0) {
00048             // set the table entry
00049             this->m_instance_pointer_table[index].key = key;
00050             this->m_instance_pointer_table[index].instance = instance;
00051         } else {
00052             // table is FULL!  Error...
00053             this->logger()->log("ERROR: InstancePointerTable is FULL... please expand table");
00054         }
00055     }
00056 }
00057 
00058 // get from the table
00059 void *InstancePointerTable::get(string key)
00060 {
00061     // get our index
00062     int index = this->indexFromKey(key);
00063     if (index >= 0) {
00064         return this->m_instance_pointer_table[index].instance;
00065     }
00066     return NULL;
00067 }
00068 
00069 // find next empty slot
00070 int InstancePointerTable::nextEmptySlot()
00071 {
00072     return this->indexFromKey("");
00073 }
00074 
00075 // lookup into the table
00076 int InstancePointerTable::indexFromKey(string key)
00077 {
00078     int index = -1;
00079     bool found = false;
00080 
00081     for(int i=0; i<IPT_MAX_ENTRIES && !found; ++i) {
00082         if (this->m_instance_pointer_table[i].key.compare(key) == 0) {
00083             found = true;
00084             index = i;
00085         }
00086     }
00087 
00088     return index;
00089 }
00090 
00091 // initialize
00092 void InstancePointerTable::init()
00093 {
00094     for(int i=0; i<IPT_MAX_ENTRIES; ++i) {
00095         this->m_instance_pointer_table[i].key = "";
00096         this->m_instance_pointer_table[i].instance = NULL;
00097     }
00098 }
00099 
00100 // set our logger
00101 void InstancePointerTable::setLogger(const Logger *logger)
00102 {
00103     this->m_logger = (Logger *)logger;
00104 }
00105 
00106 // get our logger
00107 Logger *InstancePointerTable::logger()
00108 {
00109     return this->m_logger;
00110 }