Thinger.io Client Library for ARM mbed platform. This is a generic library that provides a base class that can be used to other develop hardware specific libraries.

Fork of ThingerClient by Alvaro Luis Bustamante

Committer:
alvarolb
Date:
Sat Dec 26 13:18:01 2015 +0000
Revision:
4:de51256455f7
Parent:
0:b75d784c7c1a
Adapter pson to properly work in ARM Mbed old compiler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alvarolb 0:b75d784c7c1a 1 // The MIT License (MIT)
alvarolb 0:b75d784c7c1a 2 //
alvarolb 0:b75d784c7c1a 3 // Copyright (c) 2015 THINGER LTD
alvarolb 0:b75d784c7c1a 4 // Author: alvarolb@gmail.com (Alvaro Luis Bustamante)
alvarolb 0:b75d784c7c1a 5 //
alvarolb 0:b75d784c7c1a 6 // Permission is hereby granted, free of charge, to any person obtaining a copy
alvarolb 0:b75d784c7c1a 7 // of this software and associated documentation files (the "Software"), to deal
alvarolb 0:b75d784c7c1a 8 // in the Software without restriction, including without limitation the rights
alvarolb 0:b75d784c7c1a 9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
alvarolb 0:b75d784c7c1a 10 // copies of the Software, and to permit persons to whom the Software is
alvarolb 0:b75d784c7c1a 11 // furnished to do so, subject to the following conditions:
alvarolb 0:b75d784c7c1a 12 //
alvarolb 0:b75d784c7c1a 13 // The above copyright notice and this permission notice shall be included in
alvarolb 0:b75d784c7c1a 14 // all copies or substantial portions of the Software.
alvarolb 0:b75d784c7c1a 15 //
alvarolb 0:b75d784c7c1a 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
alvarolb 0:b75d784c7c1a 17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
alvarolb 0:b75d784c7c1a 18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
alvarolb 0:b75d784c7c1a 19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
alvarolb 0:b75d784c7c1a 20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
alvarolb 0:b75d784c7c1a 21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
alvarolb 0:b75d784c7c1a 22 // THE SOFTWARE.
alvarolb 0:b75d784c7c1a 23
alvarolb 0:b75d784c7c1a 24 #ifndef THINGER_MAP_H
alvarolb 0:b75d784c7c1a 25 #define THINGER_MAP_H
alvarolb 0:b75d784c7c1a 26
alvarolb 0:b75d784c7c1a 27 #include <string.h>
alvarolb 0:b75d784c7c1a 28
alvarolb 0:b75d784c7c1a 29 template <class T>
alvarolb 0:b75d784c7c1a 30 class thinger_map {
alvarolb 0:b75d784c7c1a 31
alvarolb 0:b75d784c7c1a 32 public:
alvarolb 0:b75d784c7c1a 33 thinger_map() : head_(NULL), last_(NULL) {
alvarolb 0:b75d784c7c1a 34
alvarolb 0:b75d784c7c1a 35 }
alvarolb 0:b75d784c7c1a 36
alvarolb 0:b75d784c7c1a 37 virtual ~thinger_map() {
alvarolb 0:b75d784c7c1a 38 }
alvarolb 0:b75d784c7c1a 39
alvarolb 0:b75d784c7c1a 40 public:
alvarolb 0:b75d784c7c1a 41
alvarolb 0:b75d784c7c1a 42 struct entry {
alvarolb 0:b75d784c7c1a 43 entry(const char* key) : key_(key), next_(NULL){
alvarolb 0:b75d784c7c1a 44
alvarolb 0:b75d784c7c1a 45 }
alvarolb 0:b75d784c7c1a 46
alvarolb 0:b75d784c7c1a 47 const char* key_;
alvarolb 0:b75d784c7c1a 48 struct entry * next_;
alvarolb 0:b75d784c7c1a 49 T value_;
alvarolb 0:b75d784c7c1a 50 };
alvarolb 0:b75d784c7c1a 51
alvarolb 0:b75d784c7c1a 52 private:
alvarolb 0:b75d784c7c1a 53
alvarolb 0:b75d784c7c1a 54 entry * head_;
alvarolb 0:b75d784c7c1a 55 entry * last_;
alvarolb 0:b75d784c7c1a 56
alvarolb 0:b75d784c7c1a 57 public:
alvarolb 0:b75d784c7c1a 58
alvarolb 0:b75d784c7c1a 59 T& operator[](const char* key){
alvarolb 0:b75d784c7c1a 60 entry * current = head_;
alvarolb 0:b75d784c7c1a 61 while(current != NULL){
alvarolb 0:b75d784c7c1a 62 if(strcmp(key, current->key_)==0){
alvarolb 0:b75d784c7c1a 63 return current->value_;
alvarolb 0:b75d784c7c1a 64 }
alvarolb 0:b75d784c7c1a 65 current = current->next_;
alvarolb 0:b75d784c7c1a 66 }
alvarolb 0:b75d784c7c1a 67 // TODO replace with memory allocator for allowing static memory/dynamic memory
alvarolb 0:b75d784c7c1a 68 current = new entry(key);
alvarolb 0:b75d784c7c1a 69
alvarolb 0:b75d784c7c1a 70 if(head_==NULL) head_ = current;
alvarolb 0:b75d784c7c1a 71 if(last_!=NULL) last_->next_ = current;
alvarolb 0:b75d784c7c1a 72 last_ = current;
alvarolb 0:b75d784c7c1a 73 return current->value_;
alvarolb 0:b75d784c7c1a 74 }
alvarolb 0:b75d784c7c1a 75
alvarolb 0:b75d784c7c1a 76 entry* begin(){
alvarolb 0:b75d784c7c1a 77 return head_;
alvarolb 0:b75d784c7c1a 78 }
alvarolb 0:b75d784c7c1a 79
alvarolb 0:b75d784c7c1a 80 entry* end(){
alvarolb 0:b75d784c7c1a 81 return last_;
alvarolb 0:b75d784c7c1a 82 }
alvarolb 0:b75d784c7c1a 83
alvarolb 0:b75d784c7c1a 84 bool empty()
alvarolb 0:b75d784c7c1a 85 {
alvarolb 0:b75d784c7c1a 86 return head_ == last_;
alvarolb 0:b75d784c7c1a 87 }
alvarolb 0:b75d784c7c1a 88
alvarolb 0:b75d784c7c1a 89 T* find(const char* key)
alvarolb 0:b75d784c7c1a 90 {
alvarolb 0:b75d784c7c1a 91 if(key==NULL) return NULL;
alvarolb 0:b75d784c7c1a 92 entry * current = head_;
alvarolb 0:b75d784c7c1a 93 while(current != NULL){
alvarolb 0:b75d784c7c1a 94 if(strcmp(key, current->key_)==0){
alvarolb 0:b75d784c7c1a 95 return &current->value_;
alvarolb 0:b75d784c7c1a 96 }
alvarolb 0:b75d784c7c1a 97 current = current->next_;
alvarolb 0:b75d784c7c1a 98 }
alvarolb 0:b75d784c7c1a 99 return NULL;
alvarolb 0:b75d784c7c1a 100 }
alvarolb 0:b75d784c7c1a 101
alvarolb 0:b75d784c7c1a 102 };
alvarolb 0:b75d784c7c1a 103
alvarolb 0:b75d784c7c1a 104 #endif