Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TRObject.h
Go to the documentation of this file.
1// @(#)root/r:$Id$
2// Author: Omar Zapata Omar.Zapata@cern.ch 29/05/2013
3
4/*************************************************************************
5 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11#ifndef ROOT_R_TRObject
12#define ROOT_R_TRObject
13
14#include <RExports.h>
15
16
17namespace ROOT {
18 namespace R {
19
20 /**
21
22 This is a class to get ROOT's objects from R's objects
23 <center><h2>TRObject class</h2></center>
24
25 <p>
26 The TRObject class lets you obtain ROOT's objects from R's objects.<br>
27 It has some basic template operators to convert R's objects into ROOT's datatypes<br>
28 </p>
29 A simple example<br>
30 <p>
31
32 </p>
33
34 \code{.cpp}
35 #include<TRInterface.h>
36 void Proxy()
37 {
38 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
39 ROOT::R::TRObject obj;
40 obj=r.Eval("seq(1,10)");
41 TVectorD v=obj;
42 v.Print();
43 }
44 \endcode
45 Output
46 \code
47
48 Vector (10) is as follows
49
50 | 1 |
51 ------------------
52 0 |1
53 1 |2
54 2 |3
55 3 |4
56 4 |5
57 5 |6
58 6 |7
59 7 |8
60 8 |9
61 9 |10
62
63 \endcode
64
65 <h2>Users Guide </h2>
66 <a href="https://oproject.org/pages/ROOT%20R%20Users%20Guide"> https://oproject.org/pages/ROOT R Users Guide</a><br>
67
68 */
69 class TRObject: public TObject {
70 friend SEXP Rcpp::wrap<TRObject>(const TRObject &f);
71 private:
72 Rcpp::RObject fObj; //internal Rcpp::RObject
73 Bool_t fStatus;//status tell if is a valid object
74 public:
75 /**
76 Default constructor
77 */
79 /**
80 Construct a TRObject given a R base object
81 \param robj raw R object
82 */
83 TRObject(SEXP robj);
84 /**
85 Construct a TRObject given a R base object
86 \param robj raw R object
87 \param status if the raw object is valid obj
88 */
89 TRObject(SEXP robj, Bool_t status);
90
91 /**
92 TRObject is a current valid object?
93 \param status if the current object is valid obj
94 */
95 void SetStatus(Bool_t status)
96 {
97 fStatus = status;
98 }
99
100 /**
101 TRObject is a current valid object?
102 \return status if the current object
103 */
105 {
106 return fStatus;
107 }
108
109 /**
110 The R objects can to have associate attributes
111 with this method you can added attribute to TRObject given an object in the template argument.
112 \param name attribute name
113 \param obj object associated to the attribute name in the current TRObject
114 */
115 template<class T> void SetAttribute(const TString name, T obj)
116 {
117 fObj.attr(name.Data()) = obj;
118 }
119
120 /**
121 The R objects can to have associate attributes
122 with this method you can added attribute to TRObject given an object in the template argument.
123 \param name attribute name
124 \return object associated to the attribute name in the current TRObject
125 */
127 {
128 return fObj.attr(name.Data());
129 }
130
131 void operator=(SEXP xx);
132
133 /**
134 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
135 this method lets you wrap those datatypes
136 \param obj template object to be wrapped
137 \return TRObject reference of wrapped object
138 */
139 template<class T> TRObject &Wrap(T obj)
140 {
141 fObj =::Rcpp::wrap(obj);
142 return *this;
143 }
144
145 /**
146 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
147 this method lets you unwrap those datatypes encapsulate into this TRObject.
148 \note If the current TRObject is not a valid object it will return and empty object and it will print an error message
149 \return template return with the require datatype
150 */
151 template<class T> T As()
152 {
153 if (fStatus) {
154 T data =::Rcpp::as<T>(fObj);
155 return data;
156 } else {
157 Error("Cast Operator", "Can not make the requested data, returning an unknown value");
158 return T();
159 }
160 }
161
162 template<class T> T operator=(TRObject &obj)
163 {
164 return ::Rcpp::as<T>(obj);
165 }
166
167 operator SEXP()
168 {
169 return fObj;
170 }
171
172 operator SEXP() const
173 {
174 return fObj;
175 }
176
177 operator Rcpp::RObject()
178 {
179 return fObj;
180 }
181
182 template <class T> operator T()
183 {
184
185 if (fStatus) {
186 T data =::Rcpp::as<T>(fObj);
187 return data;
188 } else {
189 Error("Cast Operator", "Can not make the requested data, returning an unknown value");
190 return T();
191 }
192 }
194 };
195
196 }
197}
198
199
200#endif
#define f(i)
Definition RSha256.hxx:104
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
char name[80]
Definition TGX11.cxx:148
Rcpp::RObject fObj
Definition TRObject.h:72
T As()
Some datatypes of ROOT or c++ can be wrapped in to a TRObject, this method lets you unwrap those data...
Definition TRObject.h:151
void operator=(SEXP xx)
Definition TRObject.cxx:20
void SetAttribute(const TString name, T obj)
The R objects can to have associate attributes with this method you can added attribute to TRObject g...
Definition TRObject.h:115
Bool_t GetStatus()
TRObject is a current valid object?
Definition TRObject.h:104
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition TRObject.h:95
T operator=(TRObject &obj)
Definition TRObject.h:162
TRObject()
Default constructor.
Definition TRObject.h:78
TRObject & Wrap(T obj)
Some datatypes of ROOT or c++ can be wrapped in to a TRObject, this method lets you wrap those dataty...
Definition TRObject.h:139
TRObject GetAttribute(const TString name)
The R objects can to have associate attributes with this method you can added attribute to TRObject g...
Definition TRObject.h:126
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
TObject()
TObject constructor.
Definition TObject.h:259
Basic string class.
Definition TString.h:138
namespace associated R package for ROOT.
Definition RExports.h:72
SEXP wrap(const TString &s)
Definition RExports.h:83
TString as(SEXP s)
Definition RExports.h:87