Skip to content

Commit 43831ae

Browse files
committed
Merge pull request #454 from romainfrancois/master
Module enhancements
2 parents 4e205f7 + bb2d02e commit 43831ae

File tree

16 files changed

+298
-46
lines changed

16 files changed

+298
-46
lines changed

DESCRIPTION

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 0.12.4.1
3+
Version: 0.12.4.2
44
Date: 2016-03-26
5-
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey,
5+
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey,
66
Qiang Kou, Douglas Bates and John Chambers
77
Maintainer: Dirk Eddelbuettel <[email protected]>
88
Description: The 'Rcpp' package provides R functions as well as C++ classes which
99
offer a seamless integration of R and C++. Many R data types and objects can be
1010
mapped back and forth to C++ equivalents which facilitates both writing of new
11-
code as well as easier integration of third-party libraries. Documentation
12-
about 'Rcpp' is provided by several vignettes included in this package, via the
13-
'Rcpp Gallery' site at <http://gallery.rcpp.org>, the paper by Eddelbuettel and
14-
Francois (2011, JSS), and the book by Eddelbuettel (2013, Springer); see
11+
code as well as easier integration of third-party libraries. Documentation
12+
about 'Rcpp' is provided by several vignettes included in this package, via the
13+
'Rcpp Gallery' site at <http://gallery.rcpp.org>, the paper by Eddelbuettel and
14+
Francois (2011, JSS), and the book by Eddelbuettel (2013, Springer); see
1515
'citation("Rcpp")' for details on these last two.
1616
Depends: R (>= 3.0.0)
1717
Imports: methods, utils

NAMESPACE

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export(Module,
3030
demangle,
3131
sizeof,
3232
cpp_object_initializer,
33-
cpp_object_dummy,
34-
Rcpp.plugin.maker
33+
cpp_object_dummy,
34+
Rcpp.plugin.maker,
35+
copy,
36+
destruct,
37+
is_destructed
3538
)
3639
S3method( print, bytes )
3740
exportClass(RcppClass)
38-
39-

