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 
34 template <class A, class B>
35 std::ostream &
36 operator << (std::ostream &os, const std::pair<A, B> &p)
37 {
38  os << "(" << p.first << "," << p.second << ")";
39  return os;
40 }
41 
42 // Doxygen for this class is auto-generated by
43 // utils/print-introspected-doxygen.h
44 
46 template <class A, class B>
47 class PairValue : public AttributeValue
48 {
49 public:
51  typedef std::pair<Ptr<A>, Ptr<B> > value_type;
53  typedef typename std::result_of<decltype(&A::Get)(A)>::type first_type;
55  typedef typename std::result_of<decltype(&B::Get)(B)>::type second_type;
57  typedef typename std::pair<first_type, second_type> result_type;
58 
59  PairValue ();
60 
66  PairValue (const result_type &value); // "import" constructor
67 
68  // Inherited
69  Ptr<AttributeValue> Copy (void) const;
70  bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
71  std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
72 
80  result_type Get (void) const;
85  void Set (const result_type &value);
86 
87  template <typename T>
88  bool GetAccessor (T &value) const;
89 
90 private:
92 };
93 
95 {
96 public:
97  typedef std::pair<Ptr<const AttributeChecker>, Ptr<const AttributeChecker> > checker_pair_type;
98 
105  virtual void SetCheckers (Ptr<const AttributeChecker> firstchecker, Ptr<const AttributeChecker> secondchecker) = 0;
106 
112  virtual checker_pair_type GetCheckers (void) const = 0;
113 };
114 
123 template <class A, class B>
125 MakePairChecker (const PairValue<A, B> &value);
126 
137 template <class A, class B>
140 
146 template <class A, class B>
148 
149 template <typename A, typename B, typename T1>
151 
152 } // namespace ns3
153 
154 /*****************************************************************************
155  * Implementation below
156  *****************************************************************************/
157 
158 namespace ns3 {
159 
160 // This internal class defines templated PairChecker class that is instantiated
161 // in MakePairChecker. The non-templated base ns3::PairChecker is returned in that
162 // function. This is the same pattern as ObjectPtrContainer.
163 namespace internal {
164 
169 template <class A, class B>
171 {
172 public:
173  PairChecker (void);
175  void SetCheckers (Ptr<const AttributeChecker> firstchecker, Ptr<const AttributeChecker> secondchecker);
176  typename ns3::PairChecker::checker_pair_type GetCheckers (void) const;
177 
178 private:
181 };
182 
183 template <class A, class B>
185  : m_firstchecker (0),
186  m_secondchecker (0)
187 {}
188 
189 template <class A, class B>
191  : m_firstchecker (firstchecker),
192  m_secondchecker (secondchecker)
193 {}
194 
195 template <class A, class B>
196 void
198 {
199  m_firstchecker = firstchecker;
200  m_secondchecker = secondchecker;
201 }
202 
203 template <class A, class B>
206 {
207  return std::make_pair (m_firstchecker, m_secondchecker);
208 }
209 
210 } // namespace internal
211 
212 template <class A, class B>
215 {
216  return MakePairChecker <A, B> ();
217 }
218 
219 template <class A, class B>
220 Ptr<const AttributeChecker>
222 {
223  auto checker = MakePairChecker <A, B> ();
224  auto acchecker = DynamicCast<PairChecker> (checker);
225  acchecker->SetCheckers (firstchecker, secondchecker);
226  return checker;
227 }
228 
229 template <class A, class B>
230 Ptr<AttributeChecker>
232 {
233  std::string pairName;
234  std::string underlyingType;
235  typedef PairValue<A, B> T;
236  std::string first_type_name = typeid (typename T::value_type::first_type).name ();
237  std::string second_type_name = typeid (typename T::value_type::second_type).name ();
238  {
239  std::ostringstream oss;
240  oss << "ns3::PairValue<" << first_type_name << ", " << second_type_name << ">";
241  pairName = oss.str ();
242  }
243 
244  {
245  std::ostringstream oss;
246  oss << typeid (typename T::value_type).name ();
247  underlyingType = oss.str ();
248  }
249 
250  return MakeSimpleAttributeChecker<T, internal::PairChecker<A, B> > (pairName, underlyingType);
251 }
252 
253 template <class A, class B>
255  : m_value (std::make_pair (Create <A> (), Create <B> ()))
256 {}
257 
258 template <class A, class B>
260 {
261  Set (value);
262 }
263 
264 template <class A, class B>
265 Ptr<AttributeValue>
267 {
268  auto p = Create <PairValue <A, B> > ();
269  // deep copy if non-null
270  if (m_value.first)
271  p->m_value = std::make_pair (DynamicCast<A> (m_value.first->Copy ()),
272  DynamicCast<B> (m_value.second->Copy ()));
273  return p;
274 }
275 
276 template <class A, class B>
277 bool
279 {
280  auto pchecker = DynamicCast<const PairChecker> (checker);
281  if (!pchecker) return false;
282 
283  std::istringstream iss (value); // copies value
284  iss >> value;
285  auto first = pchecker->GetCheckers ().first->CreateValidValue (StringValue (value));
286  if (!first) return false;
287 
288  auto firstattr = DynamicCast <A> (first);
289  if (!firstattr) return false;
290 
291  iss >> value;
292  auto second = pchecker->GetCheckers ().second->CreateValidValue (StringValue (value));
293  if (!second) return false;
294 
295  auto secondattr = DynamicCast <B> (second);
296  if (!secondattr) return false;
297 
298  m_value = std::make_pair (firstattr, secondattr);
299  return true;
300 }
301 
302 template <class A, class B>
303 std::string
305 {
306  std::ostringstream oss;
307  oss << m_value.first->SerializeToString (checker);
308  oss << " ";
309  oss << m_value.second->SerializeToString (checker);
310 
311  return oss.str ();
312 }
313 
314 template <class A, class B>
317 {
318  return std::make_pair (m_value.first->Get (), m_value.second->Get ());
319 }
320 
321 template <class A, class B>
322 void
324 {
325  m_value = std::make_pair (Create <A> (value.first), Create <B> (value.second));
326 }
327 
328 template <class A, class B>
329 template <typename T>
330 bool
332 {
333  value = T (Get ());
334  return true;
335 }
336 
337 template <typename A, typename B, typename T1>
339 {
340  return MakeAccessorHelper<PairValue<A, B> > (a1);
341 }
342 
343 } // namespace ns3
344 
345 #endif // PAIR_H
std::result_of< decltype(&B::Get)(B)>::type second_type
Type of ordinal (second entry of pair).
Definition: pair.h:55
Represent the type of an attribute.
Definition: attribute.h:166
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Definition: second.py:1
Hold variables of type string.
Definition: string.h:41
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
Hold a value for an Attribute.
Definition: attribute.h:68
std::result_of< decltype(&A::Get)(A)>::type first_type
Type of abscissa (first entry of pair).
Definition: pair.h:53
STL namespace.
Ptr< const AttributeChecker > m_firstchecker
Definition: pair.h:179
void Set(const result_type &value)
Set the stored value.
Definition: pair.h:323
value_type m_value
The stored Pair instance.
Definition: pair.h:91
std::pair< Ptr< const AttributeChecker >, Ptr< const AttributeChecker > > checker_pair_type
Definition: pair.h:97
virtual void SetCheckers(Ptr< const AttributeChecker > firstchecker, Ptr< const AttributeChecker > secondchecker)=0
Set the individual AttributeChecker for each pair entry.
std::pair< first_type, second_type > result_type
Type returned by Get or passed in Set.
Definition: pair.h:57
Ptr< T > Create(Ts... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Definition: ptr.h:405
Every class exported by the ns3 library is enclosed in the ns3 namespace.
AttributeChecker implementation for PairValue.
Definition: pair.h:94
std::pair< Ptr< A >, Ptr< B > > value_type
Type of value stored in the PairValue.
Definition: pair.h:51
void SetCheckers(Ptr< const AttributeChecker > firstchecker, Ptr< const AttributeChecker > secondchecker)
Set the individual AttributeChecker for each pair entry.
Definition: pair.h:197
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Definition: pair.h:278
virtual checker_pair_type GetCheckers(void) const =0
Get the pair of checkers for each pair entry.
Ptr< const AttributeChecker > m_secondchecker
Definition: pair.h:180
ns3::PairChecker::checker_pair_type GetCheckers(void) const
Get the pair of checkers for each pair entry.
Definition: pair.h:205
bool GetAccessor(T &value) const
Access the Pair value as type T.
Definition: pair.h:331
Ptr< AttributeValue > Copy(void) const
Definition: pair.h:266
result_type Get(void) const
Get the stored value as a std::pair.
Definition: pair.h:316
Internal checker class templated to each AttributeChecker for each entry in the pair.
Definition: pair.h:170
Hold objects of type std::pair<A, B>.
Definition: pair.h:47
Definition: first.py:1
Ptr< const AttributeAccessor > MakePairAccessor(T1 a1)
Definition: pair.h:338
std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Definition: pair.h:304
Ptr< AttributeChecker > MakePairChecker(const PairValue< A, B > &value)
Make a PairChecker from a PairValue.
Definition: pair.h:214