Bug 1604

Summary: Config::GetDefault
Product: ns-3 Reporter: Nicola Baldo <nicola>
Component: coreAssignee: Peter Barnes <pdbarnes>
Status: PATCH PENDING ---    
Severity: enhancement CC: ns-bugs, pdbarnes, tommaso.pecorella
Priority: P5    
Version: pre-release   
Hardware: All   
OS: All   
Bug Depends on:    
Bug Blocks: 2330    
Attachments: std::string Config::GetDefault() patch
Patch for template <typename R> R Config::GetDefault()
Patch for template <typename R> R Config::GetDefault()

Description Nicola Baldo 2013-03-18 13:55:57 EDT
It would be nice to have a Config::GetDefault method that allows to retrieve the default value of an attribute at run time.
Comment 1 Tommaso Pecorella 2013-03-19 08:31:26 EDT
Isn't this doing the same thing ?

bool ns3::TypeId::LookupAttributeByName ( std::string name, struct AttributeInformation *info ) const

I mean, you get the class' TypeId, then you use LookupAttributeByName, then you look into the AttributeInformation for the ns3::Ptr< const ns3::AttributeValue > initialValue, then you extract the value from it.

Maybe it could be done in an easier way, however you can't know the attribute's type in advance, so the GetDefault function might have the very same implementation clumsiness.

Unless you're searching for the default value of the attribute of another class. In this case it's "just" a bit more complex, as you'll need first to retrieve the TypeId for that class.

Anyway, it can be done. If I'm right...

Comment 2 Peter Barnes 2014-09-12 18:28:55 EDT
I think the heart of the code needed is now in command-line.cc:439:

CommandLine::AddValue (const std::string &name,
                       const std::string &attributePath)

which does essentially what Tommaso suggests.

@Nicola: which default do you want to return?  
AttributeInformation::initialValue or
Comment 3 Nicola Baldo 2014-10-22 15:39:24 EDT
> @Nicola: which default do you want to return?  
> AttributeInformation::initialValue or
> AttributeInformation::originalInitialValue

The usage I have in mind is to return the default value that will be used for new objects, e.g., if the default value was changed from the cmd line, then the changed value should be returned. I guess this is  AttributeInformation::initialValue, right?
Comment 4 Peter Barnes 2014-10-22 18:24:35 EDT
Created attachment 1910 [details]
std::string Config::GetDefault() patch
Comment 5 Peter Barnes 2014-10-22 18:36:21 EDT
This patch adds 

std::string Config::GetDefault (const std::string attributePath)

As Tommaso noted it's hard to infer the actual type of the attribute (it could be any of the ~36 derivatives of AttributeValue), never mind the underlying object, so the patch just returns the initial value serialized to a string.

If you think you might know the type of the attribute, I could try to write something like

template<typename T>
Config::GetDefault (const std::string attributePath);

which would attempt to create an initialized T.  Of course T would have to be derived from AttributeValue for this to succeed, and you'd have to check that the return wasn't null.
Comment 6 Peter Barnes 2015-01-16 17:09:33 EST
Created attachment 1945 [details]
Patch for template <typename R> R Config::GetDefault()

This is a patch implementing 

template <typename R>
R  Config::GetDefault (const std::string attributePath);

This is specialized for almost all the types underlying the various AttributeValue classes.

Usage is like

  Box boxDefault = GetDefault<Box> (".../Building/Boundaries");

If the attribute path can't be converted to the requested type R it causes a fatal error.
Comment 7 Peter Barnes 2015-01-16 17:12:52 EST
Created attachment 1946 [details]
Patch for template <typename R> R Config::GetDefault()

Whoops, left off the doxygen.conf lines.