Dependents:   communication

Files at this revision

API Documentation at this revision

Comitter:
inst
Date:
Thu Mar 31 04:42:55 2016 +0000
Commit message:

Changed in this revision

SimpleMapDeSerializer.hpp Show annotated file Show diff for this revision Revisions of this file
SimpleMapSerializer.hpp Show annotated file Show diff for this revision Revisions of this file
StringToValue.hpp Show annotated file Show diff for this revision Revisions of this file
ValueToString.hpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f398994b70f5 SimpleMapDeSerializer.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleMapDeSerializer.hpp	Thu Mar 31 04:42:55 2016 +0000
@@ -0,0 +1,27 @@
+#ifndef SIMPLE_MAP_DESERIALIZER_HPP
+#define SIMPLE_MAP_DESERIALIZER_HPP
+
+#include <string>
+#include <map>
+#include <deque>
+#include "StringToValue.hpp"
+
+namespace SimpleMapSerialization{
+template <class T,class U>
+void simpleMapDeSerializer(const std::string &str, char delim,std::map<T,U>& result){
+    std::deque<std::string> res;
+    size_t current = 0, found;
+    while((found = str.find_first_of(delim, current)) != std::string::npos){
+        res.push_back(std::string(str, current, found - current));
+        current = found + 1;
+    }
+    res.push_back(std::string(str, current, str.size() - current));
+    for (std::deque<std::string>::iterator it = res.begin(); it != res.end(); it+=2){
+        T key = ConvertData::stringToValue<T>(*it);
+        U value = ConvertData::stringToValue<U>(*(it+1));
+        result[key] = value;
+    }
+}
+}
+
+#endif //SIMPLE_MAP_DESERIALIZER_HPP
\ No newline at end of file
diff -r 000000000000 -r f398994b70f5 SimpleMapSerializer.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleMapSerializer.hpp	Thu Mar 31 04:42:55 2016 +0000
@@ -0,0 +1,25 @@
+#ifndef SIMPLE_MAP_SERIALIZER_HPP
+#define SIMPLE_MAP_SERIALIZER_HPP
+
+#include <string>
+#include <map>
+#include "ValueToString.hpp"
+
+namespace SimpleMapSerialization{
+template <class T,class U>
+std::string simpleMapSerializer(char delim,const std::map<T,U>& map)
+{
+    std::string s;
+    for (typename std::map<T,U>::iterator mapIte = map.begin(); mapIte != map.end(); mapIte++) {
+        T key = mapIte->first;
+        U val = mapIte->second;
+        std::string sKey = ConvertData::ValueToString(key) + delim;
+        std::string sVal = ConvertData::ValueToString(val) + delim;
+        s += sKey + sVal;
+    }
+    s.resize(s.size() - 1);
+    return s;
+}
+}
+
+#endif //SIMPLE_MAP_SERIALIZER_HPP
\ No newline at end of file
diff -r 000000000000 -r f398994b70f5 StringToValue.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StringToValue.hpp	Thu Mar 31 04:42:55 2016 +0000
@@ -0,0 +1,35 @@
+#ifndef STRING_TO_VALUE_HPP
+#define STRING_TO_VALUE_HPP
+
+#include <string>
+
+namespace ConvertData{
+template <class T>
+T stringToValue(std::string str)
+{
+    T result;
+    return result;
+}
+template<>
+std::string stringToValue(std::string str)
+{
+    return str;
+}
+
+template<>
+int stringToValue(std::string str)
+{
+    return atoi(str.c_str());
+}
+long convert(std::string str)
+{
+    return atol(str.c_str());
+}
+template<>
+float stringToValue(std::string str)
+{
+    return atof(str.c_str());
+}
+}
+
+#endif //STRING_TO_VALUE_HPP
\ No newline at end of file
diff -r 000000000000 -r f398994b70f5 ValueToString.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ValueToString.hpp	Thu Mar 31 04:42:55 2016 +0000
@@ -0,0 +1,55 @@
+#ifndef VALUE_TO_STRING_HPP
+#define VALUE_TO_STRING_HPP
+
+#include <string>
+
+namespace ConvertData{
+template <class T>
+std::string ValueToString(T t){
+    
+}
+
+template<>
+std::string ValueToString(int i){
+    char ch[16] = {0};
+    std::string str;
+    sprintf(ch,"%d",i);
+    str = ch;
+    return str;
+}
+
+
+template<>
+std::string ValueToString(long l){
+    char ch[16] = {0};
+    std::string str;
+    sprintf(ch,"%ld",l);
+    str = ch;
+    return str;
+}
+
+template<>
+std::string ValueToString(float f){
+    char ch[16] = {0};
+    std::string str;
+    sprintf(ch,"%f",f);
+    str = ch;
+    return str;
+}
+
+template<>
+std::string ValueToString(double d){
+    char ch[16] = {0};
+    std::string str;
+    sprintf(ch,"%lf",d);
+    str = ch;
+    return str;
+}
+
+template<>
+std::string ValueToString(std::string str){
+    return str;
+}
+}
+
+#endif //VALUE_TO_STRING_HPP
\ No newline at end of file