Click here to Skip to main content
Licence CPOL
First Posted 25 Dec 2006
Views 19,432
Downloads 91
Bookmarked 10 times

CAtomT - An ATOM wrapper for WTL

By | 25 Dec 2006 | Article
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;

License

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

About the Author

isemenov



Russian Federation Russian Federation

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAbout ATOM and more... PinmemberRemon12:48 29 Dec '06  
GeneralRe: About ATOM and more... PinmemberRemon13:31 29 Dec '06  
QuestionWhat is this ATOM? Pinmemberarmentage7:44 28 Dec '06  
AnswerRe: What is this ATOM? Pinmemberisemenov22:13 28 Dec '06  
Question? PinmemberShawn Poulson8:29 27 Dec '06  
AnswerRe: ? Pinmemberisemenov22:18 28 Dec '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Dec 2006
Article Copyright 2006 by isemenov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid