Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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_