| 1 | /****************************************************************************** |
| 2 | * Arachnoid Graphics Plugin for Mupen64Plus |
| 3 | * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/ |
| 4 | * |
| 5 | * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | *****************************************************************************/ |
| 21 | |
| 22 | #ifndef STRING_VALUE_H_ |
| 23 | #define STRING_VALUE_H_ |
| 24 | #include <cstdlib> |
| 25 | #include <string> |
| 26 | #include <sstream> |
| 27 | |
| 28 | //! Represents numeric value(s) with a string using conversion operators |
| 29 | struct StringValue |
| 30 | { |
| 31 | StringValue() {data = "";} |
| 32 | StringValue(const StringValue& v) { data=v.data; } |
| 33 | StringValue(const std::string& s) { data=s; } |
| 34 | StringValue(const char *s) { data = s; } |
| 35 | |
| 36 | StringValue(bool b) |
| 37 | { |
| 38 | std::stringstream s; |
| 39 | s << b; |
| 40 | data= s.str(); |
| 41 | } |
| 42 | |
| 43 | StringValue(int i) { |
| 44 | std::stringstream s; |
| 45 | s << i; |
| 46 | data= s.str(); |
| 47 | } |
| 48 | |
| 49 | StringValue(float f) { |
| 50 | std::stringstream s; |
| 51 | s << f; |
| 52 | data= s.str(); |
| 53 | } |
| 54 | |
| 55 | StringValue(double f) { |
| 56 | std::stringstream s; |
| 57 | s << f; |
| 58 | data= s.str(); |
| 59 | } |
| 60 | |
| 61 | // assignment operators |
| 62 | StringValue& operator=(const char* s) { data=s; return *this; } |
| 63 | StringValue& operator=(const std::string& s) { data=s; return *this; } |
| 64 | StringValue& operator=(const StringValue& v) { data=v.data; return *this; } |
| 65 | template<class T> StringValue& operator=(T x) |
| 66 | { |
| 67 | std::string y; |
| 68 | std::stringstream ss; |
| 69 | ss << x; |
| 70 | ss >> y; |
| 71 | data = y; |
| 72 | return *this; |
| 73 | } |
| 74 | // comparison operators |
| 75 | bool operator==(const char* s) const { return data==s; } |
| 76 | bool operator==(const std::string& s) const { return data==s; } |
| 77 | bool operator==(const StringValue& x) const { return data==x.data; } |
| 78 | template<class T> bool operator==(T x) const { return (x == T(*this)); } |
| 79 | // conversion operators |
| 80 | operator bool() const { |
| 81 | if (data=="false" || data=="0") return false; |
| 82 | return true; |
| 83 | } |
| 84 | operator float() const { |
| 85 | return (float) atof(data.c_str()); |
| 86 | } |
| 87 | operator double() const { |
| 88 | return atof(data.c_str()); |
| 89 | } |
| 90 | operator char() const { |
| 91 | char x; |
| 92 | std::stringstream ss; ss << data; ss >> x; |
| 93 | return x; |
| 94 | } |
| 95 | operator unsigned char() const { |
| 96 | unsigned char x; |
| 97 | std::stringstream ss; ss << data; ss >> x; |
| 98 | return x; |
| 99 | } |
| 100 | operator int() const { |
| 101 | return atoi(data.c_str()); |
| 102 | } |
| 103 | operator short() const { |
| 104 | return (short) atoi(data.c_str()); |
| 105 | } |
| 106 | operator long() const { |
| 107 | return atol(data.c_str()); |
| 108 | } |
| 109 | operator unsigned short() const { |
| 110 | unsigned short x; |
| 111 | std::stringstream ss; ss << data; ss >> x; |
| 112 | return x; |
| 113 | } |
| 114 | operator unsigned int() const { |
| 115 | unsigned int x; |
| 116 | std::stringstream ss; ss << data; ss >> x; |
| 117 | return x; |
| 118 | } |
| 119 | operator unsigned long() const { |
| 120 | unsigned int x; |
| 121 | std::stringstream ss; ss << data; ss >> x; |
| 122 | return x; |
| 123 | } |
| 124 | operator const char *() const { |
| 125 | return data.c_str(); |
| 126 | } |
| 127 | |
| 128 | std::string str() const { return data; } |
| 129 | private: |
| 130 | std::string data; |
| 131 | }; |
| 132 | |
| 133 | #endif |