actually allow connection and disconnection to trace sources registered in TypeIds

This commit is contained in:
Mathieu Lacage
2008-02-22 00:08:00 +01:00
parent 377f70de36
commit f8ca997175
7 changed files with 264 additions and 3 deletions

View File

@@ -9,6 +9,7 @@
#include "double.h"
#include "object-vector.h"
#include "integer-trace-source.h"
#include "trace-source-accessor.h"
namespace ns3 {
@@ -18,6 +19,10 @@ public:
AttributeTest ();
virtual bool RunTests (void);
private:
void NotifySource1 (int64_t old, int64_t n) {
m_gotNew = n;
}
int64_t m_gotNew;
};
class Derived : public Object
@@ -104,6 +109,8 @@ public:
MakeIntegerTraceSourceAccessor (&AttributeObjectTest::DoSetIntSrc,
&AttributeObjectTest::DoGetIntSrc),
MakeIntegerChecker<int8_t> ())
.AddTraceSource ("Source1", "help test",
MakeTraceSourceAccessor (&AttributeObjectTest::m_intSrc1))
;
return tid;
@@ -385,6 +392,15 @@ AttributeTest::RunTests (void)
NS_TEST_ASSERT (p->SetAttribute ("IntegerTraceSource2", Integer (-128)));
NS_TEST_ASSERT (!p->SetAttribute ("IntegerTraceSource2", Integer (-129)));
m_gotNew = -2;
NS_TEST_ASSERT (p->SetAttribute ("IntegerTraceSource1", Integer (-1)));
NS_TEST_ASSERT (p->TraceSourceConnect ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
NS_TEST_ASSERT (p->SetAttribute ("IntegerTraceSource1", Integer (0)));
NS_TEST_ASSERT_EQUAL (m_gotNew, 0);
NS_TEST_ASSERT (p->TraceSourceDisconnect ("Source1", MakeCallback (&AttributeTest::NotifySource1, this)));
NS_TEST_ASSERT (p->SetAttribute ("IntegerTraceSource1", Integer (1)));
NS_TEST_ASSERT_EQUAL (m_gotNew, 0);
return result;
}

View File

