A Discrete-Event Network Simulator
API
enum.h
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 #ifndef ENUM_VALUE_H
21 #define ENUM_VALUE_H
22 
23 #include "attribute.h"
25 #include <list>
26 
33 namespace ns3 {
34 
35 // Additional docs for class EnumValue:
54 class EnumValue : public AttributeValue
55 {
56 public:
57  EnumValue ();
63  EnumValue (int value);
64  void Set (int value);
65  int Get (void) const;
66  template <typename T>
67  bool GetAccessor (T & value) const;
68 
69  virtual Ptr<AttributeValue> Copy (void) const;
70  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
71  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
72 
73 private:
74  int m_value;
75 };
76 
77 template <typename T>
78 bool
79 EnumValue::GetAccessor (T &value) const
80 {
81  value = T (m_value);
82  return true;
83 }
84 
86 {
87 public:
88  EnumChecker ();
89 
95  void AddDefault (int value, std::string name);
101  void Add (int value, std::string name);
102 
108  std::string GetName (int value) const;
109 
115  int GetValue (const std::string name) const;
116 
117  // Inherited
118  virtual bool Check (const AttributeValue &value) const;
119  virtual std::string GetValueTypeName (void) const;
120  virtual bool HasUnderlyingTypeInformation (void) const;
121  virtual std::string GetUnderlyingTypeInformation (void) const;
122  virtual Ptr<AttributeValue> Create (void) const;
123  virtual bool Copy (const AttributeValue &src, AttributeValue &dst) const;
124 
125 private:
127  typedef std::pair<int,std::string> Value;
129  typedef std::list<Value> ValueSet;
132 };
133 
134 template <typename T1>
136 
137 template <typename T1, typename T2>
139 
159 template <typename... Ts>
161 MakeEnumChecker (int v, std::string n, Ts... args)
162 {
163  Ptr<EnumChecker> checker = Create<EnumChecker> ();
164  checker->AddDefault (v, n);
165  return MakeEnumChecker (checker, args...);
166 }
167 
179 template <typename... Ts>
180 Ptr<const AttributeChecker>
181 MakeEnumChecker (Ptr<EnumChecker> checker, int v, std::string n, Ts... args)
182 {
183  checker->Add (v, n);
184  return MakeEnumChecker (checker, args...);
185 }
186 
193 // inline to allow tail call optimization
194 inline
195 Ptr<const AttributeChecker>
197 {
198  return checker;
199 }
200 
201 
202 template <typename T1>
204 {
205  return MakeAccessorHelper<EnumValue> (a1);
206 }
207 
208 template <typename T1, typename T2>
210 {
211  return MakeAccessorHelper<EnumValue> (a1, a2);
212 }
213 
214 } // namespace ns3
215 
216 #endif /* ENUM_VALUE_H */
int GetValue(const std::string name) const
Get the enum value by name.
Definition: enum.cc:110
Represent the type of an attribute.
Definition: attribute.h:166
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
virtual std::string GetUnderlyingTypeInformation(void) const
Definition: enum.cc:145
virtual Ptr< AttributeValue > Create(void) const
Definition: enum.cc:158
Hold a value for an Attribute.
Definition: attribute.h:68
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
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:203
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
bool GetAccessor(T &value) const
Access the Enum value as type T.
Definition: enum.h:79
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
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
ns3::MakeAccessorHelper declarations and template implementations.
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:161
virtual Ptr< AttributeValue > Copy(void) const
Definition: enum.cc:59
std::list< Value > ValueSet
Type of container for storing Enum values and symbol names.
Definition: enum.h:129
int Get(void) const
Definition: enum.cc:53