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 @ingroup R
69 */
70 class TRObject: public TObject {
71 friend SEXP Rcpp::wrap<TRObject>(const TRObject &f);
72 private:
73 Rcpp::RObject fObj; //internal Rcpp::RObject
74 Bool_t fStatus;//status tell if is a valid object
75 public:
76 /**
77 Default constructor
78 */
80 /**
81 Construct a TRObject given a R base object
82 \param robj raw R object
83 */
84 TRObject(SEXP robj);
85 /**
86 Construct a TRObject given a R base object
87 \param robj raw R object
88 \param status if the raw object is valid obj
89 */
90 TRObject(SEXP robj, Bool_t status);
91
92 /**
93 TRObject is a current valid object?
94 \param status if the current object is valid obj
95 */
96 void SetStatus(Bool_t status)
97 {
98 fStatus = status;
99 }
100
101 /**
102 TRObject is a current valid object?
103 \return status if the current object
104 */
106 {
107 return fStatus;
108 }
109
110 /**
111 The R objects can to have associate attributes
112 with this method you can added attribute to TRObject given an object in the template argument.
113 \param name attribute name
114 \param obj object associated to the attribute name in the current TRObject
115 */
116 template<class T> void SetAttribute(const TString name, T obj)
117 {
118 fObj.attr(name.Data()) = obj;
119 }
120
121 /**
122 The R objects can to have associate attributes
123 with this method you can added attribute to TRObject given an object in the template argument.
124 \param name attribute name
125 \return object associated to the attribute name in the current TRObject
126 */
128 {
129 return fObj.attr(name.Data());
130 }
131
132 void operator=(SEXP xx);
133
134 /**
135 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
136 this method lets you wrap those datatypes
137 \param obj template object to be wrapped
138 \return TRObject reference of wrapped object
139 */
140 template<class T> TRObject &Wrap(T obj)
141 {
142 fObj =::Rcpp::wrap(obj);
143 return *this;
144 }
145
146 /**
147 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
148 this method lets you unwrap those datatypes encapsulate into this TRObject.
149 \note If the current TRObject is not a valid object it will return and empty object and it will print an error message
150 \return template return with the require datatype
151 */
152 template<class T> T As()
153 {
154 if (fStatus) {
155 T data =::Rcpp::as<T>(fObj);
156 return data;
157 } else {
158 Error("Cast Operator", "Can not make the requested data, returning an unknown value");
159 return T();
160 }
161 }
162
163 template<class T> T operator=(TRObject &obj)
164 {
165 return ::Rcpp::as<T>(obj);
166 }
167
168 operator SEXP()
169 {
170 return fObj;
171 }
172
173 operator SEXP() const
174 {
175 return fObj;
176 }
177
178 operator Rcpp::RObject()
179 {
180 return fObj;
181 }
182
183 template <class T> operator T()
184 {
185
186 if (fStatus) {
187 T data =::Rcpp::as<T>(fObj);
188 return data;
189 } else {
190 Error("Cast Operator", "Can not make the requested data, returning an unknown value");
191 return T();
192 }
193 }
195 };
196
197 }
198}
199
200
201#endif
#define f(i)
Definition RSha256.hxx:104
#define ClassDef(name, id)
Definition Rtypes.h:337
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
This is a class to get ROOT's objects from R's objects.
Definition TRObject.h:70
Rcpp::RObject fObj
Definition TRObject.h:73
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:152
void operator=(SEXP xx)
Definition TRObject.cxx:21
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:116
Bool_t GetStatus()
TRObject is a current valid object?
Definition TRObject.h:105
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition TRObject.h:96
T operator=(TRObject &obj)
Definition TRObject.h:163
TRObject()
Default constructor.
Definition TRObject.h:79
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:140
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:127
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
Basic string class.
Definition TString.h:139
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
SEXP wrap(const TString &s)
Definition RExports.h:82