Boost logo

Boost Users :

From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2005-09-18 16:14:10


Hi Alexander,

Alexander Schuh <alexander.schuh <at> liwest.at> writes:

>
> Hi,
> in Example 1 there is a structure:
>
> struct employee{ int id; std::string name; int age; …
> };
>
>
>
> Is it possible to create automatically
>
> A) container
> with id as an index
>
> B) a
> view with name as index
>
> C) a
> view with age as index ?
>
>
>
> For this example it is easy to create the containers
> and views by hand but I plan to
> use at least 10 structures with many dimensions.
>
> For some containers I also need hash_indices -
> so there is really a lot of similar code, which
> I don’t want to have.

As C++ lacks reflection facilities, you cannot
really get much shorter a specification of the
container than it is shown in the example. With a
little of preprocessor machinery, it is possible
to specify the containers a little more compactly:

DEFINE_MULTI_INDEX_CONTAINER(
  employee,
  (ordered_unique,int,id)
  (ordered_non_unique,std::string,name)
  (ordered_non_unique,int,age)
)

With the aid of Boost.Preprocessor, it shouldn't be
too hard to implement the DEFINE_MULTI_INDEX_CONTAINER
macro so that it expands to the definition in Example 1.
Drop me a line if you are interested in this and
need assistance crafting such a macro.

> Also I want to provide iterators or one general
> (template) iterator for all dimensions. How would
> you write such a general iterator?
>
> There should be a function that takes for example “employee_name”
> or “employee_id” as a parameter to query
> the employee container. It would also be interesting to have a function that
> creates an iterator based only on the
>
> Information, for example, multi_index::container(1)_index(2).
>

You'd need some sort of runtime polymorphism to
achieve this. Google for "any_iterator", there's
a suitable construct in the Adobe Open Source library, and
also a similar lib by Doug Gregor (which finally didn't
make it into Boost AFAIK.) Combine any_iterator with an
appopriate object factory and you'll get what you
want, if I understood your goal correctly.

>
> Is there a function that calculates the max. index
> number of a certain container?
>

Yep:

template<typename MultiIndexContainer>
struct num_of_indices:
  boost::mpl::size<
    typename MultiIndexContainer::index_type_list
>
{};

(the max index number is just one less than num_of_indices.)
 
>
> Thank you very much,
>
> Alex Schuh

You're welcome; thank you for using Boost.MultiIndex,

Joaquín M López Muñoz
Telefónica, Investigación y Desarollo


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