R/Module.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2010 - 2014 John Chambers, Dirk Eddelbuettel and Romain Francois
1+
# Copyright (C) 2010 - 2016 John Chambers, Dirk Eddelbuettel and Romain Francois
22
#
33
# This file is part of Rcpp.
44
#
@@ -128,7 +128,6 @@ new_CppObject_xp <- function(module, pointer, ...) {
128128
new_dummyObject <- function(...)
129129
.External( class__dummyInstance, ...)
130130

131-
132131
# class method for $initialize
133132
cpp_object_initializer <- function(.self, .refClassDef, ..., .object_pointer){
134133
selfEnv <- as.environment(.self)
@@ -438,3 +437,16 @@ cpp_fields <- function( CLASS, where){
438437

439438
.CppClassName <- function(name)
440439
paste0("Rcpp_",name)
440+
441+
copy <- function( obj ){
442+
.Call(copy_constructor, obj$.cppclass, obj$.pointer )
443+
}
444+
445+
destruct <- function(obj){
446+
.Call(destructor, obj$.cppclass, obj$.pointer)
447+
invisible(NULL)
448+
}
449+
450+
is_destructed <- function(obj){
451+
.Call(is_destructed_impl, obj$.pointer)
452+
}

inst/NEWS.Rd

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@
1414
\itemize{
1515
R 3.3.0 Windows with Rtools 3.3 is now supported (Qin Wenfeng in PR \ghpr{451}).
1616
}
17+
\item Changes in Rcpp Modules:
18+
\itemize{
19+
\item New function \code{copy} to invoke the copy constructor of a
20+
C++ class that has been exposed by modules.
21+
\item New function \code{destruct} to explicitely call the
22+
destructor of the underlying C++ object without waiting for the
23+
garbage collector.
24+
\item New function \code{is\_destructed} to check if an object has been
25+
destructed (presumably by \code{destruct} )
26+
}
1727
}
1828
}
19-
29+
2030
\section{Changes in Rcpp version 0.12.4 (2016-03-22)}{
2131
\itemize{
2232
\item Changes in Rcpp API:
@@ -65,13 +75,13 @@
6575
\ghpr{406} by Qiang fixing \ghit{365}).
6676
\item A missing \code{std::hash} function interface for
6777
\code{Rcpp::String} has been addded (PR \ghpr{408} by Qiang fixing
68-
\ghit{84}).
78+
\ghit{84}).
6979
}
7080
\item Changes in Rcpp Attributes:
7181
\itemize{
7282
\item Avoid invalid function names when generating C++ interfaces (PR
7383
\ghpr{403} by JJ fixing \ghit{402}).
74-
\item Insert additional space around \code{&} in function interface (PR
84+
\item Insert additional space around \code{&} in function interface (PR
7585
\ghpr{400} by Kazuki Fukui fixing \ghit{278}).
7686
}
7787
\item Changes in Rcpp Modules:
@@ -95,7 +105,7 @@
95105
by Florian)
96106
\item Before creating a single String object from a \code{SEXP}, ensure
97107
that it is from a vector of length one (PR \ghpr{376} by Dirk, fixing
98-
\ghit{375}).
108+
\ghit{375}).
99109
\item No longer use \code{STRING_ELT} as a left-hand side, thanks to a
100110
heads-up by Luke Tierney (PR \ghpr{378} by Dirk, fixing \ghit{377}).
101111
\item Rcpp Module objects are now checked more carefully (PR \ghpr{381}
@@ -107,7 +117,7 @@
107117
\item \code{operator<<()} now always shows decimal points (PR \ghpr{396}
108118
by Dan)
109119
\item Matrix classes now have a \code{transpose()} function (PR \ghpr{397}
110-
by Dirk fixing \ghit{383})
120+
by Dirk fixing \ghit{383})
111121
\item \code{operator<<()} for complex types was added (PRs \ghpr{398} by
112122
Qiang and \ghpr{399} by Dirk, fixing \ghit{187})
113123
}
@@ -125,20 +135,20 @@
125135
\item Changes in Rcpp Documentation:
126136
\itemize{
127137
\item The \code{NEWS} file now links to GitHub issue tickets and pull
128-
requests.
138+
requests.
129139
\item The \code{Rcpp.bib} file with bibliographic references was updated.
130140
}
131141
}
132142
}
133-
143+
134144
\section{Changes in Rcpp version 0.12.1 (2015-09-10)}{
135145
\itemize{
136146
\item Changes in Rcpp API:
137147
\itemize{
138-
\item Correct use of WIN32 instead of _WIN32 to please Windows 10
148+
\item Correct use of WIN32 instead of _WIN32 to please Windows 10
139149
\item Add an assignment operator to \code{DimNameProxy} (PR \ghpr{339} by Florian)
140150
\item Add vector and matrix accessors \code{.at()} with bounds checking
141-
(PR \ghpr{342} by Florian)
151+
(PR \ghpr{342} by Florian)
142152
\item Correct character vector conversion from single char (PR \ghpr{344} by
143153
Florian fixing issue \ghit{343})
144154
\item Correct on use of \code{R_xlen_t} back to \code{size_t} (PR \ghpr{348} by
@@ -186,13 +196,13 @@
186196
R expressions; this should resolve errors where calling handlers (e.g.
187197
through \code{suppressMessages()}) were not properly respected.
188198
\item All internal length variables have been changed from \code{R_len_t}
189-
to \code{R_xlen_t} to support vectors longer than 2^31-1 elements (via
199+
to \code{R_xlen_t} to support vectors longer than 2^31-1 elements (via
190200
PR \ghpr{303} by Qiang Kou).
191201
\item The sugar function \code{sapply} now supports lambda functions
192202
(addressing \ghit{213} thanks to Matt Dziubinski)
193203
\item The \code{var} sugar function now uses a more robust two-pass
194204
method, supports complex numbers, with new unit tests added (via PR
195-
\ghpr{320} by Matt Dziubinski)
205+
\ghpr{320} by Matt Dziubinski)
196206
\item \code{String} constructors now allow encodings (via PR \ghpr{310}
197207
by Qiang Kou)
198208
\item \code{String} objects are preserving the underlying \code{SEXP}
@@ -201,7 +211,7 @@
201211
\item DataFrame constructors are now a little more careful (via PR
202212
\ghpr{301} by Romain Francois)
203213
\item For R 3.2.0 or newer, \code{Rf_installChar()} is used instead of
204-
\code{Rf_install(CHAR())} (via PR \ghpr{332}).
214+
\code{Rf_install(CHAR())} (via PR \ghpr{332}).
205215
}
206216
\item Changes in Rcpp Attributes:
207217
\itemize{
@@ -233,7 +243,7 @@
233243
matrices (via a pull request by Dmitrii Meleshko).
234244
\item A new \code{empty()} string constructor was added (via another pull
235245
request).
236-
\item Better support for Vectors with a storage policy different from the
246+
\item Better support for Vectors with a storage policy different from the
237247
default, i.e. \code{NoProtectStorage}, was added.
238248
}
239249
\item Changes in Rcpp Attributes:
@@ -258,16 +268,16 @@
258268
\item The \code{Rcpp::Environment} constructor can now use a supplied
259269
parent environment.
260270
\item The \code{Rcpp::Function} constructor can now use a supplied
261-
environment or namespace.
271+
environment or namespace.
262272
\item The \code{attributes_hidden} macro from R is used to shield internal
263273
functions; the \code{R_ext/Visibility.h} header is now included as well.
264-
\item A \code{Rcpp::print} function was added as a wrapper around \code{Rf_PrintValue}.
274+
\item A \code{Rcpp::print} function was added as a wrapper around \code{Rf_PrintValue}.
265275
}
266276
\item Changes in Rcpp Attributes:
267277
\itemize{
268278
\item The \code{pkg_types.h} file is now included in \code{RcppExports.cpp}
269-
if it is present in either the \code{inst/include} or \code{src}.
270-
\item \code{sourceCpp} was modified to allow includes of local files
279+
if it is present in either the \code{inst/include} or \code{src}.
280+
\item \code{sourceCpp} was modified to allow includes of local files
271281
(e.g. \code{#include "foo.hpp"}). Implementation files (*.cc; *.cpp) corresponding
272282
to local includes are also automatically built if they exist.
273283
\item The generated attributes code was simplified with respect to

inst/include/Rcpp/config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#define RCPP_VERSION Rcpp_Version(0,12,4)
3131

3232
// the current source snapshot
33-
#define RCPP_DEV_VERSION RcppDevVersion(0,12,4,1)
33+
#define RCPP_DEV_VERSION RcppDevVersion(0,12,4,2)
3434

3535
#endif
36-

inst/include/Rcpp/module/class.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// class.h: Rcpp R/C++ interface class library -- Rcpp modules
44
//
5-
// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -22,6 +22,21 @@
2222
#ifndef Rcpp_Module_CLASS_h
2323
#define Rcpp_Module_CLASS_h
2424

25+
template <typename Class, bool ok>
26+
struct CopyConstructor {
27+
static Class* get( Class* obj ){
28+
return new Class(*obj) ;
29+
}
30+
} ;
31+
32+
template <typename Class>
33+
struct CopyConstructor<Class,false> {
34+
static Class* get( Class* obj){
35+
stop("no copy constructor available") ;
36+
return obj ;
37+
}
38+
} ;
39+
2540
template <typename Class>
2641
class class_ : public class_Base {
2742
public:
@@ -146,7 +161,22 @@
146161

147162
throw std::range_error( "no valid constructor available for the argument list" ) ;
148163
END_RCPP
149-
}
164+
}
165+
166+
SEXP invoke_copy_constructor( SEXP object ){
167+
BEGIN_RCPP
168+
XP xp(object) ;
169+
return internal::make_new_object<Class>( CopyConstructor< Class, traits::has_copy_constructor<Class>::value >::get(xp) ) ;
170+
END_RCPP
171+
}
172+
173+
SEXP invoke_destructor(SEXP object) {
174+
BEGIN_RCPP
175+
run_finalizer(object);
176+
XP(object).release() ;
177+
return R_NilValue ;
178+
END_RCPP
179+
}
150180

151181
bool has_default_constructor(){
152182
int n = constructors.size() ;
@@ -477,7 +507,6 @@
477507
self* class_pointer ;
478508
std::string typeinfo_name ;
479509

480-
481510
class_( ) : class_Base(), vec_methods(), properties(), specials(0), constructors(), factories() {};
482511

483512

inst/include/Rcpp/module/class_Base.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// class_Base.h: Rcpp R/C++ interface class library -- Rcpp modules
44
//
5-
// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -37,6 +37,8 @@ class class_Base {
3737

3838
virtual void run_finalizer(SEXP){ }
3939

40+
virtual SEXP invoke_copy_constructor(SEXP) = 0 ;
41+
virtual SEXP invoke_destructor(SEXP) = 0 ;
4042
virtual bool has_default_constructor(){ return false ; }
4143
virtual bool has_method( const std::string& ){
4244
return false ;
@@ -80,7 +82,7 @@ class class_Base {
8082
}
8183
void add_enum( const std::string& enum_name, const std::map<std::string, int>& value ){
8284
enums.insert( ENUM_MAP_PAIR( enum_name, value ) ) ;
83-
}
85+
}
8486

8587
std::string name ;
8688
std::string docstring ;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// Copyright (C) 2016 Romain Francois
4+
//
5+
// This file is part of Rcpp.
6+
//
7+
// Rcpp is free software: you can redistribute it and/or modify it
8+
// under the terms of the GNU General Public License as published by
9+
// the Free Software Foundation, either version 2 of the License, or
10+
// (at your option) any later version.
11+
//
12+
// Rcpp is distributed in the hope that it will be useful, but
13+
// WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19+
20+
#ifndef Rcpp__traits__has_copy_constructor__h
21+
#define Rcpp__traits__has_copy_constructor__h
22+
23+
namespace Rcpp{
24+
namespace traits{
25+
26+
#if defined(RCPP_USING_CXX11)
27+
template <typename T> struct has_copy_constructor :
28+
integral_constant<bool, std::is_copy_constructible<T>::value >{} ;
29+
#else
30+
template<typename T> struct has_copy_constructor : true_type {} ;
31+
#endif
32+
33+
} // traits
34+
} // Rcpp
35+
36+
#define RCPP_DISABLE_COPY_CONSTRUCTOR(CLASS) \
37+
namespace Rcpp { namespace traits { \
38+
template <> struct has_copy_constructor<CLASS> : false_type {} ; \
39+
}}
40+
41+
#endif

inst/include/Rcpp/traits/traits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// traits.h: Rcpp R/C++ interface class library -- traits to help wrap
55
//
6-
// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
77
//
88
// This file is part of Rcpp.
99
//
@@ -44,6 +44,7 @@ struct int2type { enum { value = I }; };
4444
#include <Rcpp/traits/named_object.h>
4545
#include <Rcpp/traits/is_convertible.h>
4646
#include <Rcpp/traits/has_iterator.h>
47+
#include <Rcpp/traits/has_copy_constructor.h>
4748
#include <Rcpp/traits/expands_to_logical.h>
4849
#include <Rcpp/traits/matrix_interface.h>
4950
#include <Rcpp/traits/is_sugar_expression.h>
@@ -77,4 +78,3 @@ struct int2type { enum { value = I }; };
7778
#include <Rcpp/traits/is_primitive.h>
7879

7980
#endif
80-

0 commit comments

Comments
 (0)