5

I am reading the source code of leveldb, esp. regarding mutexlock.

I found this declaration:

class SCOPED_LOCKABLE MutexLock {
 public:
  explicit MutexLock(port::Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu)
      : mu_(mu)  {
    this->mu_->Lock();
  }
  ~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); }

 private:
  port::Mutex *const mu_;
  // No copying allowed
  MutexLock(const MutexLock&);
  void operator=(const MutexLock&);
};

and I found that SCOPED_LOCKABLE is defined as empty, so why use it in the class declaration?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
storm
  • 395
  • 1
  • 4
  • 11
  • 1
    I guess that with some configuration, the macro end up being defined to something (see http://gperftools.googlecode.com/svn-history/r99/trunk/src/base/thread_annotations.h) – Luc Touraille May 29 '13 at 08:07
  • FYI: The code is from Google's open source project leveldb : https://github.com/google/leveldb/blob/77948e7eec0613fb5cbecb7e320b9498607030b5/util/mutexlock.h – Nitish Upreti Sep 14 '15 at 04:26

2 Answers2

3

In class or function definitions if developer need to attach extra characteristic it uses MACROS than hard coding in each class or function definitions. This is a good practice for programming. because one day if you need to change this characteristic you have to change in only one place not everywhere of the code.

Some usage of macros in class definition

#ifdef CONTROLLER_EXPORTS
   #define CONTROLLER_API __declspec(dllexport)
#else
   #define CONTROLLER_API __declspec(dllimport)
#endif

class CONTROLLER_API CConfiguration{
} ;

You can get some more windows related useful clues here. http://msdn.microsoft.com/en-us/library/dabb5z75(v=vs.80).aspx

Even you can use access modifiers also like this, because some time for testing you may need to change the access level temporary.

#define PRIVATE private
#define PUBLIC public

class A{
PRIVATE:
  int m_a;
PUBLIC:
  int m_b;
}

Then what is exact your issue? it can be any useful characteristic define like above. here is one example i got from git

#define SCOPED_LOCKABLE     __attribute__ ((scoped_lockable)) 
  • For details about __attribute__ check here
  • For the source I got above code check here
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
1

It might be defined as something different in a different environment. Sometimes it can affect linkage.

It can also indicate that other headers need to be included to make the library headers properly configured.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99