question: functor of a sigleton member function using loki

question: functor of a sigleton member function using loki

Post by Martin & Mary-An » Wed, 30 Jul 2003 07:09:40



I am endeavoring to create a Functor of a singleton member function using
the loki library (borland version) and Borland C++ Builder 6.

Unwrapping my typedefs I'm using something along the lines of:

Functor<type1,TYPELIST_1(type2)>
command(TheSingleton::Instance(),&SingletonClass::MemberFunction);

The code will not compile with the complaint of "'operator *' not
implemented in type SingletonClass for arguments of the same type" when the
dereference operator is attempted on my SingletonClass in functor.h.

I'm no guru. Have I used the wrong syntax, can I not do what I would wish or
have I hit a compiler bug?

Please reply to newsgroup or to mll .at. telus .dot. net

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

question: functor of a sigleton member function using loki

Post by llewell » Thu, 31 Jul 2003 07:33:32



Quote:> I am endeavoring to create a Functor of a singleton member function using
> the loki library (borland version) and Borland C++ Builder 6.

> Unwrapping my typedefs I'm using something along the lines of:

> Functor<type1,TYPELIST_1(type2)>
> command(TheSingleton::Instance(),&SingletonClass::MemberFunction);

Unfortunately, this is not enough code to make your problem comprehensible.
    Please post a short but complete code example that is sufficient to
    reproduce the problem. Someone ought to be able to paste the code
    into an editor, compile, and get the error message which is
    troubling you.

Quote:> The code will not compile with the complaint of "'operator *' not
> implemented in type SingletonClass for arguments of the same type" when the
> dereference operator is attempted on my SingletonClass in functor.h.

> I'm no guru. Have I used the wrong syntax, can I not do what I would wish or
> have I hit a compiler bug?

We can't tell.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

question: functor of a sigleton member function using loki

Post by tonci.to.. » Thu, 31 Jul 2003 08:11:52


Quote:> Functor<type1,TYPELIST_1(type2)>
> command(TheSingleton::Instance(),&SingletonClass::MemberFunction);

Does your TheSingleton::Instance() returns reference or pointer to singleton?
Loki functor accepts obj ptr type.
Try following:

Functor<type1,TYPELIST_1(type2)>
command(&TheSingleton::Instance(),&SingletonClass::MemberFunction);

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

question: functor of a sigleton member function using loki

Post by Martin & Mary-An » Thu, 31 Jul 2003 17:15:50


Maybe if I had put my brain in I would have spotted the answer to my
question.

Loki's SingletonHolder::Instance() returns a reference to the
singleton not a pointer to one as required by Functor.

 >Functor<type1,TYPELIST_1(type2)>
 >command(TheSingleton::Instance(),&SingletonClass::MemberFunction);
 >
 >The code will not compile with the complaint of "'operator *' not
 >implemented in type SingletonClass for arguments of the same type"
when the
 >dereference operator is attempted on my SingletonClass in functor.h.
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

question: functor of a sigleton member function using loki

Post by John Di » Thu, 31 Jul 2003 17:12:24



 > I am endeavoring to create a Functor of a singleton member function using
 > the loki library (borland version) and Borland C++ Builder 6.
 >
 > Unwrapping my typedefs I'm using something along the lines of:
 >
 > Functor<type1,TYPELIST_1(type2)>
 > command(TheSingleton::Instance(),&SingletonClass::MemberFunction);
 >
 > The code will not compile with the complaint of "'operator *' not
 > implemented in type SingletonClass for arguments of the same type" when the
 > dereference operator is attempted on my SingletonClass in functor.h.
 >
 > I'm no guru. Have I used the wrong syntax, can I not do what I would wish or
 > have I hit a compiler bug?
 >
 > Please reply to newsgroup or to mll .at. telus .dot. net

The way that I use it is like this, (I have use for parameterizing it
as both single and multithreaded versions):

template <template <class> class ThreadingModel>
class SomeClass
{
public:

   //! The singleton type.
   typedef Loki::SingletonHolder<SomeClass, Loki::PhoenixSingleton,
Loki::CreateStatic, ThreadingModel> Singleton;

   //! Do Something
   void DoSomething( type1, type2, ... )
   {
     ...
   }

private:

   //! Allow CreatePolicy access to class.  (This is hardcoded here...)
   friend class Loki::CreateStatic<SomeClass>;

   //! Constructor
   SomeClass()
   {
     ...
   }

   //! Destructor which does nothing.
   ~SomeClass()
   {}

   //! Copy constructor disabled, illegal to copy SomeClass.
   SomeClass( const SomeClass& );

   //! Assignment operator disabled, illegal to assign SomeClass.
   SomeClass& operator=( const SomeClass& );

   // other stuff...

Quote:};

Then you can use

Loki::Functor<void, TYPELIST_N(types...), ...> functor(
&SomeClass<ThreadingModel>::Singleton::Instance(),
&SomeClass<ThreadingModel>::DoSomething );

where you substitute ThreadingModel with some class.

This works on gcc and intel compiler, have not done any testing on
windows compilers.  I personally like the Singleton typedef, but it
hardcodes the Singleton type to the class.  I usually template it with
the ThreadingModel, and hardcode the Create and Lifetime policies.

Hope this helps.
John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. loki, Sun Forte and functors

I am trying to use loki functors and have found what Andrei warns
about in his intro where he says some compilers cannot compile loki.
Indeed. Has anyone else tried to use loki functors with the Forte 6.2
compiler? I would like to know if there are any workarounds.

Regards,

Andrew M.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. @Home's Bad Service

3. Loki Functor's default constructor considered harmful

4. MPI research and development job opening

5. static call of virtual member funtion using pointer to member function

6. merge tables - rename

7. Threads: using a member function as controlling function

8. New Guide to LANS and Cabling

9. Member Function Pointer to Member Function?

10. casting pointer to member function to pointer to base class member function

11. member functions vs non member functions

12. Having difficulty attempting to call class member functions using pointers to functions

13. function calls using pointer to member function