Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / C++
Article

Anonymous DataChunk in a non-MFC way

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
14 Sep 2005BSD 35.2K   155   13   9
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


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat is the difference to std::stringstream? Pin
XXXXXXXXXXXXXXXX7715-Sep-05 23:49
XXXXXXXXXXXXXXXX7715-Sep-05 23:49 
AnswerRe: What is the difference to std::stringstream? Pin
balloy17-Sep-05 22:13
balloy17-Sep-05 22:13 
GeneralLike it Pin
Jerry Evans15-Sep-05 2:42
Jerry Evans15-Sep-05 2:42 
GeneralThanks your idea Pin
balloy15-Sep-05 19:59
balloy15-Sep-05 19:59 
GeneralRe: Thanks your idea Pin
Johann Gerell18-Sep-05 21:45
Johann Gerell18-Sep-05 21:45 
GeneralRe: Thanks your idea Pin
balloy18-Sep-05 22:34
balloy18-Sep-05 22:34 
GeneralRe: Thanks your idea Pin
Johann Gerell24-Sep-05 11:22
Johann Gerell24-Sep-05 11:22 
GeneralThanks, I found it Pin
balloy25-Sep-05 16:56
balloy25-Sep-05 16:56 
Generalmemcpy Pin
Pierre Couderc19-Sep-05 22:41
Pierre Couderc19-Sep-05 22:41 
Nothing can be quicker that memcpy (and memset) which are done in only 1 asm instruction, and a minimun CPU cycle, whatever the number of bytes :
rep movsd


Pierre Couderc
www.tol.fr

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.