|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2007 INRIA |
|
4 * All rights reserved. |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License version 2 as |
|
8 * published by the Free Software Foundation; |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 * |
|
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
20 */ |
|
21 #ifndef TRACE_CONTEXT_H |
|
22 #define TRACE_CONTEXT_H |
|
23 |
|
24 #include <stdint.h> |
|
25 #include <vector> |
|
26 #include "ns3/fatal-error.h" |
|
27 #include "trace-context-element.h" |
|
28 |
|
29 namespace ns3 { |
|
30 |
|
31 /** |
|
32 * \brief Provide context to trace sources |
|
33 * \ingroup lowleveltracing |
|
34 * |
|
35 * Instances of this class are used to hold context |
|
36 * for each trace source. Each instance holds a list of |
|
37 * 'contexts'. Trace sinks can lookup these contexts |
|
38 * from this list with the ns3::TraceContext::Get method. |
|
39 * |
|
40 * This class is implemented |
|
41 * using Copy On Write which means that copying unmodified |
|
42 * versions of this class is very cheap. However, modifying |
|
43 * the content of this class through a call |
|
44 * to ns3::TraceContext::Add will trigger a costly memory |
|
45 * reallocation if needed. |
|
46 */ |
|
47 class TraceContext |
|
48 { |
|
49 public: |
|
50 TraceContext (); |
|
51 TraceContext (TraceContext const &o); |
|
52 TraceContext const & operator = (TraceContext const &o); |
|
53 ~TraceContext (); |
|
54 |
|
55 /** |
|
56 * \param context add context to list of trace contexts. |
|
57 */ |
|
58 template <typename T> |
|
59 void Add (T const &context); |
|
60 |
|
61 /** |
|
62 * \param o the other context |
|
63 * |
|
64 * Perform the Union operation (in the sense of set theory) on the |
|
65 * two input lists of elements. This method is used in the |
|
66 * ns3::CallbackTraceSourceSource class when multiple sinks are connected |
|
67 * to a single source to ensure that the source does not need |
|
68 * to store a single TraceContext instance per connected sink. |
|
69 * Instead, all sinks share the same TraceContext. |
|
70 */ |
|
71 void Add (TraceContext const &o); |
|
72 |
|
73 /** |
|
74 * \param context context to get from this list of trace contexts. |
|
75 * |
|
76 * This method cannot fail. If the requested trace context is not |
|
77 * stored in this TraceContext, then, the program will halt. |
|
78 */ |
|
79 template <typename T> |
|
80 void Get (T &context) const; |
|
81 |
|
82 void Print (std::ostream &os) const; |
|
83 private: |
|
84 friend class TraceContextTest; |
|
85 // used exclusively for testing code. |
|
86 template <typename T> |
|
87 bool SafeGet (T &context) const; |
|
88 template <typename T> |
|
89 bool SafeAdd (const T &context); |
|
90 |
|
91 uint8_t *CheckPresent (uint8_t uid) const; |
|
92 bool DoAdd (uint8_t uid, uint8_t const *buffer); |
|
93 bool DoGet (uint8_t uid, uint8_t *buffer) const; |
|
94 |
|
95 struct Data { |
|
96 uint16_t count; |
|
97 uint16_t size; |
|
98 uint8_t data[4]; |
|
99 } * m_data; |
|
100 }; |
|
101 |
|
102 }//namespace ns3 |
|
103 |
|
104 namespace ns3 { |
|
105 |
|
106 template <typename T> |
|
107 void |
|
108 TraceContext::Add (T const &context) |
|
109 { |
|
110 const TraceContextElement *parent; |
|
111 // if the following assignment fails, it is because the input |
|
112 // to this function is not a subclass of the TraceContextElement class. |
|
113 parent = &context; |
|
114 uint8_t *data = (uint8_t *) &context; |
|
115 bool ok = DoAdd (T::GetUid (), data); |
|
116 if (!ok) |
|
117 { |
|
118 NS_FATAL_ERROR ("Trying to add twice the same type with different values is invalid."); |
|
119 } |
|
120 } |
|
121 template <typename T> |
|
122 void |
|
123 TraceContext::Get (T &context) const |
|
124 { |
|
125 TraceContextElement *parent; |
|
126 // if the following assignment fails, it is because the input |
|
127 // to this function is not a subclass of the TraceContextElement class. |
|
128 parent = &context; |
|
129 uint8_t *data = (uint8_t *) &context; |
|
130 bool found = DoGet (T::GetUid (), data); |
|
131 if (!found) |
|
132 { |
|
133 NS_FATAL_ERROR ("Type not stored in TraceContext"); |
|
134 } |
|
135 } |
|
136 template <typename T> |
|
137 bool |
|
138 TraceContext::SafeGet (T &context) const |
|
139 { |
|
140 TraceContextElement *parent; |
|
141 // if the following assignment fails, it is because the input |
|
142 // to this function is not a subclass of the TraceContextElement class. |
|
143 parent = &context; |
|
144 uint8_t *data = (uint8_t *) &context; |
|
145 bool found = DoGet (T::GetUid (), data); |
|
146 return found; |
|
147 } |
|
148 template <typename T> |
|
149 bool |
|
150 TraceContext::SafeAdd (const T &context) |
|
151 { |
|
152 const TraceContextElement *parent; |
|
153 // if the following assignment fails, it is because the input |
|
154 // to this function is not a subclass of the TraceContextElement class. |
|
155 parent = &context; |
|
156 uint8_t *data = (uint8_t *) &context; |
|
157 bool ok = DoAdd (T::GetUid (), data); |
|
158 return ok; |
|
159 } |
|
160 }//namespace ns3 |
|
161 |
|
162 #endif /* TRACE_CONTEXT_H */ |