map_value.cc
author Peter Hegyi <hegyi@tmit.bme.hu>
Mon, 07 Jul 2008 08:10:39 -0500
changeset 1 67188bd752db
permissions -rw-r--r--
SVN revision 3500 made compilable with Lemon 1.0.
hegyi@1
     1
#include "map_value.h"
hegyi@1
     2
#include <sstream>
hegyi@1
     3
#include <iostream>
hegyi@1
     4
hegyi@1
     5
MapValue::MapValue()
hegyi@1
     6
{
hegyi@1
     7
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
     8
  has_value = false;
hegyi@1
     9
}
hegyi@1
    10
hegyi@1
    11
MapValue::MapValue(double d)
hegyi@1
    12
{
hegyi@1
    13
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    14
  p_value = new double(d);
hegyi@1
    15
  type = NUMERIC;
hegyi@1
    16
  has_value = true;
hegyi@1
    17
}
hegyi@1
    18
hegyi@1
    19
MapValue::MapValue(std::string str)
hegyi@1
    20
{
hegyi@1
    21
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    22
  p_value = new std::string(str);
hegyi@1
    23
  type = STRING;
hegyi@1
    24
  has_value = true;
hegyi@1
    25
}
hegyi@1
    26
hegyi@1
    27
MapValue::MapValue(const char* str)
hegyi@1
    28
{
hegyi@1
    29
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    30
  p_value = new std::string(str);
hegyi@1
    31
  type = STRING;
hegyi@1
    32
  has_value = true;
hegyi@1
    33
}
hegyi@1
    34
hegyi@1
    35
MapValue::operator double() const
hegyi@1
    36
{
hegyi@1
    37
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    38
  if (!has_value) throw IllegalOperation();
hegyi@1
    39
  if (type == NUMERIC)
hegyi@1
    40
    return *(static_cast<double*>(p_value));
hegyi@1
    41
  else
hegyi@1
    42
    throw IllegalOperation();
hegyi@1
    43
}
hegyi@1
    44
hegyi@1
    45
MapValue::operator std::string() const
hegyi@1
    46
{
hegyi@1
    47
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    48
  if (!has_value) throw IllegalOperation();
hegyi@1
    49
  std::string ret;
hegyi@1
    50
  switch (type)
hegyi@1
    51
  {
hegyi@1
    52
    case NUMERIC:
hegyi@1
    53
      {
hegyi@1
    54
        double d = *(static_cast<double*>(p_value));
hegyi@1
    55
        std::ostringstream ostr;
hegyi@1
    56
        ostr << d;
hegyi@1
    57
        ret = ostr.str();
hegyi@1
    58
      }
hegyi@1
    59
      break;
hegyi@1
    60
    case STRING:
hegyi@1
    61
      ret = *(static_cast<std::string*>(p_value));
hegyi@1
    62
      break;
hegyi@1
    63
  }
hegyi@1
    64
  return ret;
hegyi@1
    65
}
hegyi@1
    66
hegyi@1
    67
MapValue::MapValue(const MapValue& v)
hegyi@1
    68
{
hegyi@1
    69
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    70
  if (!v.has_value) throw IllegalOperation();
hegyi@1
    71
  has_value = true;
hegyi@1
    72
  type = v.type;
hegyi@1
    73
  switch (v.type)
hegyi@1
    74
  {
hegyi@1
    75
    case NUMERIC:
hegyi@1
    76
      p_value = new double(*(static_cast<double*>(v.p_value)));
hegyi@1
    77
      break;
hegyi@1
    78
    case STRING:
hegyi@1
    79
      p_value = new std::string(*(static_cast<std::string*>(v.p_value)));
hegyi@1
    80
      break;
hegyi@1
    81
  }
hegyi@1
    82
}
hegyi@1
    83
hegyi@1
    84
MapValue& MapValue::operator=(const MapValue& v)
hegyi@1
    85
{
hegyi@1
    86
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
    87
  if (&v != this)
hegyi@1
    88
  {
hegyi@1
    89
    if (!v.has_value) throw IllegalOperation();
hegyi@1
    90
    clear();
hegyi@1
    91
    has_value = true;
hegyi@1
    92
    type = v.type;
hegyi@1
    93
    switch (v.type)
hegyi@1
    94
    {
hegyi@1
    95
      case NUMERIC:
hegyi@1
    96
        p_value = new double(*(static_cast<double*>(v.p_value)));
hegyi@1
    97
        break;
hegyi@1
    98
      case STRING:
hegyi@1
    99
        p_value = new std::string(*(static_cast<std::string*>(v.p_value)));
hegyi@1
   100
        break;
hegyi@1
   101
    }
hegyi@1
   102
  }
hegyi@1
   103
  return *this;
hegyi@1
   104
}
hegyi@1
   105
hegyi@1
   106
MapValue::~MapValue()
hegyi@1
   107
{
hegyi@1
   108
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
   109
  clear();
hegyi@1
   110
}
hegyi@1
   111
hegyi@1
   112
void MapValue::clear()
hegyi@1
   113
{
hegyi@1
   114
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
   115
  if (!has_value) return;
hegyi@1
   116
  switch (type)
hegyi@1
   117
  {
hegyi@1
   118
    case NUMERIC:
hegyi@1
   119
      delete static_cast<double*>(p_value);
hegyi@1
   120
      break;
hegyi@1
   121
    case STRING:
hegyi@1
   122
      delete static_cast<std::string*>(p_value);
hegyi@1
   123
      break;
hegyi@1
   124
  }
hegyi@1
   125
}
hegyi@1
   126
hegyi@1
   127
MapValue::Type MapValue::getType() const
hegyi@1
   128
{
hegyi@1
   129
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
   130
  return type;
hegyi@1
   131
}
hegyi@1
   132
hegyi@1
   133
bool MapValue::hasValue() const
hegyi@1
   134
{
hegyi@1
   135
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
   136
  return has_value;
hegyi@1
   137
}
hegyi@1
   138
hegyi@1
   139
std::ostream& operator<<(std::ostream &os, const MapValue& v)
hegyi@1
   140
{
hegyi@1
   141
  //std::cout << __PRETTY_FUNCTION__ << std::endl;
hegyi@1
   142
  if (!v.has_value) return os;
hegyi@1
   143
  switch (v.type)
hegyi@1
   144
  {
hegyi@1
   145
    case MapValue::NUMERIC:
hegyi@1
   146
      os << *(static_cast<double*>(v.p_value));
hegyi@1
   147
      break;
hegyi@1
   148
    case MapValue::STRING:
hegyi@1
   149
      os << *(static_cast<std::string*>(v.p_value));
hegyi@1
   150
      break;
hegyi@1
   151
  }
hegyi@1
   152
  return os;
hegyi@1
   153
}