A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
templated-classes.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5/**
6 * @file
7 * Example to demonstrate and test the registration of templated classes.
8 */
9
10#include "ns3/core-module.h"
11
12NS_LOG_COMPONENT_DEFINE("TemplatedClassesExample");
13
14namespace ns3
15{
16namespace alphaTest
17{
18
19/**
20 * Test class, inside the namespace ns3::alphaTest
21 */
22template <typename T>
23class AlphaProt : public std::enable_if_t<std::is_same_v<Object, T>, T>
24{
25 public:
26 /**
27 * @brief Get the TypeId
28 *
29 * @return The TypeId for this class
30 */
32 {
33 std::string name = GetTemplateClassName<AlphaProt<T>>();
34
35 static TypeId tid = TypeId(name).SetParent<Object>();
36 return tid;
37 }
38};
39
40} // namespace alphaTest
41
42/**
43 * Test class, inside the namespace ns3
44 */
45template <typename T>
46class BetaProt : public std::enable_if_t<std::is_same_v<Object, T>, T>
47{
48 public:
49 /**
50 * @brief Get the TypeId
51 *
52 * @return The TypeId for this class
53 */
55 {
56 std::string name = GetTemplateClassName<BetaProt<T>>();
57
58 static TypeId tid = TypeId(name).SetParent<Object>();
59 return tid;
60 }
61};
62
65
66} // namespace ns3
67
68using namespace ns3;
69
70int
71main(int argc, char* argv[])
72{
74
75 std::string alphaName("ns3::alphaTest::AlphaProt<Object>");
76 std::string alphaClassName(alpha->GetInstanceTypeId().GetName());
77 std::string alphaClassNameDirect(GetTemplateClassName<alphaTest::AlphaProt<Object>>());
78
79 if (alphaClassName != alphaName)
80 {
81 std::cerr << "Unexpected templated class name (\"" << alphaClassName << "\" instead of \""
82 << alphaName << "\")";
83 return -1;
84 }
85 if (alphaClassNameDirect != alphaName)
86 {
87 std::cerr << "Unexpected templated class name (\"" << alphaClassNameDirect
88 << "\" instead of \"" << alphaName << "\")";
89 return -1;
90 }
91
93
94 std::string betaName("ns3::BetaProt<Object>");
95 std::string betaClassName(beta->GetInstanceTypeId().GetName());
96 std::string betaClassNameDirect(GetTemplateClassName<BetaProt<Object>>());
97
98 if (betaClassName != betaName)
99 {
100 std::cerr << "Unexpected templated class name (\"" << betaClassName << "\" instead of \""
101 << betaName << "\")";
102 return -1;
103 }
104 if (betaClassNameDirect != betaName)
105 {
106 std::cerr << "Unexpected templated class name (\"" << betaClassNameDirect
107 << "\" instead of \"" << betaName << "\")";
108 return -1;
109 }
110
111 return 0;
112}
Test class, inside the namespace ns3.
static TypeId GetTypeId()
Get the TypeId.
A base class which provides memory management and object aggregation.
Definition object.h:81
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Test class, inside the namespace ns3::alphaTest.
static TypeId GetTypeId()
Get the TypeId.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_OBJECT_TEMPLATE_CLASS_WITH_NS_DEFINE(nst, type, nsp, param)
Explicitly instantiate a template class with one template parameter and register the resulting instan...
#define NS_OBJECT_TEMPLATE_CLASS_DEFINE(type, param)
Explicitly instantiate a template class with one template parameter and register the resulting instan...
Definition object-base.h:67
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string GetTemplateClassName()
Helper function to get the name (as a string) of the type of a template class.