A Discrete-Event Network Simulator
API
pair.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Caliola Engineering, LLC.
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  * Author: Jared Dulmage <jared.dulmage@caliola.com>
18  */
19 
20 #ifndef PAIR_H
21 #define PAIR_H
22 
23 #include "attribute-helper.h"
24 #include "string.h"
25 
26 #include <sstream>
27 #include <type_traits>
28 #include <typeinfo> // typeid
29 #include <utility>
30 
31 namespace ns3
32 {
33 
42 template <class A, class B>
43 std::ostream&
44 operator<<(std::ostream& os, const std::pair<A, B>& p)
45 {
46  os << "(" << p.first << "," << p.second << ")";
47  return os;
48 }
49 
50 // Doxygen for this class is auto-generated by
51 // utils/print-introspected-doxygen.h
52 
54 template <class A, class B>
55 class PairValue : public AttributeValue
56 {
57  public:
59  typedef std::pair<Ptr<A>, Ptr<B>> value_type;
61  typedef typename std::invoke_result_t<decltype(&A::Get), A> first_type;
63  typedef typename std::invoke_result_t<decltype(&B::Get), B> second_type;
65  typedef typename std::pair<first_type, second_type> result_type;
66 
67  PairValue();
68 
74  PairValue(const result_type& value); // "import" constructor
75 
76  // Inherited
77  Ptr<AttributeValue> Copy() const override;
78  bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
79  std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
80 
88  result_type Get() const;
89  /* Documented by print-introspected-doxygen.cc */
90  void Set(const result_type& value);
91 
92  template <typename T>
93  bool GetAccessor(T& value) const;
94 
95  private:
97 };
98 
100 {
101  public:
103  typedef std::pair<Ptr<const AttributeChecker>, Ptr<const AttributeChecker>> checker_pair_type;
104 
111  virtual void SetCheckers(Ptr<const AttributeChecker> firstchecker,
112  Ptr<const AttributeChecker> secondchecker) = 0;
113 
119  virtual checker_pair_type GetCheckers() const = 0;
120 };
121 
130 template <class A, class B>
132 
143 template <class A, class B>
145  Ptr<const AttributeChecker> secondchecker);
146 
152 template <class A, class B>
154 
155 template <typename A, typename B, typename T1>
157 
158 } // namespace ns3
159 
160 /*****************************************************************************
161  * Implementation below
162  *****************************************************************************/
163 
164 namespace ns3
165 {
166 
167 // This internal class defines templated PairChecker class that is instantiated
168 // in MakePairChecker. The non-templated base ns3::PairChecker is returned in that
169 // function. This is the same pattern as ObjectPtrContainer.
170 namespace internal
171 {
172 
177 template <class A, class B>
179 {
180  public:
182  PairChecker();
189  Ptr<const AttributeChecker> secondchecker);
190  void SetCheckers(Ptr<const AttributeChecker> firstchecker,
191  Ptr<const AttributeChecker> secondchecker) override;
192  typename ns3::PairChecker::checker_pair_type GetCheckers() const override;
193 
194  private:
199 };
200 
201 template <class A, class B>
203  : m_firstchecker(nullptr),
204  m_secondchecker(nullptr)
205 {
206 }
207 
208 template <class A, class B>
210  Ptr<const AttributeChecker> secondchecker)
211  : m_firstchecker(firstchecker),
212  m_secondchecker(secondchecker)
213 {
214 }
215 
216 template <class A, class B>
217 void
219  Ptr<const AttributeChecker> secondchecker)
220 {
221  m_firstchecker = firstchecker;
222  m_secondchecker = secondchecker;
223 }
224 
225 template <class A, class B>
228 {
229  return std::make_pair(m_firstchecker, m_secondchecker);
230 }
231 
232 } // namespace internal
233 
234 template <class A, class B>
237 {
238  return MakePairChecker<A, B>();
239 }
240 
241 template <class A, class B>
242 Ptr<const AttributeChecker>
244 {
245  auto checker = MakePairChecker<A, B>();
246  auto acchecker = DynamicCast<PairChecker>(checker);
247  acchecker->SetCheckers(firstchecker, secondchecker);
248  return checker;
249 }
250 
251 template <class A, class B>
252 Ptr<AttributeChecker>
254 {
255  std::string pairName;
256  std::string underlyingType;
257  typedef PairValue<A, B> T;
258  std::string first_type_name = typeid(typename T::value_type::first_type).name();
259  std::string second_type_name = typeid(typename T::value_type::second_type).name();
260  {
261  std::ostringstream oss;
262  oss << "ns3::PairValue<" << first_type_name << ", " << second_type_name << ">";
263  pairName = oss.str();
264  }
265 
266  {
267  std::ostringstream oss;
268  oss << typeid(typename T::value_type).name();
269  underlyingType = oss.str();
270  }
271 
272  return MakeSimpleAttributeChecker<T, internal::PairChecker<A, B>>(pairName, underlyingType);
273 }
274 
275 template <class A, class B>
277  : m_value(std::make_pair(Create<A>(), Create<B>()))
278 {
279 }
280 
281 template <class A, class B>
283 {
284  Set(value);
285 }
286 
287 template <class A, class B>
288 Ptr<AttributeValue>
290 {
291  auto p = Create<PairValue<A, B>>();
292  // deep copy if non-null
293  if (m_value.first)
294  {
295  p->m_value = std::make_pair(DynamicCast<A>(m_value.first->Copy()),
296  DynamicCast<B>(m_value.second->Copy()));
297  }
298  return p;
299 }
300 
301 template <class A, class B>
302 bool
304 {
305  auto pchecker = DynamicCast<const PairChecker>(checker);
306  if (!pchecker)
307  {
308  return false;
309  }
310 
311  std::istringstream iss(value); // copies value
312  iss >> value;
313  auto first = pchecker->GetCheckers().first->CreateValidValue(StringValue(value));
314  if (!first)
315  {
316  return false;
317  }
318 
319  auto firstattr = DynamicCast<A>(first);
320  if (!firstattr)
321  {
322  return false;
323  }
324 
325  iss >> value;
326  auto second = pchecker->GetCheckers().second->CreateValidValue(StringValue(value));
327  if (!second)
328  {
329  return false;
330  }
331 
332  auto secondattr = DynamicCast<B>(second);
333  if (!secondattr)
334  {
335  return false;
336  }
337 
338  m_value = std::make_pair(firstattr, secondattr);
339  return true;
340 }
341 
342 template <class A, class B>
343 std::string
345 {
346  std::ostringstream oss;
347  oss << m_value.first->SerializeToString(checker);
348  oss << " ";
349  oss << m_value.second->SerializeToString(checker);
350 
351  return oss.str();
352 }
353 
354 template <class A, class B>
357 {
358  return std::make_pair(m_value.first->Get(), m_value.second->Get());
359 }
360 
361 template <class A, class B>
362 void
364 {
365  m_value = std::make_pair(Create<A>(value.first), Create<B>(value.second));
366 }
367 
368 template <class A, class B>
369 template <typename T>
370 bool
372 {
373  value = T(Get());
374  return true;
375 }
376 
385 template <typename A, typename B, typename T1>
388 {
389  return MakeAccessorHelper<PairValue<A, B>>(a1);
390 }
391 
392 } // namespace ns3
393 
394 #endif // PAIR_H
Attribute helper (ATTRIBUTE_ )macros definition.
Represent the type of an attribute.
Definition: attribute.h:168
Hold a value for an Attribute.
Definition: attribute.h:70
virtual void SetCheckers(Ptr< const AttributeChecker > firstchecker, Ptr< const AttributeChecker > secondchecker)=0
Set the individual AttributeChecker for each pair entry.
std::pair< Ptr< const AttributeChecker >, Ptr< const AttributeChecker > > checker_pair_type
Type holding an AttributeChecker for each member of a pair.
Definition: pair.h:103
virtual checker_pair_type GetCheckers() const =0
Get the pair of checkers for each pair entry.
Hold objects of type std::pair<A, B>.
Definition: pair.h:56
PairValue(const result_type &value)
Construct this PairValue from a std::pair.
std::pair< first_type, second_type > result_type
Type returned by Get or passed in Set.
Definition: pair.h:65
result_type Get() const
Get the stored value as a std::pair.
Definition: pair.h:356
bool GetAccessor(T &value) const
Definition: pair.h:371
std::invoke_result_t< decltype(&A::Get), A > first_type
Type of abscissa (first entry of pair).
Definition: pair.h:61
std::pair< Ptr< A >, Ptr< B > > value_type
Type of value stored in the PairValue.
Definition: pair.h:59
value_type m_value
Definition: pair.h:96
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: pair.h:344
void Set(const result_type &value)
Definition: pair.h:363
std::invoke_result_t< decltype(&B::Get), B > second_type
Type of ordinal (second entry of pair).
Definition: pair.h:63
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: pair.h:303
Ptr< AttributeValue > Copy() const override
Definition: pair.h:289
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Hold variables of type string.
Definition: string.h:56
Internal checker class templated to each AttributeChecker for each entry in the pair.
Definition: pair.h:179
Ptr< const AttributeChecker > m_secondchecker
The second checker.
Definition: pair.h:198
Ptr< const AttributeChecker > m_firstchecker
The first checker.
Definition: pair.h:196
void SetCheckers(Ptr< const AttributeChecker > firstchecker, Ptr< const AttributeChecker > secondchecker) override
Set the individual AttributeChecker for each pair entry.
Definition: pair.h:218
ns3::PairChecker::checker_pair_type GetCheckers() const override
Get the pair of checkers for each pair entry.
Definition: pair.h:227
PairChecker()
Default c'tor.
Definition: pair.h:202
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:876
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:442
Definition: first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakePairAccessor(T1 a1)
Create an AttributeAccessor for std::pair<>.
Definition: pair.h:387
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Ptr< AttributeChecker > MakePairChecker(const PairValue< A, B > &value)
Make a PairChecker from a PairValue.
Definition: pair.h:236
Definition: second.py:1
ns3::StringValue attribute value declarations.