src/core/enum.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2969 9d05d2a95dfa
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2457
    20
#include "enum.h"
mathieu@2381
    21
#include "fatal-error.h"
mathieu@2381
    22
#include <sstream>
mathieu@2381
    23
mathieu@2381
    24
namespace ns3 {
mathieu@2381
    25
mathieu@2965
    26
EnumValue::EnumValue ()
mathieu@2520
    27
  : m_v ()
mathieu@2520
    28
{}
mathieu@2965
    29
EnumValue::EnumValue (int v)
mathieu@2381
    30
  : m_v (v)
mathieu@2381
    31
{}
mathieu@2381
    32
void 
mathieu@2965
    33
EnumValue::Set (int v)
mathieu@2381
    34
{
mathieu@2381
    35
  m_v = v;
mathieu@2381
    36
}
mathieu@2381
    37
int 
mathieu@2965
    38
EnumValue::Get (void) const
mathieu@2381
    39
{
mathieu@2381
    40
  return m_v;
mathieu@2381
    41
}
mathieu@2965
    42
Ptr<AttributeValue>
mathieu@2965
    43
EnumValue::Copy (void) const
mathieu@2381
    44
{
mathieu@2965
    45
  return ns3::Create<EnumValue> (*this);
mathieu@2381
    46
}
mathieu@2381
    47
std::string 
mathieu@2965
    48
EnumValue::SerializeToString (Ptr<const AttributeChecker> checker) const
mathieu@2381
    49
{
mathieu@2427
    50
  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
mathieu@2381
    51
  NS_ASSERT (p != 0);
mathieu@2427
    52
  for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
mathieu@2381
    53
    {
mathieu@2381
    54
      if (i->first == m_v)
mathieu@2381
    55
	{
mathieu@2381
    56
	  return i->second;
mathieu@2381
    57
	}
mathieu@2381
    58
    }
mathieu@2381
    59
  
mathieu@2440
    60
  NS_FATAL_ERROR ("The user has set an invalid C++ value in this Enum");
mathieu@2381
    61
  // quiet compiler.
mathieu@2381
    62
  return "";
mathieu@2381
    63
}
mathieu@2381
    64
bool 
mathieu@2965
    65
EnumValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
mathieu@2381
    66
{
mathieu@2427
    67
  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
mathieu@2381
    68
  NS_ASSERT (p != 0);
mathieu@2427
    69
  for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
mathieu@2381
    70
    {
mathieu@2381
    71
      if (i->second == value)
mathieu@2381
    72
	{
mathieu@2381
    73
	  m_v = i->first;
mathieu@2381
    74
	  return true;
mathieu@2381
    75
	}
mathieu@2381
    76
    }
mathieu@2381
    77
  return false;
mathieu@2381
    78
}
mathieu@2381
    79
mathieu@2427
    80
EnumChecker::EnumChecker ()
mathieu@2381
    81
{}
mathieu@2381
    82
mathieu@2381
    83
void 
mathieu@2427
    84
EnumChecker::AddDefault (int v, std::string name)
mathieu@2381
    85
{
mathieu@2381
    86
  m_valueSet.push_front (std::make_pair (v, name));
mathieu@2381
    87
}
mathieu@2381
    88
void 
mathieu@2427
    89
EnumChecker::Add (int v, std::string name)
mathieu@2381
    90
{
mathieu@2381
    91
  m_valueSet.push_back (std::make_pair (v, name));
mathieu@2381
    92
}
mathieu@2381
    93
bool 
mathieu@2965
    94
EnumChecker::Check (const AttributeValue &value) const
mathieu@2381
    95
{
mathieu@2965
    96
  const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
mathieu@2381
    97
  if (p == 0)
mathieu@2381
    98
    {
mathieu@2381
    99
      return false;
mathieu@2381
   100
    }
mathieu@2381
   101
  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end (); i++)
mathieu@2381
   102
    {
mathieu@2381
   103
      if (i->first == p->Get ())
mathieu@2381
   104
	{
mathieu@2381
   105
	  return true;
mathieu@2381
   106
	}
mathieu@2381
   107
    }
mathieu@2381
   108
  return false;
mathieu@2381
   109
}
mathieu@2599
   110
std::string 
mathieu@2969
   111
EnumChecker::GetValueTypeName (void) const
mathieu@2599
   112
{
mathieu@2969
   113
  return "ns3::EnumValue";
mathieu@2599
   114
}
mathieu@2599
   115
bool 
mathieu@2969
   116
EnumChecker::HasUnderlyingTypeInformation (void) const
mathieu@2599
   117
{
mathieu@2599
   118
  return true;
mathieu@2599
   119
}
mathieu@2599
   120
std::string 
mathieu@2969
   121
EnumChecker::GetUnderlyingTypeInformation (void) const
mathieu@2599
   122
{
mathieu@2599
   123
  std::ostringstream oss;
mathieu@2599
   124
  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end ();)
