A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
enum.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "enum.h"
20
21#include "fatal-error.h"
22#include "log.h"
23
24#include <algorithm> // find_if
25#include <numeric> // std::accumulate
26#include <sstream>
27
34namespace ns3
35{
36
38
40 : m_value()
41{
42 NS_LOG_FUNCTION(this);
43}
44
46 : m_value(value)
47{
48 NS_LOG_FUNCTION(this << value);
49}
50
51void
53{
54 NS_LOG_FUNCTION(this << value);
55 m_value = value;
56}
57
58int
60{
61 NS_LOG_FUNCTION(this);
62 return m_value;
63}
64
67{
68 NS_LOG_FUNCTION(this);
69 return ns3::Create<EnumValue>(*this);
70}
71
72std::string
74{
75 NS_LOG_FUNCTION(this << checker);
76 const EnumChecker* p = dynamic_cast<const EnumChecker*>(PeekPointer(checker));
77 NS_ASSERT(p != nullptr);
78 std::string name = p->GetName(m_value);
79 return name;
80}
81
82bool
84{
85 NS_LOG_FUNCTION(this << value << checker);
86 const EnumChecker* p = dynamic_cast<const EnumChecker*>(PeekPointer(checker));
87 NS_ASSERT(p != nullptr);
88 m_value = p->GetValue(value);
89 return true;
90}
91
93{
94 NS_LOG_FUNCTION(this);
95}
96
97void
98EnumChecker::AddDefault(int value, std::string name)
99{
100 NS_LOG_FUNCTION(this << value << name);
101 m_valueSet.emplace_front(value, name);
102}
103
104void
105EnumChecker::Add(int value, std::string name)
106{
107 NS_LOG_FUNCTION(this << value << name);
108 m_valueSet.emplace_back(value, name);
109}
110
111std::string
112EnumChecker::GetName(int value) const
113{
114 auto it = std::find_if(m_valueSet.begin(), m_valueSet.end(), [value](Value v) {
115 return v.first == value;
116 });
117 NS_ASSERT_MSG(it != m_valueSet.end(),
118 "invalid enum value " << value << "! Missed entry in MakeEnumChecker?");
119 return it->second;
120}
121
122int
123EnumChecker::GetValue(const std::string name) const
124{
125 auto it = std::find_if(m_valueSet.begin(), m_valueSet.end(), [name](Value v) {
126 return v.second == name;
127 });
129 it != m_valueSet.end(),
130 "name "
131 << name
132 << " is not a valid enum value. Missed entry in MakeEnumChecker?\nAvailable values: "
133 << std::accumulate(m_valueSet.begin(),
134 m_valueSet.end(),
135 std::string{},
136 [](std::string a, Value v) {
137 if (a.empty())
138 {
139 return v.second;
140 }
141 else
142 {
143 return std::move(a) + ", " + v.second;
144 }
145 }));
146 return it->first;
147}
148
149bool
150EnumChecker::Check(const AttributeValue& value) const
151{
152 NS_LOG_FUNCTION(this << &value);
153 const EnumValue* p = dynamic_cast<const EnumValue*>(&value);
154 if (p == nullptr)
155 {
156 return false;
157 }
158 auto pvalue = p->Get();
159 auto it = std::find_if(m_valueSet.begin(), m_valueSet.end(), [pvalue](Value v) {
160 return v.first == pvalue;
161 });
162 return (it != m_valueSet.end());
163}
164
165std::string
166EnumChecker::GetValueTypeName() const
167{
168 NS_LOG_FUNCTION(this);
169 return "ns3::EnumValue";
170}
171
172bool
173EnumChecker::HasUnderlyingTypeInformation() const
174{
175 NS_LOG_FUNCTION(this);
176 return true;
177}
178
179std::string
180EnumChecker::GetUnderlyingTypeInformation() const
181{
182 NS_LOG_FUNCTION(this);
183 std::ostringstream oss;
184 bool moreValues = false;
185 for (const auto& i : m_valueSet)
186 {
187 oss << (moreValues ? "|" : "") << i.second;
188 moreValues = true;
189 }
190 return oss.str();
191}
192
194EnumChecker::Create() const
195{
196 NS_LOG_FUNCTION(this);
197 return ns3::Create<EnumValue>();
198}
199
200bool
201EnumChecker::Copy(const AttributeValue& source, AttributeValue& destination) const
202{
203 NS_LOG_FUNCTION(this << &source << &destination);
204 const EnumValue* src = dynamic_cast<const EnumValue*>(&source);
205 EnumValue* dst = dynamic_cast<EnumValue*>(&destination);
206 if (src == nullptr || dst == nullptr)
207 {
208 return false;
209 }
210 *dst = *src;
211 return true;
212}
213
214} // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:70
AttributeChecker implementation for EnumValue.
Definition: enum.h:87
std::pair< int, std::string > Value
Type for the pair value, name.
Definition: enum.h:128
void Add(int value, std::string name)
Add a new value.
Definition: enum.cc:105
ValueSet m_valueSet
The stored Enum values and symbol names.
Definition: enum.h:132
int GetValue(const std::string name) const
Get the enum value by name.
Definition: enum.cc:123
std::string GetName(int value) const
Get the enum symbol name by value.
Definition: enum.cc:112
void AddDefault(int value, std::string name)
Add a default value.
Definition: enum.cc:98
Hold variables of type enum.
Definition: enum.h:56
void Set(int value)
Set the value.
Definition: enum.cc:52
int Get() const
Definition: enum.cc:59
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: enum.cc:73
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: enum.cc:83
int m_value
The stored integer value.
Definition: enum.h:75
Ptr< AttributeValue > Copy() const override
Definition: enum.cc:66
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:66
#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:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
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:488