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