A Discrete-Event Network Simulator
API
attribute.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 "attribute.h"
20 
21 #include "fatal-error.h"
22 #include "log.h"
23 #include "string.h"
24 
25 #include <sstream>
26 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("AttributeValue");
38 
40 {
41 }
42 
44 {
45 }
46 
48 {
49 }
50 
52 {
53 }
54 
56 {
57 }
58 
60 {
61 }
62 
65 {
66  NS_LOG_FUNCTION(this << &value);
67  if (Check(value))
68  {
69  return value.Copy();
70  }
71  // attempt to convert to string.
72  const auto str = dynamic_cast<const StringValue*>(&value);
73  if (str == nullptr)
74  {
75  return nullptr;
76  }
77  // attempt to convert back to value.
79  bool ok = v->DeserializeFromString(str->Get(), this);
80  if (!ok)
81  {
82  return nullptr;
83  }
84  ok = Check(*v);
85  if (!ok)
86  {
87  return nullptr;
88  }
89  return v;
90 }
91 
93 {
94  NS_LOG_FUNCTION(this);
95 }
96 
99 {
100  NS_LOG_FUNCTION(this);
101  return Create<EmptyAttributeValue>();
102 }
103 
104 std::string
106 {
107  NS_LOG_FUNCTION(this << checker);
108  return "";
109 }
110 
111 bool
113 {
114  NS_LOG_FUNCTION(this << value << checker);
115  return true;
116 }
117 
120 {
121 }
122 
124 {
125 }
126 
127 bool
128 EmptyAttributeAccessor::Set(ObjectBase* object [[maybe_unused]],
129  const AttributeValue& value [[maybe_unused]]) const
130 {
131  return true;
132 }
133 
134 bool
135 EmptyAttributeAccessor::Get(const ObjectBase* object [[maybe_unused]],
136  AttributeValue& attribute [[maybe_unused]]) const
137 {
138  return true;
139 }
140 
141 bool
143 {
144  return false;
145 }
146 
147 bool
149 {
150  return false;
151 }
152 
154  : AttributeChecker()
155 {
156 }
157 
159 {
160 }
161 
162 bool
163 EmptyAttributeChecker::Check(const AttributeValue& value [[maybe_unused]]) const
164 {
165  return true;
166 }
167 
168 std::string
170 {
171  return "EmptyAttribute";
172 }
173 
174 bool
176 {
177  return false;
178 }
179 
180 std::string
182 {
183  return "";
184 }
185 
188 {
189  static EmptyAttributeValue t;
190  return Ptr<AttributeValue>(&t, false);
191 }
192 
193 bool
194 EmptyAttributeChecker::Copy(const AttributeValue& source [[maybe_unused]],
195  AttributeValue& destination [[maybe_unused]]) const
196 {
197  return true;
198 }
199 
200 } // namespace ns3
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
allow setting and getting the value of an attribute.
Definition: attribute.h:116
virtual ~AttributeAccessor()
Definition: attribute.cc:51
Represent the type of an attribute.
Definition: attribute.h:168
virtual ~AttributeChecker()
Definition: attribute.cc:59
virtual bool Check(const AttributeValue &value) const =0
Ptr< AttributeValue > CreateValidValue(const AttributeValue &value) const
Create a valid value from the argument value, or reinterpret the argument as a string.
Definition: attribute.cc:64
virtual Ptr< AttributeValue > Create() const =0
Hold a value for an Attribute.
Definition: attribute.h:70
virtual ~AttributeValue()
Definition: attribute.cc:43
bool Get(const ObjectBase *object, AttributeValue &attribute) const override
Definition: attribute.cc:135
bool Set(ObjectBase *object, const AttributeValue &value) const override
Definition: attribute.cc:128
~EmptyAttributeAccessor() override
Definition: attribute.cc:123
bool HasSetter() const override
Definition: attribute.cc:148
bool HasGetter() const override
Definition: attribute.cc:142
std::string GetValueTypeName() const override
Definition: attribute.cc:169
bool Check(const AttributeValue &value) const override
Definition: attribute.cc:163
Ptr< AttributeValue > Create() const override
Definition: attribute.cc:187
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: attribute.cc:194
bool HasUnderlyingTypeInformation() const override
Definition: attribute.cc:175
~EmptyAttributeChecker() override
Definition: attribute.cc:158
std::string GetUnderlyingTypeInformation() const override
Definition: attribute.cc:181
A class for an empty attribute value.
Definition: attribute.h:234
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: attribute.cc:105
EmptyAttributeValue()
Default constructor.
Definition: attribute.cc:92
Ptr< AttributeValue > Copy() const override
Definition: attribute.cc:98
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: attribute.cc:112
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Hold variables of type string.
Definition: string.h:56
NS_FATAL_x macro definitions.
#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.
ns3::StringValue attribute value declarations.