Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-12-09 10:30:07


"Paul Baxter" <pauljbaxter_at_[hidden]> writes:

>> "Paul Baxter" <pauljbaxter_at_[hidden]> writes:
>>
>>> I have a fixed list of parameter names and with each an associated
>>> set of attributes. I wish to access the attributes via the parameter
>>> name rather than the equivalent list entry index.
>>>
>>> In the past, we've created ENUMs with meaningful names to represent
>>> the parameter name->index mapping but I can't help feeling there
>>> must be a better way usng the name directly without this extra
>>> artificial mapping type.
>>
>>
>> Is there some reason http://www.boost.org/libs/parameter is not
>> appropriate for you?
>
> The parameter library looks like an incredibly useful library, I'm not sure
> it is what I'm looking for though, but am happy if you could give me an
> example. I'm looking for something that does the name->index 'lookup' at
> compile time.

The parameter library does compile-time lookups.

  args[third_parameter].get_byte_offset()

is a possible syntax with the parameter library. The key to the
compile-time lookup is that third_parameter is a different type from,
e.g., second_parameter,

> (Sorry if this fact wasn't made clearer). My initial reading
> of parameter is that it is solving a different problem.

Some facts:

You can't use a string literal as a template parameter in C++, so
there is *no way* to use a string literal as a key in a compile-time
lookup.

Your example, parameter["third parameter"].get_byte_offset(), goes
through a function call (operator[]) to get a class object. That part
is necessarily a runtime operation.

> Each of my configurable classes defines a fixed list of parameter names and
> associated attributes known at compile time.
> Rather than accessing via
> parameter 'list position' 1, I'd like to access attributes of each parameter
> (such as parameter type, default value, min/max values) via the parameter's
> name, mainly from a readability and maintainability point of view.

Sounds like the parameter library to me... provided you give up using
strings as indices.

> If I ever change the list to add/delete a parameter, the parameter
> 'access' code would require a recompile but pick up its new index
> automatically.

Sounds like the parameter library to me...

> I can't help feeling this should be easy to do and its frustrating that I
> can't find a way to achieve this other than the aforementioned enum.
>
> My application is a real-time high-speed one with hundreds (sometimes
> thousands) of configuration parameters. I need to interrogate these in a
> very short space of time which doesn't allow run-time assessment of strings
> for matching them.

http://lists.boost.org/Archives/boost/2005/09/93343.php

> Since all are known at compile time I'm sure it must be easy to do
> this. I'm pretty sure when someone shows me the obvious solution I
> will be very embarrassed, but until then I can do this with a
> std::map at runtime, but not at compile time.

I'm pretty sure the parameter library is a good -- if not perfect --
match for your problem. Since I've pointed at it twice now, if you're
not embarrassed yet, I'll stop pushing ;-)

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net