src/core/model/global-value.cc
changeset 9134 7a750f032acd
parent 9063 32755d0516f4
child 10411 f5916669dbe7
equal deleted inserted replaced
9133:bcf7cef191c1 9134:7a750f032acd
    20 #include "global-value.h"
    20 #include "global-value.h"
    21 #include "fatal-error.h"
    21 #include "fatal-error.h"
    22 #include "attribute.h"
    22 #include "attribute.h"
    23 #include "string.h"
    23 #include "string.h"
    24 #include "uinteger.h"
    24 #include "uinteger.h"
       
    25 #include "log.h"
    25 
    26 
    26 #include "ns3/core-config.h"
    27 #include "ns3/core-config.h"
    27 #ifdef HAVE_STDLIB_H
    28 #ifdef HAVE_STDLIB_H
    28 #include <cstdlib>
    29 #include <cstdlib>
    29 #endif
    30 #endif
    30 
    31 
    31 namespace ns3 {
    32 namespace ns3 {
       
    33 
       
    34 NS_LOG_COMPONENT_DEFINE ("GlobalValue");
    32 
    35 
    33 GlobalValue::GlobalValue (std::string name, std::string help,
    36 GlobalValue::GlobalValue (std::string name, std::string help,
    34                           const AttributeValue &initialValue,
    37                           const AttributeValue &initialValue,
    35                           Ptr<const AttributeChecker> checker)
    38                           Ptr<const AttributeChecker> checker)
    36   : m_name (name),
    39   : m_name (name),
    37     m_help (help),
    40     m_help (help),
    38     m_initialValue (0),
    41     m_initialValue (0),
    39     m_currentValue (0),
    42     m_currentValue (0),
    40     m_checker (checker)
    43     m_checker (checker)
    41 {
    44 {
       
    45   NS_LOG_FUNCTION (name << help << &initialValue << checker);
    42   if (m_checker == 0)
    46   if (m_checker == 0)
    43     {
    47     {
    44       NS_FATAL_ERROR ("Checker should not be zero on " << name );
    48       NS_FATAL_ERROR ("Checker should not be zero on " << name );
    45     }
    49     }
    46   m_initialValue = m_checker->CreateValidValue (initialValue);
    50   m_initialValue = m_checker->CreateValidValue (initialValue);
    54 }
    58 }
    55 
    59 
    56 void
    60 void
    57 GlobalValue::InitializeFromEnv (void)
    61 GlobalValue::InitializeFromEnv (void)
    58 {
    62 {
       
    63   NS_LOG_FUNCTION (this);
    59 #ifdef HAVE_GETENV
    64 #ifdef HAVE_GETENV
    60   char *envVar = getenv ("NS_GLOBAL_VALUE");
    65   char *envVar = getenv ("NS_GLOBAL_VALUE");
    61   if (envVar == 0)
    66   if (envVar == 0)
    62     {
    67     {
    63       return;
    68       return;
    91 }
    96 }
    92 
    97 
    93 std::string 
    98 std::string 
    94 GlobalValue::GetName (void) const
    99 GlobalValue::GetName (void) const
    95 {
   100 {
       
   101   NS_LOG_FUNCTION_NOARGS ();
    96   return m_name;
   102   return m_name;
    97 }
   103 }
    98 std::string 
   104 std::string 
    99 GlobalValue::GetHelp (void) const
   105 GlobalValue::GetHelp (void) const
   100 {
   106 {
       
   107   NS_LOG_FUNCTION_NOARGS ();
   101   return m_help;
   108   return m_help;
   102 }
   109 }
   103 void
   110 void
   104 GlobalValue::GetValue (AttributeValue &value) const
   111 GlobalValue::GetValue (AttributeValue &value) const
   105 {
   112 {
       
   113   NS_LOG_FUNCTION (&value);
   106   bool ok = m_checker->Copy (*m_currentValue, value);
   114   bool ok = m_checker->Copy (*m_currentValue, value);
   107   if (ok)
   115   if (ok)
   108     {
   116     {
   109       return;
   117       return;
   110     }
   118     }
   116   str->Set (m_currentValue->SerializeToString (m_checker));
   124   str->Set (m_currentValue->SerializeToString (m_checker));
   117 }
   125 }
   118 Ptr<const AttributeChecker> 
   126 Ptr<const AttributeChecker> 
   119 GlobalValue::GetChecker (void) const
   127 GlobalValue::GetChecker (void) const
   120 {
   128 {
       
   129   NS_LOG_FUNCTION (this);
       
   130 
   121   return m_checker;
   131   return m_checker;
   122 }
   132 }
   123 
   133 
   124 bool
   134 bool
   125 GlobalValue::SetValue (const AttributeValue &value)
   135 GlobalValue::SetValue (const AttributeValue &value)
   126 {
   136 {
       
   137   NS_LOG_FUNCTION (&value);
       
   138 
   127   Ptr<AttributeValue> v = m_checker->CreateValidValue (value);
   139   Ptr<AttributeValue> v = m_checker->CreateValidValue (value);
   128   if (v == 0)
   140   if (v == 0)
   129     {
   141     {
   130       return 0;
   142       return 0;
   131     }
   143     }
   134 }
   146 }
   135 
   147 
   136 void 
   148 void 
   137 GlobalValue::Bind (std::string name, const AttributeValue &value)
   149 GlobalValue::Bind (std::string name, const AttributeValue &value)
   138 {
   150 {
       
   151   NS_LOG_FUNCTION (name << &value);
       
   152 
   139   for (Iterator i = Begin (); i != End (); i++)
   153   for (Iterator i = Begin (); i != End (); i++)
   140     {
   154     {
   141       if ((*i)->GetName () == name)
   155       if ((*i)->GetName () == name)
   142         {
   156         {
   143           if (!(*i)->SetValue (value))
   157           if (!(*i)->SetValue (value))
   150   NS_FATAL_ERROR ("Non-existant global value: "<<name);
   164   NS_FATAL_ERROR ("Non-existant global value: "<<name);
   151 }
   165 }
   152 bool 
   166 bool 
   153 GlobalValue::BindFailSafe (std::string name, const AttributeValue &value)
   167 GlobalValue::BindFailSafe (std::string name, const AttributeValue &value)
   154 {
   168 {
       
   169   NS_LOG_FUNCTION (name << &value);
       
   170 
   155   for (Iterator i = Begin (); i != End (); i++)
   171   for (Iterator i = Begin (); i != End (); i++)
   156     {
   172     {
   157       if ((*i)->GetName () == name)
   173       if ((*i)->GetName () == name)
   158         {
   174         {
   159           return (*i)->SetValue (value);
   175           return (*i)->SetValue (value);
   162   return false;
   178   return false;
   163 }
   179 }
   164 GlobalValue::Iterator 
   180 GlobalValue::Iterator 
   165 GlobalValue::Begin (void)
   181 GlobalValue::Begin (void)
   166 {
   182 {
       
   183   NS_LOG_FUNCTION_NOARGS ();
       
   184 
   167   return GetVector ()->begin ();
   185   return GetVector ()->begin ();
   168 }
   186 }
   169 GlobalValue::Iterator 
   187 GlobalValue::Iterator 
   170 GlobalValue::End (void)
   188 GlobalValue::End (void)
   171 {
   189 {
       
   190   NS_LOG_FUNCTION_NOARGS ();
   172   return GetVector ()->end ();
   191   return GetVector ()->end ();
   173 }
   192 }
   174 
   193 
   175 void 
   194 void 
   176 GlobalValue::ResetInitialValue (void)
   195 GlobalValue::ResetInitialValue (void)
   177 {
   196 {
       
   197   NS_LOG_FUNCTION (this);
   178   m_currentValue = m_initialValue;
   198   m_currentValue = m_initialValue;
   179 }
   199 }
   180 
   200 
   181 bool
   201 bool
   182 GlobalValue::GetValueByNameFailSafe (std::string name, AttributeValue &value)
   202 GlobalValue::GetValueByNameFailSafe (std::string name, AttributeValue &value)
   183 {
   203 {
       
   204   NS_LOG_FUNCTION (name << &value);
   184   for (GlobalValue::Iterator gvit = GlobalValue::Begin (); gvit != GlobalValue::End (); ++gvit)
   205   for (GlobalValue::Iterator gvit = GlobalValue::Begin (); gvit != GlobalValue::End (); ++gvit)
   185     {
   206     {
   186       if ((*gvit)->GetName () == name)
   207       if ((*gvit)->GetName () == name)
   187         {
   208         {
   188           (*gvit)->GetValue (value);
   209           (*gvit)->GetValue (value);
   193 }
   214 }
   194 
   215 
   195 void
   216 void
   196 GlobalValue::GetValueByName (std::string name, AttributeValue &value)
   217 GlobalValue::GetValueByName (std::string name, AttributeValue &value)
   197 {
   218 {
       
   219   NS_LOG_FUNCTION (name << &value);
   198   if (!GetValueByNameFailSafe (name, value))
   220   if (!GetValueByNameFailSafe (name, value))
   199     {
   221     {
   200       NS_FATAL_ERROR ("Could not find GlobalValue named \"" << name << "\"");
   222       NS_FATAL_ERROR ("Could not find GlobalValue named \"" << name << "\"");
   201     }
   223     }
   202 }
   224 }
   203 
   225 
   204 GlobalValue::Vector *
   226 GlobalValue::Vector *
   205 GlobalValue::GetVector (void)
   227 GlobalValue::GetVector (void)
   206 {
   228 {
       
   229   NS_LOG_FUNCTION_NOARGS ();
   207   static Vector vector;
   230   static Vector vector;
   208   return &vector;
   231   return &vector;
   209 }
   232 }
   210 
   233 
   211 } // namespace ns3
   234 } // namespace ns3