Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Nikhil Garg
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 " < return ret;
>   }
>
>
>
>
>
> RCPP_MODULE(foo) {
>   Rcpp::class_("Foo")
>   .constructor()
>   .method("SayHello", ::SayHello);
>   Rcpp::function("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 
>>> using namespace Rcpp;
>>>
>>>
>>> class Foo{
>>> public:
>>>   static Foo* singleton;
>>>
>>>   Foo(){
>>>   }
>>>
>>>   void SayHello(){
>>> Rcpp::Rcout << "From \"void SayHello()\" -> "< ";
>>> 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 " <>> 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 " <>> return *foo_singleton;
>>>   }
>>>
>>> RCPP_MODULE(foo) {
>>>   Rcpp::class_("Foo")
>>>   .constructor()
>>>   .method("SayHello", ::SayHello);
>>>   Rcpp::function("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 
>>> 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 ();
 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 

Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Matthew Supernaw - NOAA Federal
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 " <("Foo")
  .constructor()
  .method("SayHello", ::SayHello);
  Rcpp::function("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 
>> using namespace Rcpp;
>>
>>
>> class Foo{
>> public:
>>   static Foo* singleton;
>>
>>   Foo(){
>>   }
>>
>>   void SayHello(){
>> Rcpp::Rcout << "From \"void SayHello()\" -> "< ";
>> 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 " <> 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 " <> return *foo_singleton;
>>   }
>>
>> RCPP_MODULE(foo) {
>>   Rcpp::class_("Foo")
>>   .constructor()
>>   .method("SayHello", ::SayHello);
>>   Rcpp::function("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 
>> 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 ();
>>> 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


Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Matthew Supernaw - NOAA Federal
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 
> using namespace Rcpp;
>
>
> class Foo{
> public:
>   static Foo* singleton;
>
>   Foo(){
>   }
>
>   void SayHello(){
> Rcpp::Rcout << "From \"void SayHello()\" -> "< ";
> 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 " < 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 " < return *foo_singleton;
>   }
>
> RCPP_MODULE(foo) {
>   Rcpp::class_("Foo")
>   .constructor()
>   .method("SayHello", ::SayHello);
>   Rcpp::function("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 
> 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 ();
>> 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
___
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


Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Matthew Supernaw - NOAA Federal
Hi Nikhil,
I'm not exactly sure what you are trying to do, but I hope this simple
example is helpful.
Thanks.
Matthew


#include 
using namespace Rcpp;


class Foo{
public:
  static Foo* singleton;

  Foo(){
  }

  void SayHello(){
Rcpp::Rcout << "From \"void SayHello()\" -> "< ";
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 " < memory address for
singleton is " <("Foo")
  .constructor()
  .method("SayHello", ::SayHello);
  Rcpp::function("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 
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 ();
> 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
___
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


Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Dirk Eddelbuettel


On 21 February 2024 at 09:21, Iñaki Ucar wrote:
| Could you please provide more details about what you tried so far and what are
| the issues you found? A link to a public repo with a test case would be even
| better.

Seconded!

I think I also did something like that 'way early' and 'way simply'. In just
one file you can just have.

   class Foo { ... };   // forward declaration, or actual declaration

   static Foo* myfooptr = nullptr;

followed by a few simple Rcpp function to init (ie allocated), set a value,
get a value and maybe destroy at end (even callable via on.exit() from R).

The key really is to differentiate between types Rcpp knows, and those types
or classes you have that it doesn't -- so you have to write accessors in
terms of the types R knows. We must have examples for that somewhere...

Cheers, Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
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


Re: [Rcpp-devel] Wrapping a c++ class with singleton using RCPP Module

2024-02-21 Thread Iñaki Ucar
Could you please provide more details about what you tried so far and what
are the issues you found? A link to a public repo with a test case would be
even better.

Iñaki

El mié., 21 feb. 2024 7:34, Nikhil Garg  escribió:

> 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 ();
> 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
>
___
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