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 
179 } // namespace ns3
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_ASSERT
#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
fatal-error.h
NS_FATAL_x macro definitions.
ns3::EnumChecker::GetValue
int GetValue(const std::string name) const
Get the enum value by name.
Definition: enum.cc:110
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::EnumValue::SerializeToString
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: enum.cc:65
ns3::EnumValue::Set
void Set(int value)
Set the value.
Definition: enum.cc:47
ns3::EnumValue::EnumValue
EnumValue()
Definition: enum.cc:36
ns3::EnumChecker::EnumChecker
EnumChecker()
Definition: enum.cc:83
ns3::AttributeValue
Hold a value for an Attribute.
Definition: attribute.h:69
ns3::EnumChecker::GetUnderlyingTypeInformation
virtual std::string GetUnderlyingTypeInformation(void) const
Definition: enum.cc:145
ns3::EnumValue::Get
int Get(void) const
Definition: enum.cc:53
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
ns3::EnumValue
Hold variables of type enum.
Definition: enum.h:55
ns3::EnumValue::m_value
int m_value
The stored integer value.
Definition: enum.h:74
ns3::EnumChecker::Check
virtual bool Check(const AttributeValue &value) const
Definition: enum.cc:119
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::EnumChecker::GetName
std::string GetName(int value) const
Get the enum symbol name by value.
Definition: enum.cc:101
ns3::EnumChecker::HasUnderlyingTypeInformation
virtual bool HasUnderlyingTypeInformation(void) const
Definition: enum.cc:139
ns3::EnumChecker::Add
void Add(int value, std::string name)
Add a new value.
Definition: enum.cc:95
ns3::EnumChecker::GetValueTypeName
virtual std::string GetValueTypeName(void) const
Definition: enum.cc:133
ns3::EnumValue::DeserializeFromString
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: enum.cc:74
ns3::EnumChecker::Copy
virtual bool Copy(const AttributeValue &src, AttributeValue &dst) const
Copy the source to the destination.
Definition: enum.cc:165
NS_ASSERT_MSG
#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
ns3::EnumChecker::AddDefault
void AddDefault(int value, std::string name)
Add a default value.
Definition: enum.cc:89
ns3::EnumChecker::m_valueSet
ValueSet m_valueSet
The stored Enum values and symbol names.
Definition: enum.h:131
ns3::EnumChecker::Value
std::pair< int, std::string > Value
Type for the pair value, name.
Definition: enum.h:127
ns3::EnumValue::Copy
virtual Ptr< AttributeValue > Copy(void) const
Definition: enum.cc:59
log.h
Debug message logging.
ns3::EnumChecker
AttributeChecker implementation for EnumValue.
Definition: enum.h:86
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::EnumChecker::Create
virtual Ptr< AttributeValue > Create(void) const
Definition: enum.cc:158
enum.h
ns3::EnumValue attribute value declarations.