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 <sstream>
23 
24 namespace ns3 {
25 
27  : m_v ()
28 {
29 }
31  : m_v (v)
32 {
33 }
34 void
36 {
37  m_v = v;
38 }
39 int
40 EnumValue::Get (void) const
41 {
42  return m_v;
43 }
45 EnumValue::Copy (void) const
46 {
47  return ns3::Create<EnumValue> (*this);
48 }
49 std::string
51 {
52  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
53  NS_ASSERT (p != 0);
54  for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
55  {
56  if (i->first == m_v)
57  {
58  return i->second;
59  }
60  }
61 
62  NS_FATAL_ERROR ("The user has set an invalid C++ value in this Enum");
63  // quiet compiler.
64  return "";
65 }
66 bool
68 {
69  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
70  NS_ASSERT (p != 0);
71  for (EnumChecker::ValueSet::const_iterator i = p->m_valueSet.begin (); i != p->m_valueSet.end (); i++)
72  {
73  if (i->second == value)
74  {
75  m_v = i->first;
76  return true;
77  }
78  }
79  return false;
80 }
81 
83 {
84 }
85 
86 void
87 EnumChecker::AddDefault (int v, std::string name)
88 {
89  m_valueSet.push_front (std::make_pair (v, name));
90 }
91 void
92 EnumChecker::Add (int v, std::string name)
93 {
94  m_valueSet.push_back (std::make_pair (v, name));
95 }
96 bool
97 EnumChecker::Check (const AttributeValue &value) const
98 {
99  const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
100  if (p == 0)
101  {
102  return false;
103  }
104  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end (); i++)
105  {
106  if (i->first == p->Get ())
107  {
108  return true;
109  }
110  }
111  return false;
112 }
113 std::string
115 {
116  return "ns3::EnumValue";
117 }
118 bool
120 {
121  return true;
122 }
123 std::string
125 {
126  std::ostringstream oss;
127  for (ValueSet::const_iterator i = m_valueSet.begin (); i != m_valueSet.end ();)
128  {
129  oss << i->second;
130  i++;
131  if (i != m_valueSet.end ())
132  {
133  oss << "|";
134  }
135  }
136  return oss.str ();
137 }
140 {
141  return ns3::Create<EnumValue> ();
142 }
143 
144 bool
145 EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
146 {
147  const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
148  EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
149  if (src == 0 || dst == 0)
150  {
151  return false;
152  }
153  *dst = *src;
154  return true;
155 }
156 
157 
159 MakeEnumChecker (int v1, std::string n1,
160  int v2, std::string n2,
161  int v3, std::string n3,
162  int v4, std::string n4,
163  int v5, std::string n5,
164  int v6, std::string n6,
165  int v7, std::string n7,
166  int v8, std::string n8,
167  int v9, std::string n9,
168  int v10, std::string n10,
169  int v11, std::string n11,
170  int v12, std::string n12,
171  int v13, std::string n13,
172  int v14, std::string n14,
173  int v15, std::string n15,
174  int v16, std::string n16,
175  int v17, std::string n17,
176  int v18, std::string n18,
177  int v19, std::string n19,
178  int v20, std::string n20,
179  int v21, std::string n21,
180  int v22, std::string n22)
181 {
182  Ptr<EnumChecker> checker = Create<EnumChecker> ();
183  checker->AddDefault (v1, n1);
184  if (n2 == "")
185  {
186  return checker;
187  }
188  checker->Add (v2, n2);
189  if (n3 == "")
190  {
191  return checker;
192  }
193  checker->Add (v3, n3);
194  if (n4 == "")
195  {
196  return checker;
197  }
198  checker->Add (v4, n4);
199  if (n5 == "")
200  {
201  return checker;
202  }
203  checker->Add (v5, n5);
204  if (n6 == "")
205  {
206  return checker;
207  }
208  checker->Add (v6, n6);
209  if (n7 == "")
210  {
211  return checker;
212  }
213  checker->Add (v7, n7);
214  if (n8 == "")
215  {
216  return checker;
217  }
218  checker->Add (v8, n8);
219  if (n9 == "")
220  {
221  return checker;
222  }
223  checker->Add (v9, n9);
224  if (n10 == "")
225  {
226  return checker;
227  }
228  checker->Add (v10, n10);
229  if (n11 == "")
230  {
231  return checker;
232  }
233  checker->Add (v11, n11);
234  if (n12 == "")
235  {
236  return checker;
237  }
238  checker->Add (v12, n12);
239  if (n13 == "")
240  {
241  return checker;
242  }
243  checker->Add (v13, n13);
244  if (n14 == "")
245  {
246  return checker;
247  }
248  checker->Add (v14, n14);
249  if (n15 == "")
250  {
251  return checker;
252  }
253  checker->Add (v15, n15);
254  if (n16 == "")
255  {
256  return checker;
257  }
258  checker->Add (v16, n16);
259  if (n17 == "")
260  {
261  return checker;
262  }
263  checker->Add (v17, n17);
264  if (n18 == "")
265  {
266  return checker;
267  }
268  checker->Add (v18, n18);
269  if (n19 == "")
270  {
271  return checker;
272  }
273  checker->Add (v19, n19);
274  if (n20 == "")
275  {
276  return checker;
277  }
278  checker->Add (v20, n20);
279  if (n21 == "")
280  {
281  return checker;
282  }
283  checker->Add (v21, n21);
284  if (n22 == "")
285  {
286  return checker;
287  }
288  checker->Add (v22, n22);
289  return checker;
290 }
291 
292 
293 } // namespace ns3