A Discrete-Event Network Simulator
API
integer.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 "integer.h"
20 
21 #include "fatal-error.h"
22 #include "log.h"
23 
24 #include <sstream>
25 
32 namespace ns3
33 {
34 
35 NS_LOG_COMPONENT_DEFINE("Integer");
36 
38 
39 namespace internal
40 {
41 
52 MakeIntegerChecker(int64_t min, int64_t max, std::string name)
53 {
54  NS_LOG_FUNCTION(min << max << name);
55 
56  struct IntegerChecker : public AttributeChecker
57  {
58  IntegerChecker(int64_t minValue, int64_t maxValue, std::string name)
59  : m_minValue(minValue),
60  m_maxValue(maxValue),
61  m_name(name)
62  {
63  }
64 
65  bool Check(const AttributeValue& value) const override
66  {
67  NS_LOG_FUNCTION(&value);
68  const auto v = dynamic_cast<const IntegerValue*>(&value);
69  if (v == nullptr)
70  {
71  return false;
72  }
73  return v->Get() >= m_minValue && v->Get() <= m_maxValue;
74  }
75 
76  std::string GetValueTypeName() const override
77  {
79  return "ns3::IntegerValue";
80  }
81 
82  bool HasUnderlyingTypeInformation() const override
83  {
85  return true;
86  }
87 
88  std::string GetUnderlyingTypeInformation() const override
89  {
91  std::ostringstream oss;
92  oss << m_name << " " << m_minValue << ":" << m_maxValue;
93  return oss.str();
94  }
95 
96  Ptr<AttributeValue> Create() const override
97  {
99  return ns3::Create<IntegerValue>();
100  }
101 
102  bool Copy(const AttributeValue& src, AttributeValue& dst) const override
103  {
104  NS_LOG_FUNCTION(&src << &dst);
105  const auto source = dynamic_cast<const IntegerValue*>(&src);
106  auto destination = dynamic_cast<IntegerValue*>(&dst);
107  if (source == nullptr || destination == nullptr)
108  {
109  return false;
110  }
111  *destination = *source;
112  return true;
113  }
114 
115  int64_t m_minValue;
116  int64_t m_maxValue;
117  std::string m_name;
118  }* checker = new IntegerChecker(min, max, name);
119 
120  return Ptr<AttributeChecker>(checker, false);
121 }
122 
123 } // namespace internal
124 
125 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
Represent the type of an attribute.
Definition: attribute.h:168
Hold a value for an Attribute.
Definition: attribute.h:70
Hold a signed integer type.
Definition: integer.h:45
int64_t Get() const
Definition: integer.cc:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
NS_FATAL_x macro definitions.
#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, name)
Define the class methods belonging to the attribute value class nameValue of the underlying class typ...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#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:442
ns3::IntegerValue attribute value declarations and template implementations.
Debug message logging.
Ptr< const AttributeChecker > MakeIntegerChecker(int64_t min, int64_t max, std::string name)
Make an Integer attribute checker with embedded numeric type name.
Definition: integer.cc:52
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:610