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