Data.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  TalkingHead - A talking head for robots
00003  *  Copyright (C) 2012 AG Aktives Sehen <agas@uni-koblenz.de>
00004  *                     Universitaet Koblenz-Landau
00005  *
00006  *  This program is free software: you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019  *  MA 02110-1301  USA or see <http://www.gnu.org/licenses/>.
00020  *******************************************************************************/
00021 
00022 #ifndef TALKING_HEAD_INCLUDE_DATA_H_
00023 #define TALKING_HEAD_INCLUDE_DATA_H_
00024 
00025 #include <fstream>
00026 #include <iostream>
00027 #include <map>
00028 #include <sstream>
00029 #include <stdexcept>
00030 #include <string>
00031 #include <vector>
00032 
00039 class Data
00040 {
00041 public:
00042 
00044     explicit Data(const std::string& data = "") : data_(data) {}
00045 
00047     ~Data(){}
00048 
00050     operator int() const
00051     {
00052         std::stringstream ss(data_);
00053         int val = 0;
00054         ss >> val;
00055 
00056         if (ss.fail())
00057         {
00058             throw std::runtime_error("Cannot convert to int.");
00059         }
00060         return val;
00061     }
00062 
00064     operator const char*() const
00065     {
00066         return data_.c_str();
00067     }
00068 
00070     operator std::string() const
00071     {
00072         return data_;
00073     }
00074 
00076     operator std::vector<float>() const
00077     {
00078         std::string str1 = "";
00079         std::string str2 = "";
00080         std::string str3 = "";
00081 
00082         size_t sep = data_.find_first_of(",");
00083         size_t sep1 = data_.find_last_of(",");
00084 
00085 
00086         if (sep != std::string::npos)
00087         {
00088             str1 = data_.substr(0, sep);
00089             str2 = data_.substr(sep + 1, sep1 - 1);
00090             str3 = data_.substr(sep1 + 1, data_.length() - 1);
00091         }
00092 
00093         std::stringstream ss1(str1);
00094         float val1 = 0.0;
00095         ss1 >> val1;
00096         std::stringstream ss2(str2);
00097         float val2 = 0.0;
00098         ss2 >> val2;
00099         std::stringstream ss3(str3);
00100         float val3 = 0.0;
00101         ss3 >> val3;
00102 
00103         if (ss1.fail() || ss2.fail() || ss3.fail())
00104         {
00105             throw std::runtime_error("Cannot convert to float.");
00106         }
00107         float values[] = {val1, val2, val3};
00108         std::vector<float> vec(values, values + sizeof(values) / sizeof(float));
00109 
00110         return vec;
00111     }
00112 
00114     operator bool() const
00115     {
00116         return data_ == "true" || data_ == "True" || data_ == "TRUE";
00117     }
00118 
00119 private:
00120 
00121    std::string data_;
00122 
00123 };
00124 
00125 #endif // TALKING_HEAD_INCLUDE_DATA_H_


robot_face
Author(s): AGAS, Julian Giesen, David Gossow
autogenerated on Mon Oct 6 2014 04:10:26