65.9K
CodeProject is changing. Read more.
Home

CAtomT - An ATOM wrapper for WTL

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.40/5 (5 votes)

Dec 26, 2006

CPOL
viewsIcon

37413

downloadIcon

167

A simple Win32 ATOM type wrapper.

Introduction

Here is a very simple ATOM type wrapper, which supports the simplest ATOMs API. The class interface is very simple, and doesn't need any description. The class source is really small, so I posted it here:

///////////////////////////////////////////////////////////////////////////////
// CAtomT - ATOM type wrapper

template[bool t_bGlobal]
class CAtomT
{
public:
    ATOM m_Atom;

public:
    enum AtomType
    {
        ATOM_INTEGER = 0,
        ATOM_STRING
    };

public:
    CAtomT(ATOM Atom = 0) : m_Atom(Atom)
    { }

    CAtomT& operator=(const CAtomT& Atom)
    {
        if (this != &Atom)
            m_Atom = Atom;

        return (*this);
    }

    operator ATOM() const
    {
        return m_Atom;
    }

    bool IsValid() const
    {
        return (m_Atom != 0);
    }

    AtomType GetType() const
    {
        ATLASSERT(IsValid());
        return (m_Atom < MAXINTATOM) ? ATOM_INTEGER : ATOM_STRING;
    }

    bool GetName(LPTSTR lpNameBuffer, int nBufferSize) const
    {
        ATLASSERT(IsValid());
        ATLASSERT(lpNameBuffer != NULL);
        if (t_bGlobal)
            return (::GlobalGetAtomName(m_Atom, lpNameBuffer, nBufferSize) != 0);
        else
            return (::GetAtomName(m_Atom, lpNameBuffer, nBufferSize) != 0);
    }

#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
    bool GetName(_CSTRING_NS::CString& strName) const
    {
        ATLASSERT(IsValid());
        const size_t MAX_ATOM_NAME_SIZE = 255;
        LPTSTR lpNameBuffer = strName.GetBufferSetLength(MAX_ATOM_NAME_SIZE);
        if (lpNameBuffer != NULL)
        {
            bool bRes = GetName(lpNameBuffer, MAX_ATOM_NAME_SIZE);
            strName.ReleaseBuffer();
            return bRes;
        }
        else
            return false;
    }
#endif // defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)

    bool Delete()
    {
        ATLASSERT(IsValid());
        if (t_bGlobal)
            m_Atom = ::GlobalDeleteAtom(m_Atom);
        else
            m_Atom = ::DeleteAtom(m_Atom);
        return (!IsValid());
    }

public:
    static CAtomT Add(ATL::_U_STRINGorID AtomName)
    {
        if (t_bGlobal)
            return CAtomT(::GlobalAddAtom(AtomName.m_lpstr));
        else
            return CAtomT(::AddAtom(AtomName.m_lpstr));
    }

    static CAtomT Find(ATL::_U_STRINGorID AtomName)
    {
        if (t_bGlobal)
            return CAtomT(::GlobalFindAtom(AtomName.m_lpstr));
        else
            return CAtomT(::FindAtom(AtomName.m_lpstr));
    }
};

#ifndef _WIN32_WCE
typedef CAtomT<false> CAtom;
#endif // _WIN32_WCE
typedef CAtomT<true> CGlobalAtom;