Click here to Skip to main content
15,895,799 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 136.4K   739   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.
//
// AcfString.h - defines class String
//

#ifndef __Acf_String__
#define __Acf_String__

namespace Acf {

namespace Text {
    class Encoding;
}

using namespace Acf::Text;

//---------------------------------------------------------------------
// class String
//
// Implements an immutable Unicode string object.
// sizeof(String) = 12 (Object) + 2 * 8 + 4 = 32 bytes
//

class String : public Object
{
// Static Fields
public:
    static const StringPtr Empty;

// Fields
private:
    AutoBuffer<wchar_t, 8> _buffer;

// Construtors
public:
    String(const char* value, int length = -1, Encoding* enc = null);
    String(const wchar_t* value, int length = -1);
    String(wchar_t c, int count);
    String(Array<wchar_t>* chars);
    String(Array<wchar_t>* chars, int startIndex, int length);

private:
    String(int length);

// Destructor
private:
    virtual ~String();

// Properties
public:
    // Chars
    __declspec(property(get=get_Chars)) wchar_t Chars[];
    wchar_t get_Chars(int index);
    // Length
    __declspec(property(get=get_Length)) int Length;
    int get_Length();

// Static Methods
public:
    static StringPtr FromCString(const char* s);
    static StringPtr FromCString(const wchar_t* s);

    //
    // Comparing strings
    //
    static bool Equals(String* s1, String* s2);
    static bool Equals(String* s1, const wchar_t* s2);
    static int CompareOrdinal(String* s1, String* s2);
    static int CompareOrdinal(String* s1, int index1, String* s2, 
        int index2, int length);
    static int Compare(String* s1, String* s2, bool caseSensitive = true);
    static int Compare(String* s1, int index1, String* s2, int index2, 
        int length, bool caseSensitive = true);

    //
    // Copying strings
    //
    static StringPtr Copy(String* s);

    //
    // Concatenating strings
    //
    static StringPtr Concat(String* s1, String* s2);
    static StringPtr Concat(String* s1, String* s2, String* s3);
    static StringPtr Concat(Array<StringPtr>* values);
    static StringPtr Join(String* separator, Array<StringPtr>* value);
    static StringPtr Join(String* separator, Array<StringPtr>* value, 
        int startIndex, int count);

    //
    // Formatting strings
    //
    static StringPtr Format(String* format, Object* arg0);
    static StringPtr Format(String* format, Object* arg0, Object* arg1);
    static StringPtr Format(String* format, Object* arg0, Object* arg1, Object* arg2);
    static StringPtr Format(String* format, Array< RefPtr<Object> >* args);
    static StringPtr Format(const wchar_t* format, Object* arg0);
    static StringPtr Format(const wchar_t* format, Object* arg0, Object* arg1);
    static StringPtr Format(const wchar_t* format, Object* arg0, Object* arg1, Object* arg2);
    static StringPtr Format(const wchar_t* format, Array< RefPtr<Object> >* args);
    static StringPtr Format(IFormatProvider* provider, const wchar_t* format, int length, 
                 Array< RefPtr<Object> >* args);

// Methods
public:
    RefPtr< Array<wchar_t> > ToCharArray();
    void CopyTo(int sourceIndex, Array<wchar_t>* destination, 
        int destinationIndex, int count);
    const wchar_t* GetCString();

    //
    // Overrides
    //
    override bool Equals(Object* obj);
    override int GetHashCode();
    override StringPtr ToString();

    //
    // Comparing strings
    //
    bool Equals(String* value);
    bool Equals(const wchar_t* value);
    bool StartsWith(String* value);
    bool StartsWith(const wchar_t* value);
    bool EndsWith(String* value);
    bool EndsWith(const wchar_t* value);

    //
    // Copying strings
    //
    StringPtr Substring(int startIndex);
    StringPtr Substring(int startIndex, int length);
    StringPtr Left(int length);
    StringPtr Right(int length);

    //
    // Converting strings
    //
    StringPtr ToLower();
    StringPtr ToUpper();

    //
    // Searching strings
    //
    int IndexOf(wchar_t value, int startIndex = 0);
    int IndexOf(wchar_t value, int startIndex, int count);
    int IndexOf(String* value, int startIndex = 0);
    int IndexOf(String* value, int startIndex, int count);
    int IndexOf(const wchar_t* value, int startIndex = 0);
    int IndexOf(const wchar_t* value, int startIndex, int count);

    int IndexOfAny(Array<wchar_t>* anyOf, int startIndex = 0);
    int IndexOfAny(Array<wchar_t>* anyOf, int startIndex, int count);

    int LastIndexOf(wchar_t value, int startIndex = 0);
    int LastIndexOf(wchar_t value, int startIndex, int count);
    int LastIndexOf(String* value);
    int LastIndexOf(String* value, int startIndex);
    int LastIndexOf(String* value, int startIndex, int count);
    int LastIndexOf(const wchar_t* value, int startIndex = 0);
    int LastIndexOf(const wchar_t* value, int startIndex, int count);

    int LastIndexOfAny(Array<wchar_t>* anyOf, int startIndex = 0);
    int LastIndexOfAny(Array<wchar_t>* anyOf, int startIndex, int count);

    //
    // Trimming and padding
    //
    StringPtr Trim();
    StringPtr Trim(Array<wchar_t>* trimChars);
    StringPtr TrimStart(Array<wchar_t>* trimChars);
    StringPtr TrimEnd(Array<wchar_t>* trimChars);
    StringPtr PadLeft(int totalLength, wchar_t ch = L' ');
    StringPtr PadRight(int totalLength, wchar_t ch = L' ');

    //
    // String manipulation
    //
    StringPtr Insert(int startIndex, String* s);
    StringPtr Insert(int startIndex, const wchar_t* s);
    StringPtr Remove(int startIndex, int count);
    StringPtr Replace(wchar_t oldChar, wchar_t newChar);
    StringPtr Replace(String* oldValue, String* newValue);
    StringPtr Replace(const wchar_t* oldValue, const wchar_t* newValue);
    RefPtr< Array< StringPtr > > Split(wchar_t separator);
    RefPtr< Array< StringPtr > > Split(Array<wchar_t>* separators, int count = INT_MAX);

private:
    wchar_t* InternalGetBuffer();

    enum TrimType
    {
        TrimTypeStart,
        TrimTypeEnd,
        TrimTypeBoth,
    };
    StringPtr TrimImpl(Array<wchar_t>* trimChars, TrimType trimType);

    enum PadType
    {
        PadTypeLeft,
        PadTypeRight,
    };
    StringPtr PadImpl(int totalLength, wchar_t paddingChar, PadType padType);
};

// String Inlines

inline const wchar_t* String::GetCString()
{
    return InternalGetBuffer();
}

inline int String::get_Length()
{
    return (this->_buffer.Length - 1);
}

// String Operators

inline StringPtr operator+(const StringPtr& s1, const wchar_t* s2)
{
    return String::Concat(s1, new String(s2));
}

inline StringPtr operator+(const wchar_t* s1, const StringPtr& s2)
{
    return String::Concat(new String(s1), s2);
}

inline StringPtr operator+(const StringPtr& s1, const StringPtr& s2)
{
    return String::Concat(s1, s2);
}

inline StringPtr operator+=(const StringPtr& s1, const StringPtr& s2)
{
    return String::Concat(s1, s2);
}

inline StringPtr operator+=(const StringPtr& s1, const wchar_t* s2)
{
    return String::Concat(s1, new String(s2));
}

inline bool operator==(const StringPtr& s1, const StringPtr& s2)
{
    return String::Equals(s1, s2);
}

inline bool operator==(const StringPtr& s1, const wchar_t* s2)
{
    return String::Equals(s1, s2);
}

inline bool operator==(int Null, const StringPtr& s)
{
    return (s.Pointer == null);
}

inline bool operator!=(const StringPtr& s1, const StringPtr& s2)
{
    return !String::Equals(s1, s2);
}

inline bool operator!=(const StringPtr& s, int Null)
{
    return (s.Pointer != null);
}

inline bool operator!=(int Null, const StringPtr& s)
{
    return (s.Pointer != null);
}

namespace Collections {

template <>
class EqualsTraits< RefPtr<String> >
{
public:
	static bool Equals(String* a, String* b)
    {
        return String::Equals(a, b);
    }
};

template <>
class CompareTraits< RefPtr<String> >
{
public:
	static int Compare(String* a, String* b)
	{
        return String::CompareOrdinal(a, b);
	}
};

} // namespace Collections

} // namespace Acf

#endif // #ifndef __Acf_String__

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