Midnight Cow / ThingerIO

Dependencies:   DHT WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thinger_map.hpp Source File

thinger_map.hpp

00001 // The MIT License (MIT)
00002 //
00003 // Copyright (c) 2015 THINGER LTD
00004 // Author: alvarolb@gmail.com (Alvaro Luis Bustamante)
00005 //
00006 // Permission is hereby granted, free of charge, to any person obtaining a copy
00007 // of this software and associated documentation files (the "Software"), to deal
00008 // in the Software without restriction, including without limitation the rights
00009 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 // copies of the Software, and to permit persons to whom the Software is
00011 // furnished to do so, subject to the following conditions:
00012 //
00013 // The above copyright notice and this permission notice shall be included in
00014 // all copies or substantial portions of the Software.
00015 //
00016 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022 // THE SOFTWARE.
00023 
00024 #ifndef THINGER_MAP_H
00025 #define THINGER_MAP_H
00026 
00027 #include <string.h>
00028 
00029 template <class T>
00030 class thinger_map {
00031 
00032 public:
00033     thinger_map() : head_(NULL), last_(NULL) {
00034 
00035     }
00036 
00037     virtual ~thinger_map() {
00038     }
00039 
00040 public:
00041 
00042     struct entry {
00043         entry(const char* key) : key_(key), next_(NULL){
00044 
00045         }
00046 
00047         const char* key_;
00048         struct entry * next_;
00049         T value_;
00050     };
00051 
00052 private:
00053 
00054     entry * head_;
00055     entry * last_;
00056 
00057 public:
00058 
00059     T& operator[](const char* key){
00060         entry * current = head_;
00061         while(current != NULL){
00062             if(strcmp(key, current->key_)==0){
00063                 return current->value_;
00064             }
00065             current = current->next_;
00066         }
00067         // TODO replace with memory allocator for allowing static memory/dynamic memory
00068         current = new entry(key);
00069 
00070         if(head_==NULL) head_ = current;
00071         if(last_!=NULL) last_->next_ = current;
00072         last_ = current;
00073         return current->value_;
00074     }
00075 
00076     entry* begin(){
00077         return head_;
00078     }
00079 
00080     entry* end(){
00081         return last_;
00082     }
00083 
00084     bool empty()
00085     {
00086         return head_ == last_;
00087     }
00088 
00089     T* find(const char* key)
00090     {
00091         if(key==NULL) return NULL;
00092         entry * current = head_;
00093         while(current != NULL){
00094             if(strcmp(key, current->key_)==0){
00095                 return &current->value_;
00096             }
00097             current = current->next_;
00098         }
00099         return NULL;
00100     }
00101 };
00102 
00103 #endif