COIN-OR::LEMON - Graph Library

source: glemon/map_value.cc @ 4:244d8c60b997

Last change on this file since 4:244d8c60b997 was 1:67188bd752db, checked in by Peter Hegyi <hegyi@…>, 16 years ago

SVN revision 3500 made compilable with Lemon 1.0.

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