Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:37

0001 
0002 // Copyright 2021, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #pragma once
0006 #include <sstream>
0007 
0008 #include <JANA/JVersion.h>
0009 
0010 #if JANA2_HAVE_ROOT
0011 #include <TObject.h>
0012 #include <TClass.h>
0013 #include <TDataMember.h>
0014 #include <TMethodCall.h>
0015 #include <TList.h>
0016 #endif // JANA2_HAVE_ROOT
0017 
0018 #include <JANA/JObject.h>
0019 #include <JANA/JEvent.h>
0020 
0021 class JStringification {
0022 
0023 public:
0024 
0025     JStringification() = default;
0026     virtual ~JStringification() = default;
0027 
0028     // These are the main entry points that users will want to use
0029     void GetObjectSummaries(std::map<std::string, JObjectSummary> &objects, std::shared_ptr<const JEvent> &jevent, const std::string &object_name, const std::string factory_tag="") const;
0030     void GetObjectSummariesAsJSON(std::vector<std::string> &json_vec, std::shared_ptr<const JEvent> &jevent, const std::string &object_name, const std::string factory_tag="") const;
0031     std::string ObjectToJSON( const std::string &hexaddr, const JObjectSummary &summary ) const;
0032 
0033     //-----------------------------------------------------------------------------------
0034 
0035     // These 4 templates use SFINAE to allow the GetAddrAsString template below to compile
0036     // even when the template argument is something like a string that cannot be cast to
0037     // an (int) or (unsigned int). We need this because we want to treat char and unsigned
0038     // char variables as numbers and not characters.
0039     template <typename T> void ConvertInt(std::stringstream &ss, T val, std::true_type) const {ss << (int)val;}
0040     template <typename T> void ConvertInt(std::stringstream &ss, T /* val */, std::false_type) const {ss << "unknown";}
0041     template <typename T> void ConvertUInt(std::stringstream &ss, T val, std::true_type) const {ss << "0x" << std::hex << (unsigned int)val << std::dec;}
0042     template <typename T> void ConvertUInt(std::stringstream &ss, T /* val */, std::false_type) const {ss << "unknown";}
0043 
0044     template <typename T> std::string GetAddrAsString(void *addr) const;
0045 
0046 #if JANA2_HAVE_ROOT
0047     std::string GetRootObjectMemberAsString(const TObject *tobj, const TDataMember *memitem, std::string type) const;
0048 #endif // JANA2_HAVE_ROOT
0049 
0050 private:
0051 
0052 
0053 };
0054 
0055 /// The GetAddrAsString template is used to convert an untyped addr
0056 /// into a std::string. It interprets the address as pointing to
0057 /// a primitive object of the same type as the template argument.
0058 // This is done in a very C-style way by doing address arithmetic.
0059 // There is almost certainly a better way to do this, but the
0060 // ROOT documentation is not forthcoming.
0061 template <typename T>
0062 std::string JStringification::GetAddrAsString(void *addr) const {
0063     auto val = *(T *)addr;
0064     std::stringstream ss;
0065     if( std::is_same<T, char>::value ) { // darn char types have to be treated special!
0066         ConvertInt(ss, val, std::is_same<T, char>());
0067     }else if( std::is_same<T, unsigned char>::value ){
0068         ConvertUInt(ss, val, std::is_same<T, unsigned char>());
0069     }else if( std::is_same<T, std::string>::value ){
0070         ss  << val;
0071     }else{
0072         ss << val;
0073     }
0074     return ss.str();
0075 }
0076