core (fixes #560): Unify TypeNameGet() in type-name.h and traced-value-callback-typedef-test-suite.cc

This commit is contained in:
Eduardo Almeida
2022-08-06 21:50:25 +00:00
committed by Tommaso Pecorella
parent 059c07926a
commit 180d71b7fa
4 changed files with 98 additions and 75 deletions

View File

@@ -25,6 +25,7 @@
#include "attribute-helper.h"
#include "event-id.h"
#include "int64x64.h"
#include "type-name.h"
#include <stdint.h>
#include <limits>
#include <cmath>
@@ -1376,6 +1377,14 @@ private:
}; // class TimeWithUnit
/**
* \ingroup time
*
* ns3::TypeNameGet<Time>() specialization.
* \returns The type name as a string.
*/
TYPENAMEGET_DEFINE (Time);
} // namespace ns3
#endif /* TIME_H */

View File

@@ -20,7 +20,8 @@
#ifndef TYPE_NAME_H
#define TYPE_NAME_H
#include <stdint.h>
#include "fatal-error.h"
#include <string>
/**
@@ -33,35 +34,55 @@ namespace ns3 {
/**
* \ingroup attributeimpl
* Type name strings for numeric AttributeValue types.
*
* \tparam T \explicit The numeric type.
* \returns The numeric type name as a string.
* Type name strings for AttributeValue types.
* Custom classes should add a template specialization of this function
* using the macro \c TYPE_NAME_GET_DEFINE(T).
*
* \tparam T \explicit The type.
* \returns The type name as a string.
*/
template <typename T>
std::string TypeNameGet (void)
{
NS_FATAL_ERROR ("Type name not defined.");
return "unknown";
}
/**
* \ingroup attributeimpl
* ns3::TypeNameGet(void) specializaton.
*
* Macro that defines a template specialization for \c TypeNameGet<T>() .
*
* \param T The type.
*/
#define TYPENAMEGET_DEFINE(T) \
template <> \
inline std::string TypeNameGet<T> (void) \
{ \
return #T; \
}
/**
* \ingroup attributeimpl
* ns3::TypeNameGet() specialization for numeric types.
* \returns The numeric type name as a string.
* @{
*/
// *NS_CHECK_STYLE_OFF*
template <> inline std::string TypeNameGet< int8_t > (void) { return "int8_t" ; };
template <> inline std::string TypeNameGet< int16_t > (void) { return "int16_t" ; };
template <> inline std::string TypeNameGet< int32_t > (void) { return "int32_t" ; };
template <> inline std::string TypeNameGet< int64_t > (void) { return "int64_t" ; };
template <> inline std::string TypeNameGet< uint8_t > (void) { return "uint8_t" ; };
template <> inline std::string TypeNameGet< uint16_t> (void) { return "uint16_t"; };
template <> inline std::string TypeNameGet< uint32_t> (void) { return "uint32_t"; };
template <> inline std::string TypeNameGet< uint64_t> (void) { return "uint64_t"; };
template <> inline std::string TypeNameGet< float > (void) { return "float" ; };
template <> inline std::string TypeNameGet< double > (void) { return "double" ; };
// *NS_CHECK_STYLE_ON*
TYPENAMEGET_DEFINE (bool);
TYPENAMEGET_DEFINE (int8_t);
TYPENAMEGET_DEFINE (int16_t);
TYPENAMEGET_DEFINE (int32_t);
TYPENAMEGET_DEFINE (int64_t);
TYPENAMEGET_DEFINE (uint8_t);
TYPENAMEGET_DEFINE (uint16_t);
TYPENAMEGET_DEFINE (uint32_t);
TYPENAMEGET_DEFINE (uint64_t);
TYPENAMEGET_DEFINE (float);
TYPENAMEGET_DEFINE (double);
TYPENAMEGET_DEFINE (long long);
TYPENAMEGET_DEFINE (unsigned long long);
TYPENAMEGET_DEFINE (long double);
/** @} */
} // namespace ns3

View File

@@ -21,6 +21,8 @@
#ifndef NS3_SEQ_NUM_H
#define NS3_SEQ_NUM_H
#include "ns3/type-name.h"
#include <limits>
#include <iostream>
#include <stdint.h>
@@ -431,8 +433,14 @@ typedef void (* SequenceNumber32)(SequenceNumber32 oldValue,
} // namespace TracedValueCallback
/**
* \ingroup seq-counters
*
* ns3::TypeNameGet<SequenceNumber32>() specialization.
* \returns The type name as a string.
*/
TYPENAMEGET_DEFINE (SequenceNumber32);
} // namespace ns3
#endif /* NS3_SEQ_NUM_H */

View File

