A Discrete-Event Network Simulator
API
enum.cc
Go to the documentation of this file.
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 "log.h"
23 #include <sstream>
24 
31 namespace ns3 {
32 
34 
36  : m_value ()
37 {
38  NS_LOG_FUNCTION (this);
39 }
41  : m_value (value)
42 {
43  NS_LOG_FUNCTION (this << value);
44 }
45 void
46 EnumValue::Set (int value)
47 {
48  NS_LOG_FUNCTION (this << value);
49  m_value = value;
50 }
51 int
52 EnumValue::Get (void) const
53 {
54  NS_LOG_FUNCTION (this);
55  return m_value;
56 }
58 EnumValue::Copy (void) const
59 {
60  NS_LOG_FUNCTION (this);
61  return ns3::Create<EnumValue> (*this);
62 }
63 std::string
65 {
66  NS_LOG_FUNCTION (this << checker);
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->first == m_value)
72  {
73  return i->second;
74  }
75  }
76 
77  NS_FATAL_ERROR ("The user has set an invalid C++ value in this Enum");
78  // quiet compiler.
79  return "";
80 }
81 bool
83 {
84  NS_LOG_FUNCTION (this << value << checker);
85  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
86  NS_ASSERT (p != 0);
87  for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
88  {
89  if (i->second == value)
90  {
91  m_value = i->first;
92  return true;
93  }
94  }
95  return false;
96 }
97 
99 {
100  NS_LOG_FUNCTION (this);
101 }
102 
103 void
104 EnumChecker::AddDefault (int value, std::string name)
105 {
106  NS_LOG_FUNCTION (this << value << name);
107  m_valueSet.push_front (std::make_pair (value, name));
108 }
109 void
110 EnumChecker::Add (int value, std::string name)
111 {
112  NS_LOG_FUNCTION (this << value << name);
113  m_valueSet.push_back (std::make_pair (value, name));
114 }
115 bool
116 EnumChecker::Check (const AttributeValue &value) const
117 {
118  NS_LOG_FUNCTION (this << &value);
119  const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
120  if (p == 0)
121  {
122  return false;
123  }
124  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end (); i++)
125  {
126  if (i->first == p->Get ())
127  {
128  return true;
129  }
130  }
131  return false;
132 }
133 std::string
135 {
136  NS_LOG_FUNCTION (this);
137  return "ns3::EnumValue";
138 }
139 bool
141 {
142  NS_LOG_FUNCTION (this);
143  return true;
144 }
145 std::string
147 {
148  NS_LOG_FUNCTION (this);
149  std::ostringstream oss;
150  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end ();)
151  {
152  oss << i->second;
153  i++;
154  if (i != m_valueSet.end ())
155  {
156  oss << "|";
157  }
158  }
159  return oss.str ();
160 }
163 {
164  NS_LOG_FUNCTION (this);
165  return ns3::Create<EnumValue> ();
166 }
167 
168 bool
169 EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
170 {
171  NS_LOG_FUNCTION (this << &source << &destination);
172  const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
173  EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
174  if (src == 0 || dst == 0)
175  {
176  return false;
177  }
178  *dst = *src;
179  return true;
180 }
181 
182 
184 MakeEnumChecker (int v1, std::string n1,
185  int v2, std::string n2,
186  int v3, std::string n3,
187  int v4, std::string n4,
188  int v5, std::string n5,
189  int v6, std::string n6,
190  int v7, std::string n7,
191  int v8, std::string n8,
192  int v9, std::string n9,
193  int v10, std::string n10,
194  int v11, std::string n11,
195  int v12, std::string n12,
196  int v13, std::string n13,
197  int v14, std::string n14,
198  int v15, std::string n15,
199  int v16, std::string n16,
200  int v17, std::string n17,
201  int v18, std::string n18,
202  int v19, std::string n19,
203  int v20, std::string n20,
204  int v21, std::string n21,
205  int v22, std::string n22)
206 {
207  NS_LOG_FUNCTION (v1 << n1 << v2 << n2 << v3 << n3 << v4 << n4 << v5 << n5 <<
208  v6 << n6 << v7 << n7 << v8 << n8 << v9 << n9 << v10 << n10 <<
209  v11 << n11 << v12 << n12 << v13 << n13 << v14 << n14 <<
210  v15 << n15 << v16 << n16 << v17 << n17 << v18 << n18 <<
211  v19 << n19 << v20 << n20 << v21 << n21 << v22 << n22);
212  Ptr<EnumChecker> checker = Create<EnumChecker> ();
213  checker->AddDefault (v1, n1);
214  if (n2 == "")
215  {
216  return checker;
217  }
218  checker->Add (v2, n2);
219  if (n3 == "")
220  {
221  return checker;
222  }
223  checker->Add (v3, n3);
224  if (n4 == "")
225  {
226  return checker;
227  }
228  checker->Add (v4, n4);
229  if (n5 == "")
230  {
231  return checker;
232  }
233  checker->Add (v5, n5);
234  if (n6 == "")
235  {
236  return checker;
237  }
238  checker->Add (v6, n6);
239  if (n7 == "")
240  {
241  return checker;
242  }
243  checker->Add (v7, n7);
244  if (n8 == "")
245  {
246  return checker;
247  }
248  checker->Add (v8, n8);
249  if (n9 == "")
250  {
251  return checker;
252  }
253  checker->Add (v9, n9);
254  if (n10 == "")
255  {
256  return checker;
257  }
258  checker->Add (v10, n10);
259  if (n11 == "")
260  {
261  return checker;
262  }
263  checker->Add (v11, n11);
264  if (n12 == "")
265  {
266  return checker;
267  }
268  checker->Add (v12, n12);
269  if (n13 == "")
270  {
271  return checker;
272  }
273  checker->Add (v13, n13);
274  if (n14 == "")
275  {
276  return checker;
277  }
278  checker->Add (v14, n14);
279  if (n15 == "")
280  {
281  return checker;
282  }
283  checker->Add (v15, n15);
284  if (n16 == "")
285  {
286  return checker;
287  }
288  checker->Add (v16, n16);
289  if (n17 == "")
290  {
291  return checker;
292  }
293  checker->Add (v17, n17);
294  if (n18 == "")
295  {
296  return checker;
297  }
298  checker->Add (v18, n18);
299  if (n19 == "")
300  {
301  return checker;
302  }
303  checker->Add (v19, n19);
304  if (n20 == "")
305  {
306  return checker;
307  }
308  checker->Add (v20, n20);
309  if (n21 == "")
310  {
311  return checker;
312  }
313  checker->Add (v21, n21);
314  if (n22 == "")
315  {
316  return checker;
317  }
318  checker->Add (v22, n22);
319  return checker;
320 }
321 
322 
323 } // namespace ns3
NS_FATAL_x macro definitions.
void AddDefault(int value, std::string name)
Add a default value.
Definition: enum.cc:104
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual std::string GetUnderlyingTypeInformation(void) const
Definition: enum.cc:146
virtual Ptr< AttributeValue > Create(void) const
Definition: enum.cc:162
Hold a value for an Attribute.
Definition: attribute.h:68
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual bool Copy(const AttributeValue &src, AttributeValue &dst) const
Copy the source to the destination.
Definition: enum.cc:169
void Add(int value, std::string name)
Add a new value.
Definition: enum.cc:110
Hold variables of type enum.
Definition: enum.h:54
int m_value
The stored integer value.
Definition: enum.h:74
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: enum.cc:64
ns3::EnumValue attribute value declarations.
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: enum.cc:82
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Set(int value)
Set the value.
Definition: enum.cc:46
virtual bool Check(const AttributeValue &value) const
Definition: enum.cc:116
AttributeChecker implementation for EnumValue.
Definition: enum.h:85
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
virtual std::string GetValueTypeName(void) const
Definition: enum.cc:134
ValueSet m_valueSet
The stored Enum values and symbol names.
Definition: enum.h:115
virtual bool HasUnderlyingTypeInformation(void) const
Definition: enum.cc:140
virtual Ptr< AttributeValue > Copy(void) const
Definition: enum.cc:58
Debug message logging.
int Get(void) const
Definition: enum.cc:52