Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Anonymous DataChunk in a non-MFC way

4.00/5 (3 votes)
14 Sep 2005BSD 1   155  
An anonymous DataChunk class to simplify memory management.

Introduction

If you have used the std::string and C-style string, you must think the std::string is so convenient in memory management. You can append/delete chars in any way without considering about the memory allocation. But there isn't any class for a byte stream in the std library. So, here's an implementation for a byte stream. I often use it when using Windows APIs, such as those for file operation, network, etc.

Usage

Here's the test code, I think it's so simple that a detailed explanation is not needed:

#include <iostream>
using namespace std;

#include "Easiware\DataChunk.h"

void main()
{
    Easiware::Memory::CDataChunk dc;
    
    dc    << 100
        << "test"
        << true;

    int i;
    std::string s;
    bool b;

    dc >> i >> s >> b;

    cout << i << endl
         << s << endl
         << b << endl;
}

Others

I wrote the code just after reading the book << Exceptional C++ >>, so I think this can be called an Exception-Safe class :). Hope you enjoy it, and thanks for any advice. Finally, thanks for reading this article, and sorry for my poor English.

License

This article, along with any associated source code and files, is licensed under The BSD License