Logo ROOT   6.16/01
Reference Guide
TRObject.h
Go to the documentation of this file.
1// @(#)root/r:$Id$
2// Author: Omar Zapata 29/05/2013
3
4/*************************************************************************
5 * Copyright (C) 2013-2014, Omar Andres Zapata Mesa *
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 opetarors 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="http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide"> http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide</a><br>
67 <a href="https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface"> https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface</a>
68
69 @ingroup R
70 */
71 class TRObject: public TObject {
72 friend SEXP Rcpp::wrap<TRObject>(const TRObject &f);
73 private:
74 Rcpp::RObject fObj; //insternal Rcpp::RObject
75 Bool_t fStatus;//status tell if is a valid object
76 public:
77 /**
78 Default constructor
79 */
81 /**
82 Construct a TRObject given a R base object
83 \param robj raw R object
84 */
85 TRObject(SEXP robj);
86 /**
87 Construct a TRObject given a R base object
88 \param robj raw R object
89 \param status if the raw object is valid obj
90 */
91 TRObject(SEXP robj, Bool_t status);
92
93 /**
94 TRObject is a current valid object?
95 \param status if the current object is valid obj
96 */
97 void SetStatus(Bool_t status)
98 {
99 fStatus = status;
100 }
101
102 /**
103 TRObject is a current valid object?
104 \return status if the current object
105 */
107 {
108 return fStatus;
109 }
110
111 /**
112 The R objects can to have associate attributes
113 with this method you can added attribute to TRObject given an object in the template argument.
114 \param name attribute name
115 \param obj object associated to the attribute name in the current TRObject
116 */
117 template<class T> void SetAttribute(const TString name, T obj)
118 {
119 fObj.attr(name.Data()) = obj;
120 }
121
122 /**
123 The R objects can to have associate attributes
124 with this method you can added attribute to TRObject given an object in the template argument.
125 \param name attribute name
126 \return object associated to the attribute name in the current TRObject
127 */
129 {
130 return fObj.attr(name.Data());
131 }
132
133 void operator=(SEXP xx);
134
135 /**
136 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
137 this method lets you wrap those datatypes
138 \param obj template object to be wrapped
139 \return TRObject reference of wrapped object
140 */
141 template<class T> TRObject &Wrap(T obj)
142 {
143 fObj =::Rcpp::wrap(obj);
144 return *this;
145 }
146
147 /**
148 Some datatypes of ROOT or c++ can be wrapped in to a TRObject,
149 this method lets you unwrap those datatypes encapsulate into this TRObject.
150 \note If the current TRObject is not a valid object it will return and empty object and it will print an error message
151 \return template return with the require datatype
152 */
153 template<class T> T As()
154 {
155 if (fStatus) {
156 T data =::Rcpp::as<T>(fObj);
157 return data;
158 } else {
159 Error("Cast Operator", "Can not make the requested data, returning an unknow value");
160 return T();
161 }
162 }
163
164 template<class T> T operator=(TRObject &obj)
165 {
166 return ::Rcpp::as<T>(obj);
167 }
168
169 operator SEXP()
170 {
171 return fObj;
172 }
173
174 operator SEXP() const
175 {
176 return fObj;
177 }
178
179 operator Rcpp::RObject()
180 {
181 return fObj;
182 }
183
184 template <class T> operator T()
185 {
186
187 if (fStatus) {
188 T data =::Rcpp::as<T>(fObj);
189 return data;
190 } else {
191 Error("Cast Operator", "Can not make the requested data, returning an unknow value");
192 return T();
193 }
194 }
195 ClassDef(TRObject, 0) //
196 };
197
198 }
199}
200
201
202#endif
#define f(i)
Definition: RSha256.hxx:104
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
bool Bool_t
Definition: RtypesCore.h:59
#define ClassDef(name, id)
Definition: Rtypes.h:324
This is a class to get ROOT's objects from R's objects.
Definition: TRObject.h:71
Rcpp::RObject fObj
Definition: TRObject.h:74
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:153
void operator=(SEXP xx)
Definition: TRObject.cxx:19
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:117
Bool_t GetStatus()
TRObject is a current valid object?
Definition: TRObject.h:106
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition: TRObject.h:97
T operator=(TRObject &obj)
Definition: TRObject.h:164
TRObject()
Default constructor.
Definition: TRObject.h:80
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:141
Bool_t fStatus
Definition: TRObject.h:75
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:128
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
Basic string class.
Definition: TString.h:131
double T(double x)
Definition: ChebyshevPol.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
SEXP wrap(const TString &s)
Definition: RExports.h:67