1#include "TRestReflector.h"
3#include "TEmulatedCollectionProxy.h"
4#include "TRestStringHelper.h"
6#include "TStreamerInfo.h"
62 if (
cl ==
nullptr && dt.size == 0) {
63 cout <<
"In TRestReflector::TRestReflector() : unrecognized type: \"" << _type <<
"\"" << endl;
67 typeinfo =
cl ==
nullptr ? dt.typeinfo :
cl->GetTypeInfo();
69 size =
cl ==
nullptr ? dt.size :
cl->Size();
70 type =
cl ==
nullptr ? dt.name :
cl->GetName();
96 cout <<
"In TRestReflector::Destroy() : cannot free on stack memory!" << endl;
112 for (
unsigned char j = 0; j < bytepreline && i <
size; j++) {
115 save << std::uppercase << std::setfill(
'0') << std::setw(2) << std::hex
116 << ((*(
address + i)) & 0xff) <<
" ";
119 cout << save.str() << endl;
127 if (
type ==
"string") {
134 if (converter !=
nullptr) {
135 return converter->ToString(
address);
137 return Form(
"Type: %s, Address: %p",
type.c_str(),
address);
142 if (
type ==
"string") {
146 if (converter !=
nullptr) {
147 converter->ParseString(
address, str);
149 cout <<
"Method for parsing string to " <<
type <<
" has not been registered!" << endl;
160 if (
cl->GetCollectionProxy() &&
dynamic_cast<TEmulatedCollectionProxy*
>(
cl->GetCollectionProxy())) {
170 if (
type.empty() ||
size == 0 ||
cl ==
nullptr) {
171 cout <<
"Error in CreateDictionary: object is zombie!" << endl;
175 int pos = ((string)
type).find(
'<');
177 string basetype = ((string)
type).substr(0, pos);
178 vector<string> stltypes{
"vector",
"list",
"map",
"set",
"array",
"deque"};
180 for (
auto stltype : stltypes) {
181 if (basetype == stltype) {
186 cout <<
"Error in CreateDictionary: unknown type \"" <<
type <<
"\"" << endl;
191 typeformatted =
Replace(typeformatted,
"<",
"_");
192 typeformatted =
Replace(typeformatted,
",",
"_");
195 string sofilename = REST_USER_PATH + (string)
"/AddonDict/Dict_" + typeformatted +
".so";
199 cout <<
"Loading external dictionary for: \"" <<
type <<
"\":" << endl;
200 cout << sofilename << endl;
204 cout <<
"Error in CreateDictionary: cannot create dictionary, path not writeable!" << endl;
205 cout <<
"path: \"" << REST_USER_PATH <<
"\"" << endl;
206 cout <<
"This is possible in case you are using public installation of REST, install one by your "
211 if (system(Form(
"mkdir -p %s/AddonDict", REST_USER_PATH.c_str())) != 0) {
212 cout <<
"mkdir failed to create directory" << endl;
216 string linkdeffilename = REST_USER_PATH + (string)
"/AddonDict/LinkDef.h";
217 ofstream ofs(linkdeffilename);
218 ofs <<
"#include <map>" << endl;
219 ofs <<
"#include <vector>" << endl;
220 ofs <<
"#include <map>" << endl;
221 ofs <<
"#include <set>" << endl;
222 ofs <<
"#include <list>" << endl;
223 ofs <<
"#include <array>" << endl;
224 ofs <<
"#ifdef __ROOTCLING__" << endl;
225 ofs <<
"#pragma link C++ class " <<
type <<
";" << endl;
226 ofs <<
"#endif" << endl;
229 string cxxfilename = REST_USER_PATH + (string)
"/AddonDict/" + typeformatted +
".cxx";
231 cout <<
"Creating external dictionary for: \"" <<
type <<
"\":" << endl;
232 cout << sofilename << endl;
234 if (system(Form(
"rootcling -f %s -c %s", cxxfilename.c_str(), linkdeffilename.c_str())) != 0) {
235 cout <<
"rootcling failed to generate dictionary" << endl;
239 if (system(Form(
"gcc %s `root-config --cflags` "
240 "`root-config --libs` -lGui -lGeom -lGdml -lMinuit -L/usr/lib64 "
241 "-lstdc++ -shared -fPIC -o %s",
242 cxxfilename.c_str(), sofilename.c_str())) != 0) {
243 cout <<
"gcc failed to generate library for the dictionary" << endl;
248 gSystem->Load(sofilename.c_str());
249 RESTListOfClasses_typeid.clear();
250 RESTListOfClasses_typename.clear();
270 cout <<
"In TRestReflector::CloneTo() : the ptr is zombie! " << endl;
275 cout <<
"In TRestReflector::CloneTo() : type doesn't match! (This :" << from.
type
276 <<
", Target : " << to.
type <<
")" << endl;
281 if (converter !=
nullptr) {
284 cout <<
"Method for cloning type: \"" << from.
type <<
"\" has not been registered!" << endl;
290 TDataMember* mem =
cl->GetDataMember(
name.c_str());
291 if (mem ==
nullptr) {
293 TVirtualStreamerInfo* vs =
cl->GetStreamerInfo();
294 TObjArray* ses = vs->GetElements();
295 int n = ses->GetLast() + 1;
296 for (
int i = 0; i < n; i++) {
297 TStreamerElement* ele = (TStreamerElement*)ses->At(i);
298 string type = ele->GetTypeName();
299 if (
type ==
"BASE") {
300 char* addr =
address + ele->GetOffset();
301 type = ele->GetClass()->GetName();
306 char* addr =
address + mem->GetOffset();
307 string type = mem->GetTypeName();
318 TList* list =
cl->GetListOfDataMembers();
320 TDataMember* mem = (TDataMember*)list->At(ID);
321 char* addr =
address + mem->GetOffset();
322 string type = mem->GetTypeName();
323 string name = mem->GetName();
333 vector<string> dataMembers;
336 TVirtualStreamerInfo* vs =
cl->GetStreamerInfo();
337 TObjArray* ses = vs->GetElements();
338 int n = ses->GetLast() + 1;
339 for (
int i = 0; i < n; i++) {
340 TStreamerElement* ele = (TStreamerElement*)ses->At(i);
341 string type = ele->GetTypeName();
342 if (
type ==
"BASE") {
343 char* addr =
address + ele->GetOffset();
344 type = ele->GetClass()->GetName();
347 dataMembers.insert(dataMembers.end(), baseDataMembers.begin(), baseDataMembers.end());
352 TList* list =
cl->GetListOfDataMembers();
353 for (
int i = 0; i < list->GetSize(); i++) {
354 TDataMember* mem = (TDataMember*)list->At(i);
355 string name = mem->GetName();
357 dataMembers.push_back(
name);
373 return cl->GetNdata();
std::string GetDataMemberValueString(const std::string &name)
Get the value of datamember as std::string.
void Assembly()
Assembly a new object, and save its address. The old object will be destroied if not null.
TRestReflector()
Default constructor.
std::string type
Type of the wrapped object.
int size
Size of the object.
void Destroy() const
Destroy the current object. It will make the class to be zombie.
char * address
Address of the wrapped object.
int GetNumberOfDataMembers() const
Get the number of data members of a class.
void operator>>(const TRestReflector &to)
Deep copy the content of the wrapped object to to.
TClass * cl
Pointer to the corresponding TClass helper, if the wrapped object is in class type.
bool is_data_type
Pointer to the corresponding TDataType helper, if the wrapped object is in data type.
std::vector< std::string > GetListOfDataMembers() const
Get a list of the class's datamembers as a std::vector of std::string, including those from base clas...
bool IsZombie() const
If this object type wrapper is invalid.
std::string ToString() const
Convert the wrapped object to std::string.
std::string name
Name field.
void PrintMemory(int bytepreline=16)
Print the Hex memory std::map of the wrappered object.
const std::type_info * typeinfo
value of typeid(T).name() of the wrapped object
bool onheap
If on heap, we can call Destroy() to TRestReflector. True only when initialized from Assembly()
TRestReflector GetDataMember(const std::string &name)
Find the class's datamember as TRestReflector object, including those from base class.
void ParseString(const std::string &str) const
Set the value of the wrapped object from std::string.
int InitDictionary()
Prepare the ROOT dictionary for this type.
This namespace serves for the reflection functionality.
TClass * GetClassQuick(std::string type)
TRestReflector WrapType(const std::string &typeName)
Wrap information an object of type: typeName, memory is not allocated.
void CloneAny(const TRestReflector &from, const TRestReflector &to)
Deep copy the content of object from to to
TRestReflector Assembly(const std::string &typeName)
Assembly an object of type: typeName, returning the allocated memory address and size.
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.