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