Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC

Introduction to ACF (Another C++ Framework)

Rate me:
Please Sign up or sign in to vote.
4.86/5 (36 votes)
27 May 200411 min read 135.9K   737   49  
This article introduces ACF, a C++ framework which brings the .NET framework to standard C++.
//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//

#include "stdafx.h"

namespace Acf {
	namespace Text {

//---------------------------------------------------------------------
// class DefaultEncoder
//
// Default encoder implementation for encodings that do not require 
// state to be maintained between successive calls.
//

class DefaultEncoder : public Encoder
{
private:
    RefPtr<Encoding> _encoding;

public:
    DefaultEncoder(Encoding* encoding)
    {
        this->_encoding = encoding;
    }

    override int GetByteCount(Array<wchar_t>* chars, int index, int count, bool /*flush*/)
    {
        return this->_encoding->GetByteCount(chars, index, count);
    }

    override int GetBytes(Array<wchar_t>* chars, int charIndex, int charCount,
        Array<byte>* bytes, int byteIndex, bool /*flush*/)
    {
        return this->_encoding->GetBytes(chars, charIndex, charCount,
            bytes, byteIndex);
    }
};

//---------------------------------------------------------------------
// class DefaultDecoder
//
// Default decoder implementation for encodings that do not require 
// state to be maintained between successive calls.
//

class DefaultDecoder : public Decoder
{
private:
    RefPtr<Encoding> _encoding;

public:
    DefaultDecoder(Encoding* encoding)
    {
        this->_encoding = encoding;
    }

    override int GetCharCount(Array<byte>* bytes, int index, int count)
    {
        return this->_encoding->GetCharCount(bytes, index, count);
    }

    override int GetChars(Array<byte>* bytes, int byteIndex, int byteCount,
        Array<wchar_t>* chars, int charIndex)
    {
        return this->_encoding->GetChars(bytes, byteIndex, byteCount,
            chars, charIndex);
    }
};

//---------------------------------------------------------------------
// class Encoding

// Static Properties

/*static*/ EncodingPtr Encoding::get_ASCII()
{
    return new ASCIIEncoding();
}

/*static*/ EncodingPtr Encoding::get_Unicode()
{
    return new UnicodeEncoding();
}

/*static*/ EncodingPtr Encoding::get_BigEndianUnicode()
{
    return new UnicodeEncoding(true, true);
}

/*static*/ EncodingPtr Encoding::get_UTF8()
{
    return new UTF8Encoding();
}

/*static*/ EncodingPtr Encoding::get_Default()
{
    int codePage = (int) ::GetACP();

    switch (codePage)
    {
    case 1200:
        return new UnicodeEncoding();
        break;

    case 1201:
        return new UnicodeEncoding(true, true);
        break;

    case 65001:
        return new UTF8Encoding();
        break;

    default:
        return new Internal::CodePageEncoding(codePage);
        break;
    }
}

// Static Methods

/*static*/ RefPtr< Array<byte> > Encoding::Convert(Encoding* srcEncoding, Encoding* dstEncoding, 
    Array<byte>* bytes)
{
    if (bytes == null)
        throw ArgumentNullException();

    return Convert(srcEncoding, dstEncoding, bytes, 0, bytes->Length);
}

/*static*/ RefPtr< Array<byte> > Encoding::Convert(Encoding* srcEncoding, Encoding* dstEncoding, 
    Array<byte>* bytes, int index, int count)
{
    Acf::VerifyArrayRange(bytes, index, count);

    RefPtr< Array<wchar_t> > chars = srcEncoding->GetChars(bytes, index, count);
    return dstEncoding->GetBytes(chars);
}

// Methods

/*virtual*/ RefPtr< Array<byte> > Encoding::GetPreamble()
{
    return new Array<byte>(0);
}

/*virtual*/ EncoderPtr Encoding::GetEncoder()
{
    return new DefaultEncoder(this);
}

/*virtual*/ DecoderPtr Encoding::GetDecoder()
{
    return new DefaultDecoder(this);
}

/*virtual*/ int Encoding::GetByteCount(Array<wchar_t>* chars)
{
    if (chars == null)
        throw ArgumentNullException();

    return GetByteCount(chars, 0, chars->Length);
}

/*virtual*/ int Encoding::GetByteCount(String* s)
{
    if (s == null)
        throw ArgumentNullException();

    RefPtr< Array<wchar_t> > chars = s->ToCharArray();
    return GetByteCount(chars, 0, chars->Length);
}

/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(Array<wchar_t>* chars)
{
    if (chars == null)
        throw ArgumentNullException();

    return GetBytes(chars, 0, chars->Length);
}

/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(Array<wchar_t>* chars, 
    int index, int count)
{
    int byteCount = GetByteCount(chars, index, count);
    RefPtr< Array<byte> > bytes = new Array<byte>(byteCount);
    GetBytes(chars, index, count, bytes, 0);
    return bytes;
}

/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(String* s)
{
    if (s == null)
        throw ArgumentNullException();

    RefPtr< Array<wchar_t> > chars = s->ToCharArray();
    return GetBytes(chars, 0, chars->Length);
}

/*virtual*/ int Encoding::GetBytes(String* s, int charIndex, int charCount,
    Array<byte>* bytes, int byteIndex)
{
    if (s == null)
        throw ArgumentNullException();

    return GetBytes(s->ToCharArray(), charIndex, charCount, bytes, byteIndex);
}

/*virtual*/ int Encoding::GetCharCount(Array<byte>* bytes)
{
    if (bytes == null)
        throw ArgumentNullException();

    return GetCharCount(bytes, 0, bytes->Length);
}

/*virtual*/ RefPtr< Array<wchar_t> > Encoding::GetChars(Array<byte>* bytes)
{
    if (bytes == null)
        throw ArgumentNullException();

    return GetChars(bytes, 0, bytes->Length);
}

/*virtual*/ RefPtr< Array<wchar_t> > Encoding::GetChars(Array<byte>* bytes, 
    int index, int count)
{
    int charCount = GetCharCount(bytes, index, count);
    RefPtr< Array<wchar_t> > chars = new Array<wchar_t>(charCount);
    GetChars(bytes, index, count, chars, 0);
    return chars;
}

/*virtual*/ StringPtr Encoding::GetString(Array<byte>* bytes)
{
    if (bytes == null)
        throw ArgumentNullException();

    return GetString(bytes, 0, bytes->Length);
}

/*virtual*/ StringPtr Encoding::GetString(Array<byte>* bytes, 
    int byteIndex, int byteCount)
{
    return new String(GetChars(bytes, byteIndex, byteCount));
}

	} // namespace Text
} // namespace Acf

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.

He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.

He also has a blog at http://blogs.wwwcoder.com/yljia/

Comments and Discussions