CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2007
    Posts
    2

    Question Resources on extending STL classes needed

    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

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Resources on extending STL classes needed

    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....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Resources on extending STL classes needed

    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/cpl...lusplus19.html
    - http://www.codersource.net/cpp_class...alization.html
    - http://www.codeproject.com/cpp/Simul...irtualFunc.asp

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

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Resources on extending STL classes needed

    Quote Originally Posted by ltcmelo
    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"...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Resources on extending STL classes needed

    Hi.

    Quote Originally Posted by TheCPUWizard
    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:

    "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.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Resources on extending STL classes needed

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Resources on extending STL classes needed

    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?

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Resources on extending STL classes needed

    It was my understanding that the OP would like to: (psuedo code follows) replace:

    Code:
    for <iterator loop>
         callAnotherLegacyCRoutine((*i))
    with
    Code:
      vectorObject.callAnotherLegacyCRoutine();
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    May 2007
    Posts
    2

    Re: Resources on extending STL classes needed

    Quote Originally Posted by exterminator
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured