A Discrete-Event Network Simulator
API
pair.h
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018 Caliola Engineering, LLC.
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  * Author: Jared Dulmage <jared.dulmage@caliola.com>
19  */
20 
21 #ifndef PAIR_H
22 #define PAIR_H
23 
24 #include <ns3/attribute-helper.h>
25 #include <ns3/string.h>
26 
27 #include <sstream>
28 #include <typeinfo> // typeid
29 #include <type_traits>
30 #include <utility>
31 
32 namespace ns3 {
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 (void) const;
78  bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
79  std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
80 
88  result_type Get (void) 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, Ptr<const AttributeChecker> secondchecker) = 0;
112 
118  virtual checker_pair_type GetCheckers (void) const = 0;
119 };
120 
129 template <class A, class B>
131 MakePairChecker (const PairValue<A, B> &value);
132 
143 template <class A, class B>
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 // This internal class defines templated PairChecker class that is instantiated
167 // in MakePairChecker. The non-templated base ns3::PairChecker is returned in that
168 // function. This is the same pattern as ObjectPtrContainer.
169 namespace internal {
170 
175 template <class A, class B>
177 {
178 public:
180  PairChecker (void);
187  void SetCheckers (Ptr<const AttributeChecker> firstchecker, Ptr<const AttributeChecker> secondchecker);
188  typename ns3::PairChecker::checker_pair_type GetCheckers (void) const;
189 
190 private:
195 };
196 
197 template <class A, class B>
199  : m_firstchecker (0),
200  m_secondchecker (0)
201 {}
202 
203 template <class A, class B>
205  : m_firstchecker (firstchecker),
206  m_secondchecker (secondchecker)
207 {}
208 
209 template <class A, class B>
210 void
212 {
213  m_firstchecker = firstchecker;
214  m_secondchecker = secondchecker;
215 }
216 
217 template <class A, class B>
220 {
221  return std::make_pair (m_firstchecker, m_secondchecker);
222 }
223 
224 } // namespace internal
225 
226 template <class A, class B>
229 {
230  return MakePairChecker <A, B> ();
231 }
232 
233 template <class A, class B>
234 Ptr<const AttributeChecker>
236 {
237  auto checker = MakePairChecker <A, B> ();
238  auto acchecker = DynamicCast<PairChecker> (checker);
239  acchecker->SetCheckers (firstchecker, secondchecker);
240  return checker;
241 }
242 
243 template <class A, class B>
244 Ptr<AttributeChecker>
246 {
247  std::string pairName;
248  std::string underlyingType;
249  typedef PairValue<A, B> T;
250  std::string first_type_name = typeid (typename T::value_type::first_type).name ();
251  std::string second_type_name = typeid (typename T::value_type::second_type).name ();
252  {
253  std::ostringstream oss;
254  oss << "ns3::PairValue<" << first_type_name << ", " << second_type_name << ">";
255  pairName = oss.str ();
256  }
257 
258  {
259  std::ostringstream oss;
260  oss << typeid (typename T::value_type).name ();
261  underlyingType = oss.str ();
262  }
263 
264  return MakeSimpleAttributeChecker<T, internal::PairChecker<A, B> > (pairName, underlyingType);
265 }
266 
267 template <class A, class B>
269  : m_value (std::make_pair (Create <A> (), Create <B> ()))
270 {}
271 
272 template <class A, class B>
274 {
275  Set (value);
276 }
277 
278 template <class A, class B>
279 Ptr<AttributeValue>
281 {
282  auto p = Create <PairValue <A, B> > ();
283  // deep copy if non-null
284  if (m_value.first)
285  p->m_value = std::make_pair (DynamicCast<A> (m_value.first->Copy ()),
286  DynamicCast<B> (m_value.second->Copy ()));
287  return p;
288 }
289 
290 template <class A, class B>
291 bool
293 {
294  auto pchecker = DynamicCast<const PairChecker> (checker);
295  if (!pchecker) return false;
296 
297  std::istringstream iss (value); // copies value
298  iss >> value;
299  auto first = pchecker->GetCheckers ().first->CreateValidValue (StringValue (value));
300  if (!first) return false;
301 
302  auto firstattr = DynamicCast <A> (first);
303  if (!firstattr) return false;
304 
305  iss >> value;
306  auto second = pchecker->GetCheckers ().second->CreateValidValue (StringValue (value));
307  if (!second) return false;
308 
309  auto secondattr = DynamicCast <B> (second);
310  if (!secondattr) return false;
311 
312  m_value = std::make_pair (firstattr, secondattr);
313  return true;
314 }
315 
316 template <class A, class B>
317 std::string
319 {
320  std::ostringstream oss;
321  oss << m_value.first->SerializeToString (checker);
322  oss << " ";
323  oss << m_value.second->SerializeToString (checker);
324 
325  return oss.str ();
326 }
327 
328 template <class A, class B>
331 {
332  return std::make_pair (m_value.first->Get (), m_value.second->Get ());
333 }
334 
335 template <class A, class B>
336 void
338 {
339  m_value = std::make_pair (Create <A> (value.first), Create <B> (value.second));
340 }
341 
342 template <class A, class B>
343 template <typename T>
344 bool
346 {
347  value = T (Get ());
348  return true;
349 }
350 
359 template <typename A, typename B, typename T1>
361 {
362  return MakeAccessorHelper<PairValue<A, B> > (a1);
363 }
364 
365 } // namespace ns3
366 
367 #endif // PAIR_H
Represent the type of an attribute.
Definition: attribute.h:167
Hold a value for an Attribute.
Definition: attribute.h:69
AttributeChecker implementation for PairValue.
Definition: pair.h:100
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(void) 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
bool GetAccessor(T &value) const
Access the Pair value as type T.
Definition: pair.h:345
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
void Set(const result_type &value)
Set the value.
Definition: pair.h:337
value_type m_value
The stored Pair instance.
Definition: pair.h:96
result_type Get(void) const
Get the stored value as a std::pair.
Definition: pair.h:330
std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: pair.h:318
Ptr< AttributeValue > Copy(void) const
Definition: pair.h:280
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: pair.h:292
std::invoke_result_t< decltype(&B::Get), B > second_type
Type of ordinal (second entry of pair).
Definition: pair.h:63
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Hold variables of type string.
Definition: string.h:41
Internal checker class templated to each AttributeChecker for each entry in the pair.
Definition: pair.h:177
ns3::PairChecker::checker_pair_type GetCheckers(void) const
Get the pair of checkers for each pair entry.
Definition: pair.h:219
Ptr< const AttributeChecker > m_secondchecker
The second checker.
Definition: pair.h:194
Ptr< const AttributeChecker > m_firstchecker
The first checker.
Definition: pair.h:192
PairChecker(void)
Default c'tor.
Definition: pair.h:198
void SetCheckers(Ptr< const AttributeChecker > firstchecker, Ptr< const AttributeChecker > secondchecker)
Set the individual AttributeChecker for each pair entry.
Definition: pair.h:211
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:409
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:360
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
Ptr< AttributeChecker > MakePairChecker(const PairValue< A, B > &value)
Make a PairChecker from a PairValue.
Definition: pair.h:228
Definition: second.py:1