@@ -39,10 +39,10 @@ public:
return *this;
}
void AddCallback (const CallbackBase & callback) {
void Connect (const CallbackBase & callback) {
m_callback.AddCallback (callback);
}
void RemoveCallback (const CallbackBase & callback) {
void Disconnect (const CallbackBase & callback) {
m_callback.RemoveCallback (callback);
}
protected:

View File

@@ -23,6 +23,7 @@
#include "singleton.h"
#include "trace-resolver.h"
#include "attribute.h"
#include "trace-source-accessor.h"
#include "log.h"
#include <vector>
#include <sstream>
@@ -66,6 +67,15 @@ public:
ns3::Attribute GetAttributeInitialValue (uint16_t uid, uint32_t i) const;
ns3::Ptr<const ns3::AttributeAccessor> GetAttributeAccessor (uint16_t uid, uint32_t i) const;
ns3::Ptr<const ns3::AttributeChecker> GetAttributeChecker (uint16_t uid, uint32_t i) const;
void AddTraceSource (uint16_t uid,
std::string name,
std::string help,
ns3::Ptr<const ns3::TraceSourceAccessor> accessor);
uint32_t GetTraceSourceN (uint16_t uid) const;
std::string GetTraceSourceName (uint16_t uid, uint32_t i) const;
std::string GetTraceSourceHelp (uint16_t uid, uint32_t i) const;
ns3::Ptr<const ns3::TraceSourceAccessor> GetTraceSourceAccessor (uint16_t uid, uint32_t i) const;
private:
struct ConstructorInformation {
ns3::CallbackBase cb;
@@ -79,6 +89,11 @@ private:
ns3::Ptr<const ns3::AttributeAccessor> param;
ns3::Ptr<const ns3::AttributeChecker> checker;
};
struct TraceSourceInformation {
std::string name;
std::string help;
ns3::Ptr<const ns3::TraceSourceAccessor> accessor;
};
struct IidInformation {
std::string name;
uint16_t parent;
@@ -86,6 +101,7 @@ private:
std::string groupName;
std::vector<struct ConstructorInformation> constructors;
std::vector<struct AttributeInformation> attributes;
std::vector<struct TraceSourceInformation> traceSources;
};
typedef std::vector<struct IidInformation>::const_iterator Iterator;
@@ -313,6 +329,47 @@ IidManager::GetAttributeChecker (uint16_t uid, uint32_t i) const
return information->attributes[i].checker;
}
void
IidManager::AddTraceSource (uint16_t uid,
std::string name,
std::string help,
ns3::Ptr<const ns3::TraceSourceAccessor> accessor)
{
struct IidInformation *information = LookupInformation (uid);
struct TraceSourceInformation source;
source.name = name;
source.help = help;
source.accessor = accessor;
information->traceSources.push_back (source);
}
uint32_t
IidManager::GetTraceSourceN (uint16_t uid) const
{
struct IidInformation *information = LookupInformation (uid);
return information->traceSources.size ();
}
std::string
IidManager::GetTraceSourceName (uint16_t uid, uint32_t i) const
{
struct IidInformation *information = LookupInformation (uid);
NS_ASSERT (i < information->traceSources.size ());
return information->traceSources[i].name;
}
std::string
IidManager::GetTraceSourceHelp (uint16_t uid, uint32_t i) const
{
struct IidInformation *information = LookupInformation (uid);
NS_ASSERT (i < information->traceSources.size ());
return information->traceSources[i].help;
}
ns3::Ptr<const ns3::TraceSourceAccessor>
IidManager::GetTraceSourceAccessor (uint16_t uid, uint32_t i) const
{
struct IidInformation *information = LookupInformation (uid);
NS_ASSERT (i < information->traceSources.size ());
return information->traceSources[i].accessor;
}
} // anonymous namespace
/*********************************************************************
@@ -631,6 +688,56 @@ TypeId::GetAttributeChecker (uint32_t i) const
return checker;
}
uint32_t
TypeId::GetTraceSourceN (void) const
{
return Singleton<IidManager>::Get ()->GetTraceSourceN (m_tid);
}
std::string
TypeId::GetTraceSourceName (uint32_t i) const
{
return Singleton<IidManager>::Get ()->GetTraceSourceName (m_tid, i);
}
std::string
TypeId::GetTraceSourceHelp (uint32_t i) const
{
return Singleton<IidManager>::Get ()->GetTraceSourceHelp (m_tid, i);
}
Ptr<const TraceSourceAccessor>
TypeId::GetTraceSourceAccessor (uint32_t i) const
{
return Singleton<IidManager>::Get ()->GetTraceSourceAccessor (m_tid, i);
}
TypeId
TypeId::AddTraceSource (std::string name,
std::string help,
Ptr<const TraceSourceAccessor> accessor)
{
Singleton<IidManager>::Get ()->AddTraceSource (m_tid, name, help, accessor);
return *this;
}
Ptr<const TraceSourceAccessor>
TypeId::LookupTraceSourceByName (std::string name) const
{
TypeId tid = TypeId (0);
TypeId nextTid = *this;
do {
tid = nextTid;
for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
{
std::string srcName = tid.GetTraceSourceName (i);
if (srcName == name)
{
return tid.GetTraceSourceAccessor (i);
}
}
nextTid = tid.GetParent ();
} while (nextTid != tid);
return 0;
}
bool operator == (TypeId a, TypeId b)
{
@@ -1035,6 +1142,30 @@ Object::GetAttribute (std::string name) const
return value;
}
bool
Object::TraceSourceConnect (std::string name, const CallbackBase &cb)
{
Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
if (accessor == 0)
{
return false;
}
bool ok = accessor->Connect (this, cb);
return ok;
}
bool
Object::TraceSourceDisconnect (std::string name, const CallbackBase &cb)
{
Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
if (accessor == 0)
{
return false;
}
bool ok = accessor->Disconnect (this, cb);
return ok;
}
Ptr<Object>
Object::DoGetObject (TypeId tid) const
{

View File

@@ -47,6 +47,7 @@ class Object;
class AttributeAccessor;
class AttributeValue;
class AttributeList;
class TraceSourceAccessor;
/**
* \brief a unique identifier for an interface.
@@ -140,6 +141,11 @@ public:
Attribute GetAttributeInitialValue (uint32_t i) const;
uint32_t GetTraceSourceN (void) const;
std::string GetTraceSourceName (uint32_t i) const;
std::string GetTraceSourceHelp (uint32_t i) const;
Ptr<const TraceSourceAccessor> GetTraceSourceAccessor (uint32_t i) const;
Ptr<Object> CreateObject (const AttributeList &attributes) const;
@@ -227,9 +233,13 @@ public:
std::string help,
uint32_t flags,
Attribute initialValue,
Ptr<const AttributeAccessor> spec,
Ptr<const AttributeAccessor> accessor,
Ptr<const AttributeChecker> checker);
TypeId AddTraceSource (std::string name,
std::string help,
Ptr<const TraceSourceAccessor> accessor);
// construct an invalid TypeId.
TypeId ();
~TypeId ();
@@ -246,6 +256,8 @@ private:
Ptr<const AttributeChecker> checker;
};
Ptr<const TraceSourceAccessor> LookupTraceSourceByName (std::string name) const;
/**
* \param name the name of the requested attribute
* \returns the Accessor associated to the requested attribute
@@ -372,6 +384,9 @@ public:
*/
Attribute GetAttribute (std::string name) const;
bool TraceSourceConnect (std::string name, const CallbackBase &cb);
bool TraceSourceDisconnect (std::string name, const CallbackBase &cb);
/**
* Increment the reference count. This method should not be called
* by user code. Object instances are expected to be used in conjunction

View File

@@ -0,0 +1,25 @@
#include "trace-source-accessor.h"
namespace ns3 {
TraceSourceAccessor::TraceSourceAccessor ()
: m_count (1)
{}
TraceSourceAccessor::~TraceSourceAccessor ()
{}
void
TraceSourceAccessor::Ref (void) const
{
m_count++;
}
void
TraceSourceAccessor::Unref (void) const
{
m_count--;
if (m_count == 0)
{
delete this;
}
}
} // namespace ns3

View File

@@ -0,0 +1,71 @@
#ifndef TRACE_SOURCE_ACCESSOR_H
#define TRACE_SOURCE_ACCESSOR_H
#include <stdint.h>
#include "object-base.h"
#include "callback.h"
#include "ptr.h"
namespace ns3 {
class TraceSourceAccessor : public ObjectBase
{
public:
TraceSourceAccessor ();
virtual ~TraceSourceAccessor ();
void Ref (void) const;
void Unref (void) const;
virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const = 0;
virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const = 0;
private:
mutable uint32_t m_count;
};
template <typename T>
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
} // namespace ns3
namespace ns3 {
template <typename T, typename SOURCE>
Ptr<const TraceSourceAccessor>
DoMakeTraceSourceAccessor (SOURCE T::*a)
{
struct Accessor : public TraceSourceAccessor
{
virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const {
T *p = dynamic_cast<T*> (obj);
if (p == 0)
{
return false;
}
(p->*m_source).Connect (cb);
return true;
}
virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const {
T *p = dynamic_cast<T*> (obj);
if (p == 0)
{
return false;
}
(p->*m_source).Disconnect (cb);
return true;
}
SOURCE T::*m_source;
} *accessor = new Accessor ();
accessor->m_source = a;
return Ptr<const TraceSourceAccessor> (accessor, false);
}
template <typename T>
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
{
return DoMakeTraceSourceAccessor (a);
}
} // namespace ns3
#endif /* TRACE_SOURCE_ACCESSOR_H */

View File

@@ -64,6 +64,7 @@ def build(bld):
'object-vector.cc',
'initial-value.cc',
'event-trace-source.cc',
'trace-source-accessor.cc',
]
if sys.platform == 'win32':
@@ -119,5 +120,7 @@ def build(bld):
'attribute-helper.h',
'initial-value.h',
'event-trace-source.h',
'integer-trace-source.h',
'trace-source-accessor.h',
]