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 #include <numeric> // std::accumulate
26 
33 namespace ns3 {
34 
36 
38  : m_value ()
39 {
40  NS_LOG_FUNCTION (this);
41 }
43  : m_value (value)
44 {
45  NS_LOG_FUNCTION (this << value);
46 }
47 void
48 EnumValue::Set (int value)
49 {
50  NS_LOG_FUNCTION (this << value);
51  m_value = value;
52 }
53 int
54 EnumValue::Get (void) const
55 {
56  NS_LOG_FUNCTION (this);
57  return m_value;
58 }
60 EnumValue::Copy (void) const
61 {
62  NS_LOG_FUNCTION (this);
63  return ns3::Create<EnumValue> (*this);
64 }
65 std::string
67 {
68  NS_LOG_FUNCTION (this << checker);
69  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
70  NS_ASSERT (p != 0);
71  std::string name = p->GetName (m_value);
72  return name;
73 }
74 bool
76 {
77  NS_LOG_FUNCTION (this << value << checker);
78  const EnumChecker *p = dynamic_cast<const EnumChecker *> (PeekPointer (checker));
79  NS_ASSERT (p != 0);
80  m_value = p->GetValue (value);
81  return true;
82 }
83 
85 {
86  NS_LOG_FUNCTION (this);
87 }
88 
89 void
90 EnumChecker::AddDefault (int value, std::string name)
91 {
92  NS_LOG_FUNCTION (this << value << name);
93  m_valueSet.push_front (std::make_pair (value, name));
94 }
95 void
96 EnumChecker::Add (int value, std::string name)
97 {
98  NS_LOG_FUNCTION (this << value << name);
99  m_valueSet.push_back (std::make_pair (value, name));
100 }
101 std::string
102 EnumChecker::GetName (int value) const
103 {
104  auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
105  [value] (Value v) { return v.first == value; } );
106  NS_ASSERT_MSG (it != m_valueSet.end (),
107  "invalid enum value " << value << "! Missed entry in MakeEnumChecker?");
108  return it->second;
109 }
110 int
111 EnumChecker::GetValue (const std::string name) const
112 {
113  auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
114  [name] (Value v) { return v.second == name; } );
115  NS_ASSERT_MSG (it != m_valueSet.end (),
116  "name " << name << " is not a valid enum value. Missed entry in MakeEnumChecker?\nAvailable values: " <<
117  std::accumulate (m_valueSet.begin (), m_valueSet.end (), std::string{}, [](std::string a, Value v) {
118  if (a.empty ())
119  {
120  return v.second;
121  }
122  else
123  {
124  return std::move (a) + ", " + v.second;
125  }
126  }));
127  return it->first;
128 }
129 bool
130 EnumChecker::Check (const AttributeValue &value) const
131 {
132  NS_LOG_FUNCTION (this << &value);
133  const EnumValue *p = dynamic_cast<const EnumValue *> (&value);
134  if (p == 0)
135  {
136  return false;
137  }
138  auto pvalue = p->Get ();
139  auto it = std::find_if (m_valueSet.begin (), m_valueSet.end (),
140  [pvalue] (Value v) { return v.first == pvalue; } );
141  return (it != m_valueSet.end ());
142 }
143 std::string
144 EnumChecker::GetValueTypeName (void) const
145 {
146  NS_LOG_FUNCTION (this);
147  return "ns3::EnumValue";
148 }
149 bool
150 EnumChecker::HasUnderlyingTypeInformation (void) const
151 {
152  NS_LOG_FUNCTION (this);
153  return true;
154 }
155 std::string
156 EnumChecker::GetUnderlyingTypeInformation (void) const
157 {
158  NS_LOG_FUNCTION (this);
159  std::ostringstream oss;
160  bool moreValues = false;
161  for (const auto &i : m_valueSet)
162  {
163  oss << (moreValues ? "|" : "") << i.second;
164  moreValues = true;
165  }
166  return oss.str ();
167 }
170 {
171  NS_LOG_FUNCTION (this);
172  return ns3::Create<EnumValue> ();
173 }
174 
175 bool
176 EnumChecker::Copy (const AttributeValue &source, AttributeValue &destination) const
177 {
178  NS_LOG_FUNCTION (this << &source << &destination);
179  const EnumValue *src = dynamic_cast<const EnumValue *> (&source);
180  EnumValue *dst = dynamic_cast<EnumValue *> (&destination);
181  if (src == 0 || dst == 0)
182  {
183  return false;
184  }
185  *dst = *src;
186  return true;
187 }
188 
189 
190 } // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:69
AttributeChecker implementation for EnumValue.
Definition: enum.h:86
void Add(int value, std::string name)
Add a new value.
Definition: enum.cc:96
ValueSet m_valueSet
The stored Enum values and symbol names.
Definition: enum.h:131
int GetValue(const std::string name) const
Get the enum value by name.
Definition: enum.cc:111
std::pair< int, std::string > Value
Type for the pair value, name.
Definition: enum.h:127
std::string GetName(int value) const
Get the enum symbol name by value.
Definition: enum.cc:102
void AddDefault(int value, std::string name)
Add a default value.
Definition: enum.cc:90
Hold variables of type enum.
Definition: enum.h:55
void Set(int value)
Set the value.
Definition: enum.cc:48
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: enum.cc:66
int Get(void) const
Definition: enum.cc:54
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: enum.cc:75
int m_value
The stored integer value.
Definition: enum.h:74
virtual Ptr< AttributeValue > Copy(void) const
Definition: enum.cc:60
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::EnumValue attribute value declarations.
NS_FATAL_x macro definitions.
#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
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:409
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:555