hegyi@1: #include "map_value.h" hegyi@1: #include hegyi@1: #include hegyi@1: hegyi@1: MapValue::MapValue() hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: has_value = false; hegyi@1: } hegyi@1: hegyi@1: MapValue::MapValue(double d) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: p_value = new double(d); hegyi@1: type = NUMERIC; hegyi@1: has_value = true; hegyi@1: } hegyi@1: hegyi@1: MapValue::MapValue(std::string str) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: p_value = new std::string(str); hegyi@1: type = STRING; hegyi@1: has_value = true; hegyi@1: } hegyi@1: hegyi@1: MapValue::MapValue(const char* str) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: p_value = new std::string(str); hegyi@1: type = STRING; hegyi@1: has_value = true; hegyi@1: } hegyi@1: hegyi@1: MapValue::operator double() const hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (!has_value) throw IllegalOperation(); hegyi@1: if (type == NUMERIC) hegyi@1: return *(static_cast(p_value)); hegyi@1: else hegyi@1: throw IllegalOperation(); hegyi@1: } hegyi@1: hegyi@1: MapValue::operator std::string() const hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (!has_value) throw IllegalOperation(); hegyi@1: std::string ret; hegyi@1: switch (type) hegyi@1: { hegyi@1: case NUMERIC: hegyi@1: { hegyi@1: double d = *(static_cast(p_value)); hegyi@1: std::ostringstream ostr; hegyi@1: ostr << d; hegyi@1: ret = ostr.str(); hegyi@1: } hegyi@1: break; hegyi@1: case STRING: hegyi@1: ret = *(static_cast(p_value)); hegyi@1: break; hegyi@1: } hegyi@1: return ret; hegyi@1: } hegyi@1: hegyi@1: MapValue::MapValue(const MapValue& v) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (!v.has_value) throw IllegalOperation(); hegyi@1: has_value = true; hegyi@1: type = v.type; hegyi@1: switch (v.type) hegyi@1: { hegyi@1: case NUMERIC: hegyi@1: p_value = new double(*(static_cast(v.p_value))); hegyi@1: break; hegyi@1: case STRING: hegyi@1: p_value = new std::string(*(static_cast(v.p_value))); hegyi@1: break; hegyi@1: } hegyi@1: } hegyi@1: hegyi@1: MapValue& MapValue::operator=(const MapValue& v) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (&v != this) hegyi@1: { hegyi@1: if (!v.has_value) throw IllegalOperation(); hegyi@1: clear(); hegyi@1: has_value = true; hegyi@1: type = v.type; hegyi@1: switch (v.type) hegyi@1: { hegyi@1: case NUMERIC: hegyi@1: p_value = new double(*(static_cast(v.p_value))); hegyi@1: break; hegyi@1: case STRING: hegyi@1: p_value = new std::string(*(static_cast(v.p_value))); hegyi@1: break; hegyi@1: } hegyi@1: } hegyi@1: return *this; hegyi@1: } hegyi@1: hegyi@1: MapValue::~MapValue() hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: clear(); hegyi@1: } hegyi@1: hegyi@1: void MapValue::clear() hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (!has_value) return; hegyi@1: switch (type) hegyi@1: { hegyi@1: case NUMERIC: hegyi@1: delete static_cast(p_value); hegyi@1: break; hegyi@1: case STRING: hegyi@1: delete static_cast(p_value); hegyi@1: break; hegyi@1: } hegyi@1: } hegyi@1: hegyi@1: MapValue::Type MapValue::getType() const hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: return type; hegyi@1: } hegyi@1: hegyi@1: bool MapValue::hasValue() const hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: return has_value; hegyi@1: } hegyi@1: hegyi@1: std::ostream& operator<<(std::ostream &os, const MapValue& v) hegyi@1: { hegyi@1: //std::cout << __PRETTY_FUNCTION__ << std::endl; hegyi@1: if (!v.has_value) return os; hegyi@1: switch (v.type) hegyi@1: { hegyi@1: case MapValue::NUMERIC: hegyi@1: os << *(static_cast(v.p_value)); hegyi@1: break; hegyi@1: case MapValue::STRING: hegyi@1: os << *(static_cast(v.p_value)); hegyi@1: break; hegyi@1: } hegyi@1: return os; hegyi@1: }