static map as class member? 
Author Message
 static map as class member?

I have a lookup table implemented as map<DWORD, LPCWSTR> that is a member of
my class (hereafter referred to as "CMyClass" and "CMyClass::m_mapLookup").
Once the table is initialized in the constructor, it is read-only for the
lifetime of the class (I use find() to search the map).  Since I can have
multiple instances of CMyClass, I made CMyClass::m_mapLookup static and put
the initialization code in a critical section:

CMyClass()
{
    /* other stuff */
    EnterCriticalSection(&csMap);
    if (!m_mapLookup.empty())
    {
        m_mapLookup.insert(...)
    }
    LeaveCriticalSection(&csMap)

Quote:
}

It seems to work OK, but I'm wondering if there are any problems with this
approach.  Comments?


Thu, 15 Dec 2005 22:12:52 GMT  
 static map as class member?

Quote:

>I have a lookup table implemented as map<DWORD, LPCWSTR> that is a member of
>my class (hereafter referred to as "CMyClass" and "CMyClass::m_mapLookup").
>Once the table is initialized in the constructor, it is read-only for the
>lifetime of the class (I use find() to search the map).  Since I can have
>multiple instances of CMyClass, I made CMyClass::m_mapLookup static and put
>the initialization code in a critical section:

>CMyClass()
>{
>    /* other stuff */
>    EnterCriticalSection(&csMap);
>    if (!m_mapLookup.empty())
>    {
>        m_mapLookup.insert(...)
>    }
>    LeaveCriticalSection(&csMap)
>}

>It seems to work OK, but I'm wondering if there are any problems with this
>approach.  Comments?

I don't think you meant to negate the condition. Beyond that, there's not
much wrong with the approach. An alternative is to wrap the map in its own
class, which you treat as a singleton.

--
Doug Harrison
Microsoft MVP - Visual C++



Fri, 16 Dec 2005 12:02:29 GMT  
 static map as class member?

Quote:

> I have a lookup table implemented as map<DWORD, LPCWSTR> that is a member of
> my class (hereafter referred to as "CMyClass" and "CMyClass::m_mapLookup").
> Once the table is initialized in the constructor, it is read-only for the
> lifetime of the class (I use find() to search the map).  Since I can have
> multiple instances of CMyClass, I made CMyClass::m_mapLookup static and put
> the initialization code in a critical section:

> CMyClass()
> {
>     /* other stuff */
>     EnterCriticalSection(&csMap);
>     if (!m_mapLookup.empty())
>     {
>         m_mapLookup.insert(...)
>     }
>  LeaveCriticalSection(&csMap)
> }

> It seems to work OK, but I'm wondering if there are any problems with this
> approach.  Comments?

Hi Bill,
insert elements in the map, if it is empty -> if( m_mapLookup.empty()
).

An other thing is - map<>::insert may throw an exception (out of
memory). In this case there is no 'LeaveCriticalSection'-call. You can
solve this by using RAII (Resource Acquisition Is Initialization).

Sample:
  struct ScopedLock {
    ScopedLock( CRITICAL_SECTION& cs )
        : m_mtx( &cs ) {
      EnterCriticalSection( m_mtx );
    }
    ~ScopedLock() {
      LeaveCriticalSection( m_mtx );
    }
  private:
    LPCRITICAL_SECTION m_mtx;

    ScopedLock( const ScopedLock& );  // no copying allowed
    ScopedLock& operator=( const ScopedLock& );
  };

so Your code will change to:
 CMyClass() {
     /* other stuff */
     ScopedLock lck( csMap );
     if (m_mapLookup.empty()) {
         m_mapLookup.insert(...)
     }
 }

see also <http://www.boost.org/libs/thread/doc/lock_concept.html>

Greetings
Werner



Fri, 16 Dec 2005 20:47:55 GMT  
 static map as class member?
Thanks, guys.



Quote:
> > I have a lookup table implemented as map<DWORD, LPCWSTR> that is a
member of
> > my class (hereafter referred to as "CMyClass" and

"CMyClass::m_mapLookup").
Quote:
> > Once the table is initialized in the constructor, it is read-only for
the
> > lifetime of the class (I use find() to search the map).  Since I can
have
> > multiple instances of CMyClass, I made CMyClass::m_mapLookup static and
put
> > the initialization code in a critical section:

> > CMyClass()
> > {
> >     /* other stuff */
> >     EnterCriticalSection(&csMap);
> >     if (!m_mapLookup.empty())
> >     {
> >         m_mapLookup.insert(...)
> >     }
> >  LeaveCriticalSection(&csMap)
> > }

> > It seems to work OK, but I'm wondering if there are any problems with
this
> > approach.  Comments?

> Hi Bill,
> insert elements in the map, if it is empty -> if( m_mapLookup.empty()
> ).

> An other thing is - map<>::insert may throw an exception (out of
> memory). In this case there is no 'LeaveCriticalSection'-call. You can
> solve this by using RAII (Resource Acquisition Is Initialization).

> Sample:
>   struct ScopedLock {
>     ScopedLock( CRITICAL_SECTION& cs )
>         : m_mtx( &cs ) {
>       EnterCriticalSection( m_mtx );
>     }
>     ~ScopedLock() {
>       LeaveCriticalSection( m_mtx );
>     }
>   private:
>     LPCRITICAL_SECTION m_mtx;

>     ScopedLock( const ScopedLock& );  // no copying allowed
>     ScopedLock& operator=( const ScopedLock& );
>   };

> so Your code will change to:
>  CMyClass() {
>      /* other stuff */
>      ScopedLock lck( csMap );
>      if (m_mapLookup.empty()) {
>          m_mapLookup.insert(...)
>      }
>  }

> see also <http://www.boost.org/libs/thread/doc/lock_concept.html>

> Greetings
> Werner



Sat, 17 Dec 2005 03:39:46 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. map as a class static member

2. does static class member change class size?

3. Class object as static member of another class

4. how to call a non-static member function of a class from a static member function of the class?

5. accessing static member of Windows service class thro reflection - need help

6. Initialising a static data member of a Class?

7. Initialising a static data member of a Class?

8. static class members - ahh!!

9. Static member in C++ class

10. class with static string member problem

11. static member variable inside class

12. LNK1169 & static const class member variables

 

 
Powered by phpBB® Forum Software