Click here to Skip to main content
15,881,819 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.
//
// AcfObjectBase.h
//
// Defines base types for reference type and value type, also 
// boxing/unboxing support.
//

#ifndef __Acf_ObjectBase__
#define __Acf_ObjectBase__

#include <typeinfo.h>	// for type_info

namespace Acf {
    namespace Internal {

class CriticalSection;

    } // namespace Internal
} // namespace Acf

namespace Acf {

using namespace Acf::Language;

class Object; // base for all reference types
    class String; // defined in AcfString.h
    template < class T > class Boxed;

typedef RefPtr<Object>   ObjectPtr;
typedef RefPtr<String>   StringPtr;

//---------------------------------------------------------------------
// class Object, sizeof(Object) = 12 bytes

class Object : public AllocationBase
{
// Fields
protected:
    int _refCount;
    CriticalSection* _critSec;

// Constructor & Destructor
public:
    Object() { }

protected:
    virtual ~Object();

// Static Methods
public:
    static bool Equals(Object* objA, Object* objB);
    static bool ReferenceEquals(Object* objA, Object* objB);

// Methods
public:
    virtual bool Equals(Object* obj);
    virtual int GetHashCode();
    virtual StringPtr ToString();

    // Reference-count routines
    int AddRef();
    int Release();

    // Lock routines
    void _Lock();
    void _Unlock();
    CriticalSection* _GetLock();

private:
    Object(const Object& src);           // no implementation
    void operator=(const Object& src);   // no implementation
};

inline bool Object::ReferenceEquals(Object* objA, Object* objB)
{
    return (objA == objB);
}

//---------------------------------------------------------------------
// class Boxed

template < class T >
class Boxed : public Object
{
// Fields
public:
    T Value;

// Constructor
public:
    Boxed(const T& v) : Value(v)
    {
    }
};

template <class T>
inline RefPtr< Boxed<T> > box(const T& value)
{
    return new Boxed<T>(value);
}

template <class T>
inline T unbox(Object* obj)
{
    Boxed<T>* o = dynamic_cast<Boxed<T>*>(obj);
    if (o == null)
        throw InvalidCastException();
    else
        return o->Value;
}


// Lock wrapper
class ScopedLock
{
private:
    Object* _obj;

public:
    ScopedLock(Object* obj)
    {
        this->_obj = obj;
        this->_obj->_Lock();
    }
    ~ScopedLock()
    {
        this->_obj->_Unlock();
    }
};

#define ACF_LOCK(obj)   Acf::ScopedLock __l(obj);

#ifndef LOCK
#define LOCK ACF_LOCK
#endif

} // namespace Acf

#endif // #ifndef __Acf_ObjectBase__

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