A Discrete-Event Network Simulator
API
enum.h
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 #ifndef ENUM_VALUE_H
20 #define ENUM_VALUE_H
21 
23 #include "attribute.h"
24 
25 #include <list>
26 
33 namespace ns3
34 {
35 
36 // Additional docs for class EnumValue:
55 class EnumValue : public AttributeValue
56 {
57  public:
58  EnumValue();
64  EnumValue(int value);
65  void Set(int value);
66  int Get() const;
67  template <typename T>
68  bool GetAccessor(T& value) const;
69 
70  Ptr<AttributeValue> Copy() const override;
71  std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
72  bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
73 
74  private:
75  int m_value;
76 };
77 
78 template <typename T>
79 bool
81 {
82  value = T(m_value);
83  return true;
84 }
85 
87 {
88  public:
89  EnumChecker();
90 
96  void AddDefault(int value, std::string name);
102  void Add(int value, std::string name);
103 
109  std::string GetName(int value) const;
110 
116  int GetValue(const std::string name) const;
117 
118  // Inherited
119  bool Check(const AttributeValue& value) const override;
120  std::string GetValueTypeName() const override;
121  bool HasUnderlyingTypeInformation() const override;
122  std::string GetUnderlyingTypeInformation() const override;
123  Ptr<AttributeValue> Create() const override;
124  bool Copy(const AttributeValue& src, AttributeValue& dst) const override;
125 
126  private:
128  typedef std::pair<int, std::string> Value;
130  typedef std::list<Value> ValueSet;
133 };
134 
135 template <typename T1>
137 
138 template <typename T1, typename T2>
140 
161 template <typename... Ts>
163 MakeEnumChecker(int v, std::string n, Ts... args)
164 {
165  Ptr<EnumChecker> checker = Create<EnumChecker>();
166  checker->AddDefault(v, n);
167  return MakeEnumChecker(checker, args...);
168 }
169 
182 template <typename... Ts>
183 Ptr<const AttributeChecker>
184 MakeEnumChecker(Ptr<EnumChecker> checker, int v, std::string n, Ts... args)
185 {
186  checker->Add(v, n);
187  return MakeEnumChecker(checker, args...);
188 }
189 
196 // inline to allow tail call optimization
197 inline Ptr<const AttributeChecker>
199 {
200  return checker;
201 }
202 
203 template <typename T1>
204 Ptr<const AttributeAccessor>
206 {
207  return MakeAccessorHelper<EnumValue>(a1);
208 }
209 
210 template <typename T1, typename T2>
211 Ptr<const AttributeAccessor>
212 MakeEnumAccessor(T1 a1, T2 a2)
213 {
214  return MakeAccessorHelper<EnumValue>(a1, a2);
215 }
216 
217 } // namespace ns3
218 
219 #endif /* ENUM_VALUE_H */
ns3::MakeAccessorHelper declarations and template implementations.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Represent the type of an attribute.
Definition: attribute.h:168
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
bool Copy(const AttributeValue &src, AttributeValue &dst) const override
Copy the source to the destination.
Definition: enum.cc:201
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
Ptr< AttributeValue > Create() const override
Definition: enum.cc:194
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
std::string GetUnderlyingTypeInformation() const override
Definition: enum.cc:180
void AddDefault(int value, std::string name)
Add a default value.
Definition: enum.cc:98
bool Check(const AttributeValue &value) const override
Definition: enum.cc:150
bool HasUnderlyingTypeInformation() const override
Definition: enum.cc:173
std::list< Value > ValueSet
Type of container for storing Enum values and symbol names.
Definition: enum.h:130
std::string GetValueTypeName() const override
Definition: enum.cc:166
Hold variables of type enum.
Definition: enum.h:56
bool GetAccessor(T &value) const
Access the Enum value as type T.
Definition: enum.h:80
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
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: enum.h:205
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:163
value
Definition: second.py:41