map_value.cc
changeset 1 67188bd752db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/map_value.cc	Mon Jul 07 08:10:39 2008 -0500
     1.3 @@ -0,0 +1,153 @@
     1.4 +#include "map_value.h"
     1.5 +#include <sstream>
     1.6 +#include <iostream>
     1.7 +
     1.8 +MapValue::MapValue()
     1.9 +{
    1.10 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.11 +  has_value = false;
    1.12 +}
    1.13 +
    1.14 +MapValue::MapValue(double d)
    1.15 +{
    1.16 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.17 +  p_value = new double(d);
    1.18 +  type = NUMERIC;
    1.19 +  has_value = true;
    1.20 +}
    1.21 +
    1.22 +MapValue::MapValue(std::string str)
    1.23 +{
    1.24 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.25 +  p_value = new std::string(str);
    1.26 +  type = STRING;
    1.27 +  has_value = true;
    1.28 +}
    1.29 +
    1.30 +MapValue::MapValue(const char* str)
    1.31 +{
    1.32 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.33 +  p_value = new std::string(str);
    1.34 +  type = STRING;
    1.35 +  has_value = true;
    1.36 +}
    1.37 +
    1.38 +MapValue::operator double() const
    1.39 +{
    1.40 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.41 +  if (!has_value) throw IllegalOperation();
    1.42 +  if (type == NUMERIC)
    1.43 +    return *(static_cast<double*>(p_value));
    1.44 +  else
    1.45 +    throw IllegalOperation();
    1.46 +}
    1.47 +
    1.48 +MapValue::operator std::string() const
    1.49 +{
    1.50 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.51 +  if (!has_value) throw IllegalOperation();
    1.52 +  std::string ret;
    1.53 +  switch (type)
    1.54 +  {
    1.55 +    case NUMERIC:
    1.56 +      {
    1.57 +        double d = *(static_cast<double*>(p_value));
    1.58 +        std::ostringstream ostr;
    1.59 +        ostr << d;
    1.60 +        ret = ostr.str();
    1.61 +      }
    1.62 +      break;
    1.63 +    case STRING:
    1.64 +      ret = *(static_cast<std::string*>(p_value));
    1.65 +      break;
    1.66 +  }
    1.67 +  return ret;
    1.68 +}
    1.69 +
    1.70 +MapValue::MapValue(const MapValue& v)
    1.71 +{
    1.72 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.73 +  if (!v.has_value) throw IllegalOperation();
    1.74 +  has_value = true;
    1.75 +  type = v.type;
    1.76 +  switch (v.type)
    1.77 +  {
    1.78 +    case NUMERIC:
    1.79 +      p_value = new double(*(static_cast<double*>(v.p_value)));
    1.80 +      break;
    1.81 +    case STRING:
    1.82 +      p_value = new std::string(*(static_cast<std::string*>(v.p_value)));
    1.83 +      break;
    1.84 +  }
    1.85 +}
    1.86 +
    1.87 +MapValue& MapValue::operator=(const MapValue& v)
    1.88 +{
    1.89 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
    1.90 +  if (&v != this)
    1.91 +  {
    1.92 +    if (!v.has_value) throw IllegalOperation();
    1.93 +    clear();
    1.94 +    has_value = true;
    1.95 +    type = v.type;
    1.96 +    switch (v.type)
    1.97 +    {
    1.98 +      case NUMERIC:
    1.99 +        p_value = new double(*(static_cast<double*>(v.p_value)));
   1.100 +        break;
   1.101 +      case STRING:
   1.102 +        p_value = new std::string(*(static_cast<std::string*>(v.p_value)));
   1.103 +        break;
   1.104 +    }
   1.105 +  }
   1.106 +  return *this;
   1.107 +}
   1.108 +
   1.109 +MapValue::~MapValue()
   1.110 +{
   1.111 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
   1.112 +  clear();
   1.113 +}
   1.114 +
   1.115 +void MapValue::clear()
   1.116 +{
   1.117 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
   1.118 +  if (!has_value) return;
   1.119 +  switch (type)
   1.120 +  {
   1.121 +    case NUMERIC:
   1.122 +      delete static_cast<double*>(p_value);
   1.123 +      break;
   1.124 +    case STRING:
   1.125 +      delete static_cast<std::string*>(p_value);
   1.126 +      break;
   1.127 +  }
   1.128 +}
   1.129 +
   1.130 +MapValue::Type MapValue::getType() const
   1.131 +{
   1.132 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
   1.133 +  return type;
   1.134 +}
   1.135 +
   1.136 +bool MapValue::hasValue() const
   1.137 +{
   1.138 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
   1.139 +  return has_value;
   1.140 +}
   1.141 +
   1.142 +std::ostream& operator<<(std::ostream &os, const MapValue& v)
   1.143 +{
   1.144 +  //std::cout << __PRETTY_FUNCTION__ << std::endl;
   1.145 +  if (!v.has_value) return os;
   1.146 +  switch (v.type)
   1.147 +  {
   1.148 +    case MapValue::NUMERIC:
   1.149 +      os << *(static_cast<double*>(v.p_value));
   1.150 +      break;
   1.151 +    case MapValue::STRING:
   1.152 +      os << *(static_cast<std::string*>(v.p_value));
   1.153 +      break;
   1.154 +  }
   1.155 +  return os;
   1.156 +}