Click to See Complete Forum and Search --> : Resources on extending STL classes needed


NCNdian
May 16th, 2007, 03:30 PM
We need to accept, then move around, a large, highly specialized c structure within C++ code and then pass it back to some c library calls. We also need to populate, and de-populate the structure as well. This structure is an array and will only ever be populated from, or depopulated to, vectors. Never a list or some other container( or at least they are willing to enforce this).

I wrote some code, works great, but now the "powers that be" suggested I extend the vector class to hide all this dirty work from the coders who will need this structure. In other words, they would like to see a class that allows future coders to do something like:

================================
// Please don't syntax critique - just
// trying to communicate an idea
#include wrapper::to::hpp:with::c::structure

std::vector<void *> myVec;
myWrapper::cStructure gnarlyStructure.

// Get a structure full of data from a
// legacy c call
rc = myLegacyCRoutine(gnarlyStrucure)
if (0U == rc) {
myVec.populateVector(gnarlyStructure)
}

...go do stuff with vector like.....

for <iterator loop>
// Pass vector elements one at a time to
// another legacy c routine
callAnotherLegacyCRoutine((*i))
}

================================
Of course the reverse wil be done too, but you get the idea. It should be said I have only used the STL, never messed with it and have no significant experience with templates in general - only very simple templates where I needed flexibility in data typing. Nothing fancy that is for sure.

So my question is: I assume this has been done before but am having a hard time finding a succinct helpful document on template specialization/extension that seems relevant or is written with folks like me in mind. In short, does anyone here know of any good resources that would help me learn to do the above?

Thanks in advance,

NCNdian
C++ - on AIX

TheCPUWizard
May 16th, 2007, 03:44 PM
7 choices...

1) Modify the STL templates. DONT!
2) Derive a concrete class publically from std::vector
3) Derive a concrete class privately from std::vector
4) Aggregate std::vector into a concrete class
5) Derive a template class publically from std::vector
6) Derive a template class privately from std::vector
7) Aggregate std::vector into a template class

From the description yo give I would take #4 or #7....

ltcmelo
May 16th, 2007, 03:55 PM
Hi.

Initially, it's not a good idea to extend from STL containers, unless you have used private inheritance (which behaves like a "has-a" relationship and not a "is-a" relationship) or you have total control of the clients of the class and know what they're doing. STL containers were not made with this in mind. That's the reason they don't have virtual destructors. An alternative is to use composition and make std::vector be a member of this class.

Templates are great! There are some nice books about them: C++ Templates, Generic Programming and the STL: Using and Extending the C++ Standard Template Library, The C++ Standard Library: A Tutorial and Reference, Modern C++ design: Generic Programming and Design Patterns Applied. However, if you don't want to grab a book, you can also find some useful resource on the web. Here are a few links:
- http://www.hackcraft.net/cpp/templateInheritance/
- http://www.icce.rug.nl/documents/cplusplus/cplusplus19.html
- http://www.codersource.net/cpp_class_templates_specialization.html
- http://www.codeproject.com/cpp/SimulationofVirtualFunc.asp

Hope that helps! And if you have a more specific question, you can post again!

TheCPUWizard
May 16th, 2007, 03:59 PM
unless you have used private inheritance (which behaves like a "has-a" relationship and not a "is-a" relationship)

Aggregation is a "has-a" relationship (class "has a" vector as a member)

Private inheritance is refered to "...is implemented in terms of"...

ltcmelo
May 16th, 2007, 04:15 PM
Hi.

Aggregation is a "has-a" relationship (class "has a" vector as a member)

Private inheritance is refered to "...is implemented in terms of"...

Quote from here (http://www.parashift.com/c++-faq-lite/private-inheritance.html):

"private inheritance is a syntactic variant of composition (AKA aggregation and/or has-a)."

That's why I said private inheritance behaves like a "has-a" relationship.

TheCPUWizard
May 17th, 2007, 04:03 AM
IMHO,

The quote you used has the syntactic structure as shown below....


"private inheritance is a syntactic variant of [composition (AKA aggregation and/or has-a)]."


In that "composition AKA aggregation"... "composition AKA has-a"... thus "aggregataion AKA has-a".

A quibble to a certain degree, but if you asked a number of people to:

"Implement an XXX class that "has-a" YYY class"
and
"Implement an XXX class "in-term-od" ZZZ class"

The majority would use aggregation for the first, and private inheritance for the second.

exterminator
May 17th, 2007, 05:00 AM
I don't think you need anything extra than what the vector<> already supplies. All you need is just build the vector<> from an array of structures and then get that back. Right?

For the former, use the member function, assign and for the latter - you can pass the address of the first member of the vector<> to the API and the length of the vector<> as the size. That's all you need. Isn't it, or do you need anything extra apart from that?

What are the motivations for this extension, what interface do you want to add to std::vector<> specifically?

TheCPUWizard
May 17th, 2007, 06:32 AM
It was my understanding that the OP would like to: (psuedo code follows) replace:


for <iterator loop>
callAnotherLegacyCRoutine((*i))

with

vectorObject.callAnotherLegacyCRoutine();

NCNdian
May 17th, 2007, 12:12 PM
I don't think you need anything extra than what the vector<> already supplies. All you need is just build the vector<> from an array of structures and then get that back. Right?

For the former, use the member function, assign and for the latter - you can pass the address of the first member of the vector<> to the API and the length of the vector<> as the size. That's all you need. Isn't it, or do you need anything extra apart from that?

What are the motivations for this extension, what interface do you want to add to std::vector<> specifically?


I need:

myVector.createGnarlyStructureFromVector()
myVector.initializeVectorWithGnarlyStructure()
myVector.cleanUpGnarlyStructure()
myVector.<additional-utility-routines>

Foir brevities sake, I didn't fully divulge all the requirements originally. There are additional requirements stemming from the fact there are already legacy 'c' routines for accessing, creating, destrying, etc. the structure. These routines must be used and can not be re-invented. So I can't just allocate/memset a chunk of memory and load the array from the vector. I have to use pre-written routines. In turn, the Powers-That-Be want my code to be "hidden" so future C++ developers just call three or 4 simple extended vector methods to handle all this.

Thanks for all the help. I am already hitting some of the web sites and plan on grabbing a book or two.

PS: The structure is a home grown doubly linked list whose payload is a char double pointer referencing a very large structure. I am as worried about the memory mangement as anything else.