src/core/model/enum.cc
changeset 9134 7a750f032acd
parent 7169 358f71a624d8
child 10968 2d29fee2b7b8
equal deleted inserted replaced
9133:bcf7cef191c1 9134:7a750f032acd
    17  *
    17  *
    18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    19  */
    20 #include "enum.h"
    20 #include "enum.h"
    21 #include "fatal-error.h"
    21 #include "fatal-error.h"
       
    22 #include "log.h"
    22 #include <sstream>
    23 #include <sstream>
       
    24 
       
    25 NS_LOG_COMPONENT_DEFINE ("Enum");
    23 
    26 
    24 namespace ns3 {
    27 namespace ns3 {
    25 
    28 
    26 EnumValue::EnumValue ()
    29 EnumValue::EnumValue ()
    27   : m_v ()
    30   : m_v ()
    28 {
    31 {
       
    32   NS_LOG_FUNCTION (this);
    29 }
    33 }
    30 EnumValue::EnumValue (int v)
    34 EnumValue::EnumValue (int v)
    31   : m_v (v)
    35   : m_v (v)
    32 {
    36 {
       
    37   NS_LOG_FUNCTION (this << v);
    33 }
    38 }
    34 void
    39 void
    35 EnumValue::Set (int v)
    40 EnumValue::Set (int v)
    36 {
    41 {
       
    42   NS_LOG_FUNCTION (this << v);
    37   m_v = v;
    43   m_v = v;
    38 }
    44 }
    39 int
    45 int
    40 EnumValue::Get (void) const
    46 EnumValue::Get (void) const
    41 {
    47 {
       
    48   NS_LOG_FUNCTION (this);
    42   return m_v;
    49   return m_v;
    43 }
    50 }
    44 Ptr<AttributeValue>
    51 Ptr<AttributeValue>
    45 EnumValue::Copy (void) const
    52 EnumValue::Copy (void) const
    46 {
    53 {
       
    54   NS_LOG_FUNCTION (this);
    47   return ns3::Create<EnumValue> (*this);
    55   return ns3::Create<EnumValue> (*this);
    48 }
    56 }
    49 std::string 
    57 std::string 
    50 EnumValue::SerializeToString (Ptr<const AttributeChecker> checker) const
    58 EnumValue::SerializeToString (Ptr<const AttributeChecker> checker) const
    51 {
    59 {
       
    60   NS_LOG_FUNCTION (this << checker);
    52   const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
    61   const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
    53   NS_ASSERT (p != 0);
    62   NS_ASSERT (p != 0);
    54   for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
    63   for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
    55     {
    64     {
    56       if (i->first == m_v)
    65       if (i->first == m_v)
    64   return "";
    73   return "";
    65 }
    74 }
    66 bool 
    75 bool 
    67 EnumValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
    76 EnumValue::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker)
    68 {
    77 {
       
    78   NS_LOG_FUNCTION (this << value << checker);
    69   const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
    79   const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
    70   NS_ASSERT (p != 0);
    80   NS_ASSERT (p != 0);
    71   for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
    81   for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
    72     {
    82     {
    73       if (i->second == value)
    83       if (i->second == value)
    79   return false;
    89   return false;
    80 }
    90 }
    81 
    91 
    82 EnumChecker::EnumChecker ()
    92 EnumChecker::EnumChecker ()
    83 {
    93 {
       
    94   NS_LOG_FUNCTION (this);
    84 }
    95 }
    85 
    96 
    86 void
    97 void
    87 EnumChecker::AddDefault (int v, std::string name)
    98 EnumChecker::AddDefault (int v, std::string name)
    88 {
    99 {
       
   100   NS_LOG_FUNCTION (this << v << name);
    89   m_valueSet.push_front (std::make_pair (v, name));
   101   m_valueSet.push_front (std::make_pair (v, name));
    90 }
   102 }
    91 void
   103 void
    92 EnumChecker::Add (int v, std::string name)
   104 EnumChecker::Add (int v, std::string name)
    93 {
   105 {
       
   106   NS_LOG_FUNCTION (this << v << name);
    94   m_valueSet.push_back (std::make_pair (v, name));
   107   m_valueSet.push_back (std::make_pair (v, name));
    95 }
   108 }
    96 bool
   109 bool
    97 EnumChecker::Check (const AttributeValue &value) const
   110 EnumChecker::Check (const AttributeValue &value) const
    98 {
   111 {
       
   112   NS_LOG_FUNCTION (this << &value);
    99   const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
   113   const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
   100   if (p == 0)
   114   if (p == 0)
   101     {
   115     {
   102       return false;
   116       return false;
   103     }
   117     }
   111   return false;
   125   return false;
   112 }
   126 }
   113 std::string 
   127 std::string 
   114 EnumChecker::GetValueTypeName (void) const
   128 EnumChecker::GetValueTypeName (void) const
   115 {
   129 {
       
   130   NS_LOG_FUNCTION (this);
   116   return "ns3::EnumValue";
   131   return "ns3::EnumValue";
   117 }
   132 }
   118 bool 
   133 bool 
   119 EnumChecker::HasUnderlyingTypeInformation (void) const
   134 EnumChecker::HasUnderlyingTypeInformation (void) const
   120 {
   135 {
       
   136   NS_LOG_FUNCTION (this);
   121   return true;
   137   return true;
   122 }
   138 }
   123 std::string 
   139 std::string 
   124 EnumChecker::GetUnderlyingTypeInformation (void) const
   140 EnumChecker::GetUnderlyingTypeInformation (void) const
   125 {
   141 {
       
   142   NS_LOG_FUNCTION (this);
   126   std::ostringstream oss;
   143   std::ostringstream oss;
   127   for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end ();)
   144   for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end ();)
   128     {
   145     {
   129       oss << i->second;
   146       oss << i->second;
   130       i++;
   147       i++;
   136   return oss.str ();
   153   return oss.str ();
   137 }
   154 }
   138 Ptr<AttributeValue>
   155 Ptr<AttributeValue>
   139 EnumChecker::Create (void) const
   156 EnumChecker::Create (void) const
   140 {
   157 {
       
   158   NS_LOG_FUNCTION (this);
   141   return ns3::Create<EnumValue> ();
   159   return ns3::Create<EnumValue> ();
   142 }
   160 }
   143 
   161 
   144 bool 
   162 bool 
   145 EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
   163 EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
   146 {
   164 {
       
   165   NS_LOG_FUNCTION (this << &source << &destination);
   147   const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
   166   const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
   148   EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
   167   EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
   149   if (src == 0 || dst == 0)
   168   if (src == 0 || dst == 0)
   150     {
   169     {
   151       return false;
   170       return false;
   177                  int v19, std::string n19,
   196                  int v19, std::string n19,
   178                  int v20, std::string n20,
   197                  int v20, std::string n20,
   179                  int v21, std::string n21,
   198                  int v21, std::string n21,
   180                  int v22, std::string n22)
   199                  int v22, std::string n22)
   181 {
   200 {
       
   201   NS_LOG_FUNCTION (v1 << n1 << v2 << n2 << v3 << n3 << v4 << n4 << v5 << n5 <<
       
   202                    v6 << n6 << v7 << n7 << v8 << n8 << v9 << n9 << v10 << n10 <<
       
   203                    v11 << n11 << v12 << n12 << v13 << n13 << v14 << n14 <<
       
   204                    v15 << n15 << v16 << n16 << v17 << n17 << v18 << n18 <<
       
   205                    v19 << n19 << v20 << n20 << v21 << n21 << v22 << n22);
   182   Ptr<EnumChecker> checker = Create<EnumChecker> ();
   206   Ptr<EnumChecker> checker = Create<EnumChecker> ();
   183   checker->AddDefault (v1, n1);
   207   checker->AddDefault (v1, n1);
   184   if (n2 == "")
   208   if (n2 == "")
   185     {
   209     {
   186       return checker;
   210       return checker;