mathieu@2599
   125
    {
mathieu@2599
   126
      oss << i->second;
mathieu@2599
   127
      i++;
mathieu@2599
   128
      if (i != m_valueSet.end ())
mathieu@2599
   129
        {
mathieu@2599
   130
          oss << "|";
mathieu@2599
   131
        }
mathieu@2599
   132
    }
mathieu@2599
   133
  return oss.str ();
mathieu@2599
   134
}
mathieu@2965
   135
Ptr<AttributeValue>
mathieu@2520
   136
EnumChecker::Create (void) const
mathieu@2520
   137
{
mathieu@2965
   138
  return ns3::Create<EnumValue> ();
mathieu@2965
   139
}
mathieu@2965
   140
mathieu@2965
   141
bool 
mathieu@2965
   142
EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
mathieu@2965
   143
{
mathieu@2965
   144
  const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
mathieu@2965
   145
  EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
mathieu@2965
   146
  if (src == 0 || dst == 0)
mathieu@2965
   147
    {
mathieu@2965
   148
      return false;
mathieu@2965
   149
    }
mathieu@2965
   150
  *dst = *src;
mathieu@2965
   151
  return true;
mathieu@2520
   152
}
mathieu@2520
   153
mathieu@2381
   154
mathieu@2436
   155
Ptr<const AttributeChecker> 
mathieu@2427
   156
MakeEnumChecker (int v1, std::string n1,
mathieu@2427
   157
		 int v2, std::string n2,
mathieu@2427
   158
		 int v3, std::string n3,
mathieu@2427
   159
		 int v4, std::string n4,
mathieu@2427
   160
		 int v5, std::string n5,
mathieu@2427
   161
		 int v6, std::string n6,
mathieu@2427
   162
		 int v7, std::string n7,
mathieu@2427
   163
		 int v8, std::string n8,
mathieu@2427
   164
		 int v9, std::string n9,
mathieu@2427
   165
		 int v10, std::string n10,
mathieu@2427
   166
		 int v11, std::string n11,
mathieu@2427
   167
		 int v12, std::string n12)
mathieu@2427
   168
{
mathieu@2427
   169
  Ptr<EnumChecker> checker = Create<EnumChecker> ();
mathieu@2427
   170
  checker->AddDefault (v1, n1);
mathieu@2427
   171
  if (n2 == "")
mathieu@2427
   172
    {
mathieu@2427
   173
      return checker;
mathieu@2427
   174
    }
mathieu@2427
   175
  checker->Add (v2, n2);
mathieu@2427
   176
  if (n3 == "")
mathieu@2427
   177
    {
mathieu@2427
   178
      return checker;
mathieu@2427
   179
    }
mathieu@2427
   180
  checker->Add (v3, n3);
mathieu@2427
   181
  if (n4 == "")
mathieu@2427
   182
    {
mathieu@2427
   183
      return checker;
mathieu@2427
   184
    }
mathieu@2427
   185
  checker->Add (v4, n4);
mathieu@2427
   186
  if (n5 == "")
mathieu@2427
   187
    {
mathieu@2427
   188
      return checker;
mathieu@2427
   189
    }
mathieu@2427
   190
  checker->Add (v5, n5);
mathieu@2427
   191
  if (n6 == "")
mathieu@2427
   192
    {
mathieu@2427
   193
      return checker;
mathieu@2427
   194
    }
mathieu@2427
   195
  checker->Add (v6, n6);
mathieu@2427
   196
  if (n7 == "")
mathieu@2427
   197
    {
mathieu@2427
   198
      return checker;
mathieu@2427
   199
    }
mathieu@2427
   200
  checker->Add (v7, n7);
mathieu@2427
   201
  if (n8 == "")
mathieu@2427
   202
    {
mathieu@2427
   203
      return checker;
mathieu@2427
   204
    }
mathieu@2427
   205
  checker->Add (v8, n8);
mathieu@2427
   206
  if (n9 == "")
mathieu@2427
   207
    {
mathieu@2427
   208
      return checker;
mathieu@2427
   209
    }
mathieu@2427
   210
  checker->Add (v9, n9);
mathieu@2427
   211
  if (n10 == "")
mathieu@2427
   212
    {
mathieu@2427
   213
      return checker;
mathieu@2427
   214
    }
mathieu@2427
   215
  checker->Add (v10, n10);
mathieu@2427
   216
  if (n11 == "")
mathieu@2427
   217
    {
mathieu@2427
   218
      return checker;
mathieu@2427
   219
    }
mathieu@2427
   220
  checker->Add (v11, n11);
mathieu@2427
   221
  if (n12 == "")
mathieu@2427
   222
    {
mathieu@2427
   223
      return checker;
mathieu@2427
   224
    }
mathieu@2427
   225
  checker->Add (v12, n12);
mathieu@2427
   226
  return checker;
mathieu@2427
   227
}
mathieu@2427
   228
mathieu@2427
   229
mathieu@2381
   230
} // namespace ns3