@@ -18,9 +18,15 @@
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
*/
#include "ns3/nstime.h"
#include "ns3/object.h"
#include "ns3/sequence-number.h"
#include "ns3/test.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h" // SequenceNumber32
#include "ns3/traced-value.h"
#include "ns3/type-id.h"
#include "ns3/type-name.h"
#include <type_traits>
using namespace ns3;
@@ -35,34 +41,6 @@ using namespace ns3;
namespace {
/**
* \ingroup system-tests-traced
*
* \name Stringify the known TracedValue type names.
*/
/**
* Generic template for unknown classes.
*
* \returns The \c TracedValueCallback type name, or "unknown" for unknown classes.
*/
template <typename T> inline
std::string TypeName (void) { return "unknown"; }
/** @{ */
template <> inline std::string TypeName <bool> (void) { return "Bool" ; }
template <> inline std::string TypeName <int8_t> (void) { return "Int8_t" ; }
template <> inline std::string TypeName <int16_t> (void) { return "Int16_t" ; }
template <> inline std::string TypeName <int32_t> (void) { return "Int32_t" ; }
template <> inline std::string TypeName <int64_t> (void) { return "Int64_t" ; }
template <> inline std::string TypeName <uint8_t> (void) { return "Uint8_t" ; }
template <> inline std::string TypeName <uint16_t> (void) { return "Uint16_t"; }
template <> inline std::string TypeName <uint32_t> (void) { return "Uint32_t"; }
template <> inline std::string TypeName <uint64_t> (void) { return "Uint64_t"; }
template <> inline std::string TypeName <double> (void) { return "Double" ; }
template <> inline std::string TypeName <Time> (void) { return "Time" ; }
template <> inline std::string TypeName <SequenceNumber32> (void) { return "SequenceNumber32" ; }
/** @} */
/**
* \ingroup system-tests-traced
*
@@ -76,7 +54,6 @@ template <> inline std::string TypeName <SequenceNumber32> (void) { return "Sequ
*/
std::string g_Result = "";
/**
* \ingroup system-tests-traced
*
@@ -87,51 +64,58 @@ std::string g_Result = "";
* \tparam T \explicit The type of the value being traced.
* Since the point of this template is to create a
* sink function, the template type must be given explicitly.
* \param [in] oldValue The original value.
* \param [in] newValue The new value.
* \param [in] oldValue The original value
* \param [in] newValue The new value
*/
template <typename T>
void TracedValueCbSink (T oldValue, T newValue)
{
std::cout << ": "
<< (int64_t)oldValue << " -> "
<< (int64_t)newValue
<< static_cast<int64_t> (oldValue)
<< " -> "
<< static_cast<int64_t> (newValue)
<< std::endl;
if (oldValue != 0)
g_Result = "oldValue should be 0";
else if (newValue != 1)
g_Result = "newValue should be 1";
} // TracedValueCbSink<>()
if (oldValue != 0)
{
g_Result = "oldValue should be 0";
}
if (newValue != 1)
{
g_Result += std::string (g_Result == "" ? "" : " | ") + "newValue should be 1";
}
} // TracedValueCbSink<>()
/**
* \ingroup system-tests-traced
*
* TracedValueCbSink specialization for Time.
* \param oldValue The old value,
* \param newValue The new value.
* \param oldValue The old value
* \param newValue The new value
*/
template <>
void TracedValueCbSink<Time> (Time oldValue, Time newValue)
{
TracedValueCbSink <int64_t> (oldValue.GetInteger (),
newValue.GetInteger ());
TracedValueCbSink<int64_t> (oldValue.GetInteger (),
newValue.GetInteger ());
}
/**
* \ingroup system-tests-traced
*
* TracedValueCbSink specialization for SequenceNumber32.
* \param oldValue The old value,
* \param newValue The new value.
* \param oldValue The old value
* \param newValue The new value
*/
template <>
void TracedValueCbSink<SequenceNumber32> (SequenceNumber32 oldValue,
SequenceNumber32 newValue)
{
TracedValueCbSink <int64_t> (oldValue.GetValue (), newValue.GetValue ());
TracedValueCbSink<int64_t> (oldValue.GetValue (),
newValue.GetValue ());
}
} // unnamed namespace
@@ -169,12 +153,12 @@ private:
static TypeId GetTypeId (void)
{
static TypeId tid =
TypeId ("CheckTvCb<" + TypeName<T>() + ">")
TypeId ("CheckTvCb<" + TypeNameGet<T> () + ">")
.SetParent <Object> ()
.AddTraceSource ("value",
"A value being traced.",
MakeTraceSourceAccessor (&CheckTvCb<T>::m_value),
("ns3::TracedValueCallback::" + TypeName<T>()) )
("ns3::TracedValueCallback::" + TypeNameGet<T> ()))
;
return tid;
} // GetTypeId ()
@@ -200,24 +184,25 @@ private:
;
// The endl is in the sink function.
if (ok)
// Odd form here is to accommodate the uneven operator support
// of Time and SequenceNumber32.
m_value = m_value + (T) 1;
else
if (!ok)
{
// finish the line started above
std::cout << std::endl;
// and log the error
g_Result = "failed to connect callback";
return;
}
// Odd form here is to accommodate the uneven operator support
// of Time and SequenceNumber32.
m_value = m_value + static_cast<T> (1);
} // Invoke()
}; // class CheckTvCb<T>
/**
* Check the TracedValue typedef against TracedValueCbSink<T>.
*