Hi,

Thanks, Inaki, Dirk, and Matthew for the replies to my query. Thank you
Matthew for your code samples, they were very helpful in finding my way
through the problem.

I am trying to wrap some code from a library, though the code base is
rather large so I didn't want to paste that into the rcpp-devel mailing
list. I have uploaded a sample code to Git Hub. You can see it here (
https://github.com/nikhil003/testSingleton).

It is pretty much what Matthew had written down with slight modifications
to reflect my particular case. Hope this gives some idea of how I imagine
it should work. I am very new to Rcpp but I will be spending some time
learning it so that I can have a working knowledge of it.

Any suggestions or guidance is highly appreciated.

Regards,
Nikhil

On Thu, 22 Feb 2024 at 06:18, Matthew Supernaw - NOAA Federal <
matthew.super...@noaa.gov> wrote:

> Hi Nikhil,
>
> Here's an updated version that exposes a FooWrapper class in the module to
> the previous posted code. Hopefully this helps!
>
> class FooWrapper{
> public:
>   Foo* foo_m;
>   FooWrapper(){}
>
>
>   FooWrapper(const FooWrapper& other):foo_m(other.foo_m){
>
>   }
>
>   FooWrapper(Foo* foo):foo_m(foo){
>
>   }
>
>   void SayHello(){
>     Rcpp::Rcout << "From \"void SayHello()\" -> "<<(this->foo_m)<<" -> ";
>     Rcpp::Rcout << "hello from Foo.\n";
>   }
>
> };
>
>
>
> RCPP_EXPOSED_CLASS(FooWrapper)
>
>   FooWrapper GetSingleton(){
>     FooWrapper ret(Foo::GetSingleton());
>     Rcpp::Rcout<<"From \"Foo& GetSingleton()\" -> memory address for
> singleton is " <<ret.foo_m<<"\n";
>     return ret;
>   }
>
>
>
>
>
> RCPP_MODULE(foo) {
>   Rcpp::class_<FooWrapper>("Foo")
>   .constructor()
>   .method("SayHello", &FooWrapper::SayHello);
>   Rcpp::function("GetSingleton", &GetSingleton);
>
> }
>
> On Wed, Feb 21, 2024 at 10:51 AM Matthew Supernaw - NOAA Federal <
> matthew.super...@noaa.gov> wrote:
>
>> Looks like I posted too soon. I just noticed the memory address from the
>> "SayHello" function doesn't match.
>>
>> On Wed, Feb 21, 2024 at 10:47 AM Matthew Supernaw - NOAA Federal <
>> matthew.super...@noaa.gov> wrote:
>>
>>> Hi Nikhil,
>>> I'm not exactly sure what you are trying to do, but I hope this simple
>>> example is helpful.
>>> Thanks.
>>> Matthew
>>>
>>>
>>> #include <Rcpp.h>
>>> using namespace Rcpp;
>>>
>>>
>>> class Foo{
>>> public:
>>>   static Foo* singleton;
>>>
>>>   Foo(){
>>>   }
>>>
>>>   void SayHello(){
>>>     Rcpp::Rcout << "From \"void SayHello()\" -> "<<this<<" -> ";
>>>     Rcpp::Rcout << "hello from Foo.\n";
>>>   }
>>>
>>>   static Foo* GetSingleton(){
>>>
>>>     if(singleton == NULL){
>>>       Foo::singleton = new Foo();
>>>     }
>>>     Rcpp::Rcout<<"From \"static Foo* GetSingleton()\" -> memory address
>>> for singleton is " <<Foo::singleton<<"\n";
>>>     return Foo::singleton;
>>>   }
>>> };
>>>
>>> Foo* Foo::singleton = NULL;
>>>
>>> RCPP_EXPOSED_CLASS(Foo)
>>>
>>>   Foo& GetSingleton(){
>>>     Foo* foo_singleton = Foo::GetSingleton();
>>>     Rcpp::Rcout<<"From \"Foo& GetSingleton()\" -> memory address for
>>> singleton is " <<foo_singleton<<"\n";
>>>     return *foo_singleton;
>>>   }
>>>
>>> RCPP_MODULE(foo) {
>>>   Rcpp::class_<Foo>("Foo")
>>>   .constructor()
>>>   .method("SayHello", &Foo::SayHello);
>>>   Rcpp::function("GetSingleton", &GetSingleton);
>>>
>>> }
>>> //From R
>>> // library(singleton)
>>> // library(Rcpp)
>>> //
>>> // foo <- Rcpp::Module("foo", PACKAGE = "singleton")
>>> //
>>> // foo_singleton<-foo$GetSingleton()
>>> // foo_singleton$SayHello()
>>>
>>> // Output:
>>> // From "static Foo* GetSingleton()" -> memory address for singleton is
>>> 0x7fcdf9387590
>>> // From "Foo& GetSingleton()" -> memory address for singleton is
>>> 0x7fcdf9387590
>>> // From "void SayHello()" -> 0x7fcdf9392b60 -> hello from Foo.
>>>
>>> On Wed, Feb 21, 2024 at 1:34 AM Nikhil Garg <nikhilgarg....@gmail.com>
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> I am looking for some advice or hoping someone can point me in the
>>>> right direction for wrapping a C++ class with singleton using Rcpp. I have
>>>> been using RCPP modules for some of this work but got stuck with a C++
>>>> object which returns a singleton.
>>>>
>>>> Here is the general structure of the object
>>>>
>>>> class Foo {
>>>>     public:
>>>>         Foo();
>>>>         ~Foo();
>>>>
>>>>         static Foo &getFoo();
>>>>         Foo(Foo const&) = delete;
>>>>         void operator=(Foo const&) = delete;
>>>> }
>>>>
>>>> Regards,
>>>> Nikhil
>>>> _______________________________________________
>>>> Rcpp-devel mailing list
>>>> Rcpp-devel@lists.r-forge.r-project.org
>>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>>>
>>>
>>>
>>> --
>>> Matthew Supernaw
>>> *Scientific Software Developer*
>>> *National Oceanic and Atmospheric Administration*
>>> *Office Of Science and Technology*
>>> *NOAA Fisheries | *U.S. Department of Commerce
>>> Phone 248 - 396 - 7797
>>>
>>>
>>>
>>
>> --
>> Matthew Supernaw
>> *Scientific Software Developer*
>> *National Oceanic and Atmospheric Administration*
>> *Office Of Science and Technology*
>> *NOAA Fisheries | *U.S. Department of Commerce
>> Phone 248 - 396 - 7797
>>
>>
>>
>
> --
> Matthew Supernaw
> *Scientific Software Developer*
> *National Oceanic and Atmospheric Administration*
> *Office Of Science and Technology*
> *NOAA Fisheries | *U.S. Department of Commerce
> Phone 248 - 396 - 7797
>
>
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to