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
_______________________________________________ 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