00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "global-value.h"
00021 #include "fatal-error.h"
00022 #include "attribute.h"
00023 #include "string.h"
00024
00025 namespace ns3 {
00026
00027 GlobalValue::GlobalValue (std::string name, std::string help,
00028 const AttributeValue &initialValue,
00029 Ptr<const AttributeChecker> checker)
00030 : m_name (name),
00031 m_help (help),
00032 m_initialValue (initialValue.Copy ()),
00033 m_checker (checker)
00034 {
00035 if (m_checker == 0)
00036 {
00037 NS_FATAL_ERROR ("Checker should no be zero.");
00038 }
00039 GetVector ()->push_back (this);
00040 }
00041
00042 std::string
00043 GlobalValue::GetName (void) const
00044 {
00045 return m_name;
00046 }
00047 std::string
00048 GlobalValue::GetHelp (void) const
00049 {
00050 return m_help;
00051 }
00052 void
00053 GlobalValue::GetValue (AttributeValue &value) const
00054 {
00055 bool ok = m_checker->Copy (*m_initialValue, value);
00056 if (ok)
00057 {
00058 return;
00059 }
00060 StringValue *str = dynamic_cast<StringValue *> (&value);
00061 if (str == 0)
00062 {
00063 NS_FATAL_ERROR ("GlobalValue name="<<m_name<<": input value is not a string");
00064 }
00065 str->Set (m_initialValue->SerializeToString (m_checker));
00066 }
00067 Ptr<const AttributeChecker>
00068 GlobalValue::GetChecker (void) const
00069 {
00070 return m_checker;
00071 }
00072
00073 bool
00074 GlobalValue::SetValue (const AttributeValue &value)
00075 {
00076 if (m_checker->Check (value))
00077 {
00078 m_initialValue = value.Copy ();
00079 return true;
00080 }
00081
00082 const StringValue *str = dynamic_cast<const StringValue *> (&value);
00083 if (str == 0)
00084 {
00085 return false;
00086 }
00087
00088 Ptr<AttributeValue> v = m_checker->Create ();
00089 bool ok = v->DeserializeFromString (str->Get (), m_checker);
00090 if (!ok)
00091 {
00092 return false;
00093 }
00094 ok = m_checker->Check (*v);
00095 if (!ok)
00096 {
00097 return false;
00098 }
00099 m_checker->Copy (*v, *PeekPointer (m_initialValue));
00100 return true;
00101 }
00102
00103 void
00104 GlobalValue::Bind (std::string name, const AttributeValue &value)
00105 {
00106 for (Iterator i = Begin (); i != End (); i++)
00107 {
00108 if ((*i)->GetName () == name)
00109 {
00110 if (!(*i)->SetValue (value))
00111 {
00112 NS_FATAL_ERROR ("Invalid new value for global value: "<<name);
00113 }
00114 return;
00115 }
00116 }
00117 NS_FATAL_ERROR ("Non-existant global value: "<<name);
00118 }
00119 bool
00120 GlobalValue::BindFailSafe (std::string name, const AttributeValue &value)
00121 {
00122 for (Iterator i = Begin (); i != End (); i++)
00123 {
00124 if ((*i)->GetName () == name)
00125 {
00126 return (*i)->SetValue (value);
00127 }
00128 }
00129 return false;
00130 }
00131 GlobalValue::Iterator
00132 GlobalValue::Begin (void)
00133 {
00134 return GetVector ()->begin ();
00135 }
00136 GlobalValue::Iterator
00137 GlobalValue::End (void)
00138 {
00139 return GetVector ()->end ();
00140 }
00141 GlobalValue::Vector *
00142 GlobalValue::GetVector (void)
00143 {
00144 static Vector vector;
00145 return &vector;
00146 }
00147
00148 }
00149
00150 #ifdef RUN_SELF_TESTS
00151
00152 #include "test.h"
00153 #include "uinteger.h"
00154
00155 namespace ns3 {
00156
00157 class GlobalValueTests : public Test
00158 {
00159 public:
00160 GlobalValueTests ();
00161 virtual bool RunTests (void);
00162 private:
00163 };
00164
00165 GlobalValueTests::GlobalValueTests ()
00166 : Test ("GlobalValue")
00167 {}
00168 bool
00169 GlobalValueTests::RunTests (void)
00170 {
00171 bool result = true;
00172 GlobalValue uint = GlobalValue ("TestUint", "help text",
00173 UintegerValue (10),
00174 MakeUintegerChecker<uint32_t> ());
00175
00176
00177 UintegerValue v;
00178 uint.GetValue (v);
00179 NS_TEST_ASSERT_EQUAL (10, v.Get ());
00180
00181 GlobalValue::Vector *vector = GlobalValue::GetVector ();
00182 for (GlobalValue::Vector::iterator i = vector->begin (); i != vector->end (); ++i)
00183 {
00184 if ((*i) == &uint)
00185 {
00186 vector->erase (i);
00187 break;
00188 }
00189 }
00190
00191 return result;
00192 }
00193
00194 static GlobalValueTests g_initialValueTests;
00195
00196 }
00197
00198 #endif