1 // ChildVector.h: Rcpp R/C++ interface class library -- vector children of lists
2 //
3 // Copyright (C) 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey
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__Vector__ChildVector__h
21 #define Rcpp__Vector__ChildVector__h
22 
23 namespace Rcpp {
24 
25 template <typename T>
26 class ChildVector : public T {
27 
28     public:
29 
ChildVector(SEXP data_,SEXP parent_,R_xlen_t i_)30     ChildVector(SEXP data_, SEXP parent_, R_xlen_t i_):
31         T(data_),
32         parent(parent_),
33         i(i_) {}
34 
ChildVector(const ChildVector & other)35     ChildVector(const ChildVector& other):
36         T(wrap(other)),
37         parent(other.parent),
38         i(other.i) {}
39 
40     inline ChildVector& operator=(const ChildVector& other) {
41         if (this != &other) {
42             this->set__(other);
43             if (parent != NULL && !Rf_isNull(parent)) {
44                 SET_VECTOR_ELT(parent, i, other);
45             }
46         }
47         return *this;
48     }
49 
50     inline ChildVector& operator=(const T& other) {
51         this->set__(other);
52         if (parent != NULL && !Rf_isNull(parent)) {
53             SET_VECTOR_ELT(parent, i, other);
54         }
55         return *this;
56     }
57 
58     template <typename U>
59     inline ChildVector& operator=(const U& other) {
60         Shield<SEXP> wrapped( wrap(other) );
61         T vec = as<T>(wrapped);
62         this->set__(vec);
63         if (parent != NULL && !Rf_isNull(parent)) {
64             SET_VECTOR_ELT(parent, i, vec);
65         }
66         return *this;
67     }
68 
69     private:
70         SEXP parent;
71         R_xlen_t i;
72 };
73 
74 } // namespace Rcpp
75 
76 #endif
77 
78