Click here to Skip to main content
15,883,705 members
Articles / Desktop Programming / WTL
Article

CAtomT - An ATOM wrapper for WTL

Rate me:
Please Sign up or sign in to vote.
3.40/5 (5 votes)
25 Dec 2006CPOL 36.9K   165   10   6
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:

C++
///////////////////////////////////////////////////////////////////////////////
// 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;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralAbout ATOM and more... Pin
Remon29-Dec-06 12:48
Remon29-Dec-06 12:48 
GeneralRe: About ATOM and more... Pin
Remon29-Dec-06 13:31
Remon29-Dec-06 13:31 
QuestionWhat is this ATOM? Pin
armentage28-Dec-06 7:44
armentage28-Dec-06 7:44 
AnswerRe: What is this ATOM? Pin
isemenov28-Dec-06 22:13
isemenov28-Dec-06 22:13 
Question? Pin
Shawn Poulson27-Dec-06 8:29
Shawn Poulson27-Dec-06 8:29 
AnswerRe: ? Pin
isemenov28-Dec-06 22:18
isemenov28-Dec-06 22:18 

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.