1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // newDateVector.h: Rcpp R/C++ interface class library -- Date vector support
4 //
5 // Copyright (C) 2016         Dirk Eddelbuettel
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__newDateVector_h
23 #define Rcpp__newDateVector_h
24 
25 #include <RcppCommon.h>
26 
27 namespace Rcpp {
28 
29     class newDateVector : public NumericVector {
30     public:
31         template <int RTYPE, bool NA, typename VEC>
newDateVector(const VectorBase<RTYPE,NA,VEC> & vec)32         newDateVector(const VectorBase<RTYPE,NA,VEC>& vec) : NumericVector(vec) {
33             setClass();
34         }
35 
newDateVector(SEXP vec)36         newDateVector(SEXP vec) : NumericVector(vec) { setClass(); }
newDateVector(int n)37         newDateVector(int n) : NumericVector(n) { setClass(); }
38 
getDates()39         inline std::vector<Date> getDates() const {
40             size_t n = this->size();
41             std::vector<Date> v(n);
42             for (size_t i=0; i<n; i++)
43                 v[i] = (*this)[i];
44             return v;
45         }
46 
47         inline newDateVector &operator=(const newDateVector &rhs) {
48             if (this != &rhs) {
49                 NumericVector::operator=(rhs);
50             }
51 
52             return *this;
53         }
54 
55         friend inline std::ostream &operator<<(std::ostream & s, const newDateVector d);
56 
57     private:
58 
setClass()59         void setClass() {
60             Shield<SEXP> dateclass(Rf_mkString("Date"));
61             Rf_setAttrib(*this, R_ClassSymbol, dateclass);
62         }
63     };
64 
65     inline std::ostream &operator<<(std::ostream & os, const newDateVector d) {
66         size_t n = d.size();
67         for (size_t i=0; i<n; i++) {
68             os << Date(d[i]).format() << " ";
69             if ((i+1) % 8 == 0) os << "\n";
70         }
71         return os;
72     }
73 }
74 
75 #endif
76