Thread: Friends design

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not the whole file; not if it's like 100 MB.
    That's why I chose the buffering approach - read a chunk of data, and when accessing it, check if we have read data up to that point, and if not, read it and buffer it.
    I just chose to encapsulate this as an iterator.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well fair enough... I don't imagine there being much of a difference in method if the files are big enough to annoy you in the terms you described. Here is one thread; Daved describes what I was thinking about. Just in case you change your mind.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I remember that thread. Basically, the read function is the fastest, and it's what I use for buffering.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Random-access iterators over files are of course a known problem for parsers, which is why I would expect a C++ parser library to have such an iterator.

    Why, look, it does:
    File Iterator
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by CornedBee View Post
    Random-access iterators over files are of course a known problem for parsers, which is why I would expect a C++ parser library to have such an iterator.

    Why, look, it does:
    File Iterator
    I've always admired the Spirit project's clean, orthogonal libraries. This is a great example of such a design. I think I'll be using this one a lot.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mario F. View Post
    Well, dunno Elysia.



    The problem (not really a problem, mind you) is that an iterator class only makes sense in the presence of an aggregator class. The moment you try to protect aggregate functionality from the main class, your iterator becomes unintuitive. Personally, as a user of your class, I'd rather see it implementing it's own traversing functionality.

    Of course it is all a matter of design. It's your choice what you want to do. Being that you still would want an iterator class, in order to better protect yourself from future changes, I'd probably partition your stream class roles with a mixin interface class that implemented the aggregate functionality of your stream class. Then, implement the iterator pattern from that aggregate.

    If this is not an option because the stream class will always be used with a buffer, you'll probably be forced to indeed derive your iterator from the stream class. But that's really perverting the idea of a derived class. In any case, you are probably looking into a private inheritance with protected members in the stream class.

    Really, I'd go back to anon initial post and suggest you consider your design. If I was to use your stream class, i'd be slightly annoyed if I had to go through an iterator to traverse what is essentially a non aggregate.
    I had to revisit this now that I have time and somewhat understand what it means.
    But I'm still not sure what you actually mean. I get the concept of aggregates somewhat.
    But what would a non-aggregate class be?

    Also, dunno if I mentioned this but, I derive my stream class from the standard library stream class (for input). This works fine, thankfully, since I won't need access to private members. Only add the actual buffer, open wrapper and of course begin/end functions for iterators.
    So it should work just as a "normal" input stream, but with the option of getting input iterators from it, which would be specially made to support the whole buffering system.

    Quote Originally Posted by CornedBee View Post
    Random-access iterators over files are of course a known problem for parsers, which is why I would expect a C++ parser library to have such an iterator.

    Why, look, it does:
    File Iterator
    Ah, I love that boost seems to have a solution for everything.
    Unfortunately, I do have one gripe with it: read-only. It's not ideal, but it's certainly worth keeping mind, so thanks for the heads up.
    However, reading (parsing) is only half the problem of what I'm thinking of designing. I want to write out data to the file, as well (albeit, this is the next step after the input is finished).
    I suppose ostream iterators might work for this, though? I don't know what type of iterator they are, if they exist.
    Last edited by Elysia; 07-30-2009 at 01:24 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ostream iterators (yes, they exist) are OutputIterators. You can write to them, but not read from them, and they support only a single pass over the sequence. (They're the pendant of InputIterators.)
    I don't see why you would need random access on output anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But I don't suppose they support seeking (ie += offset)?
    It would a lot more efficient to write out parts instead of everything. Don't know if I can make that work, but if I could, that would be cool. It's an option anyway.

    EDIT: C++ Reference says no, from what I can tell...
    Gah. Stupid iterators.
    We'll just have to do it the old fashioned way then, eh?
    Last edited by Elysia; 07-30-2009 at 01:48 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  2. any comments about a cache design?
    By George2 in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 12:53 PM
  3. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM