   dual functions with templates || macro
  in order to support internal and external locking (for MT) a class must look like this:   class test{public:   void DataMT(const int&);// internal lock   void Data(const int&, const TLock<test>&);    // external lockprivate:   int data};void DataMT(const int& ld){    TLock<test>   llock(*this);// scoped internal lock    data = ld;}void Data(const int& ld, const TLock<test>& lrLock){   data = ld;    // using external locking}since functions look much the same, can a tamplete or a macro be used for declaring and defining this two functions?
  I don''t get it...do classes derive from test or are you saying that all classes must support those two functions and do that exact code?Why not just create a base class that does this and have all your classes derive from it?Show us what you''re trying to do a little more (i.e. the context).Regards,Jeff ;  what the heck is going on in this thread?   daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool ;  what the heck ? like i said, when going with MT, a class can support internal and external locking.a NON-MT class would look like this: class nonMTtest{public:    void Data(const int&);    int  Data();private:   int data;}void Data(const int& ldata){  data = ldata;}int Data(){  return(data);}a MT class with internal locking would look like this: class Mutexable{public:   AquireMutex();   ReleaseMutex();private:   MutexmMutex;}// TLock needs Mutexable objecttemplate <class TYPE> class TLock{public:    TLock(TYPE&);    // calls aquiremutex    ~TLock();   // calls releasemutexprivate: TYPE&   mdObjectRef;};class internalMTtest: public Mutexable{public:    void Data(const int&);    int  Data();private:   int data;}void Data(const int& ldata){  TLock<internalMTtest>    guard(*this);  data = ldata;}int Data(){  TLock<internalMTtest>    guard(*this);  return(data);}and a class with external locking: class externalMTtest{public:    void Data(TLock<externalMTtest>&, const int&);    int  Data(TLock<externalMTtest>&);private:   int data;}void Data(TLock<externalMTtest>& lrLock, const int& ldata){  data = ldata;}int Data(TLock<externalMTtest>& lrLock){  return(data);}so i want a class with internal and external locking to look like this: class fullMTtest{public:    void Data(TLock<fullMTtest>&, const int&);    void Data(const int&);    int  Data(TLock<fullMTtest>&);    int  Data();private:   int data;}void Data(TLock<fullMTtest>& lrLock, const int& ldata){  data = ldata;}int Data(TLock<fullMTtest>& lrLock){  return(data);}void Data(const int& ldata){  TLock<fullMTtest>    guard(*this);  data = ldata;}int Data(){  TLock<fullMTtest>    guard(*this);  return(data);}so, instead of me writing four much alike functions, can i "automate" this? in ex: i write only the non MT func void Data(const int&) and this would translate into two functions, on with internal and one with external locking?   ;  I''ll ask again:  is there any reason you cannot implement this as base class once and have your classes derive from it?  Why do you need to write it multiple times?Here''s a silly macro if you insist: #define MAKE_SETTERS(Class, Type, data)  public: \   void Data(const Type& d) { \Lock<Class> guard(*this); data = d; \   } \   void Data(const Type& d, const Lock<Class>& lock) { \data = d; \   } \ Another option is to have a template that wraps up a sinlge primitive: template< typename T >class Lockable {private:  T t;public:  void setInternal(const T& _t) {    Lock<T> guard(*this);    t = _t;  }  void setExternal(const T& _t, const Lock<T>& lock) {    t = _t;  }  int getInternal() {    Lock<T> guard(*this);    return t;  }  int getExternal(const Lock<T>& lock) {    return t;  }};Regards,Jeff ;  Look up  Loki''s multi-threading policy classes. It uses policy classes to set the threading model for an object. Only one set of code for each object, but the objects can be parameterized by threading model. ;  @rypyrtnx for the silly macro...u ask why? if one class was in question, i''d do the hard work and implement them manually, but when more classes are in the play?@sicraneanother lib? ohh man... without help? so how i''m going to understand this one? SmartPtr<Foo, RefCountedMTWrapper<ClassLevelLockable> > pointer; this looks really nice... if only i could get it to work... what i basically need, is a MT safe smart ptr for dynamic objects, which does not lock the whole object (unless u want to), hence the wishes for internal and external locking. but as i understand it, this would have to be done for each object, right?class A{   // has data and data manip};class B: public A{   // has data and data manip};class C: public B{ // has data and data manip};so, how would one do this?   ;  Why do you want to pass the lock to the function and then not do anything with it?What''s the purpose of your "external locking" mechanism?What does it offer over: {Lock<MyMutexType> lock(mtxUsedToGuardTheInstance);theInstance.foo(bar);};quote:Original post by DrPizza  Why do you want to pass the lock to the function and then not do anything with it?I thought about this too, and the answer I came up with was:  by making the function signature that way, you force the user to create an external lock and pass it to the function (even if the function does nothing). ;  rypyr is correct class test{public:void Data(const int&);void Data(const int&, const Lock<test>&);private:    data};void test::Data(const int& ldata){   Lock<test>    guard(*this);   data = ldata;}void test::Data(const int& ldata, const Lock<test>& lrlock){    // using external lock   data = ldata;}void myfunc1(test& arg){   arg.Data(100);   arg.Data(100);   arg.Data(100);   arg.Data(100);   // each call makes a lock}void myfunc2(test& arg){   Lock<test>    guard(arg);   arg.Data(100,guard);   arg.Data(100,guard);   arg.Data(100,guard);   arg.Data(100,guard);   // only one lock for all} 