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