<commit_before>// @(#)root/cintex:$Name:$:$Id:$
// Author: <NAME> 2005

// Copyright CERN, CH-1211 Geneva 23, 2004-2005, All rights reserved.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.

#include "ROOTClassEnhancer.h"
#include "CINTdefs.h"
#include "CINTFunctional.h"
#include "Cintex/Cintex.h"
#include "TROOT.h"
#include "TClass.h"
#include "TClassEdit.h"
#include "TClassTable.h"
#include "TClassStreamer.h"
#include "TCollectionProxy.h"
#include "TVirtualCollectionProxy.h"
#include "TMemberInspector.h"
#include "RVersion.h"
#include "Reflex/Reflex.h"
#include "Reflex/Tools.h"
#include "Reflex/Builder/TypeBuilder.h"
#include "Reflex/Builder/CollectionProxy.h"
#include "Api.h"

#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
  #include "TVirtualIsAProxy.h"
#endif

using namespace ROOT::Reflex;
using namespace ROOT::Cintex;
using namespace std;

namespace ROOT { namespace Cintex {

  class IsAProxy;

  class ROOTClassEnhancerInfo  {

    Type                     fType;
    string                   fName;
    TClass*                  fTclass;
    TClass*                  fLastClass;
    std::map<const std::type_info*,TClass*> fSub_types;
    const  std::type_info*   fLastType;
    const  std::type_info*   fMyType;
    bool                     fIsVirtual;
    ROOT::TGenericClassInfo* fClassInfo;
  #if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
    IsAProxy*                fIsa_func;
  #else
    IsAFunc_t                fIsa_func;
  #endif
    VoidFuncPtr_t            fDictionary_func;

  public:
    ROOTClassEnhancerInfo(Type& t);
    virtual ~ROOTClassEnhancerInfo();

    virtual void Setup(void);
    virtual void CreateInfo(void);
    TClass* tclass() {  
      if ( fTclass == 0 ) {
        fTclass = ROOT::GetROOT()->GetClass( Name().c_str() /*, kFALSE */);
      }
      return fTclass; 
    }
    const Type&   TypeGet() const { return fType; }
    const string& Name() const { return fName; }
    ROOT::TGenericClassInfo* info() const { return fClassInfo; }

    void addFunction( const std::string& Name, const ROOT::Reflex::Type& sig,
                        ROOT::Reflex::StubFunction stubFP, void*  stubCtx, int );
    TClass* IsA(const void* obj);
    static void* stub_IsA2(void* ctxt, void* obj);
    static void* stub_IsA(void*, const std::vector<void*>&, void*);
    static void* stub_Streamer(void*, const std::vector<void*>&, void*);
    static void* stub_StreamerNVirtual(void*, const std::vector<void*>&, void*);
    static void* stub_Dictionary(void*, const std::vector<void*>&, void*);
    static void* stub_ShowMembers(void*, const std::vector<void*>&, void*);
    static void  stub_ShowMembers(TClass*, const ROOT::Reflex::Type&, void*, TMemberInspector&, char*);
    static void  stub_Dictionary( void* ctx );
    static TClass* default_CreateClass(Type typ, ROOT::TGenericClassInfo* info);
  };

#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
  class IsAProxy : public TVirtualIsAProxy {
  public:
    IsAProxy( ROOTClassEnhancerInfo* info ) : fInfo(info), fClass(0) {}
    void SetClass(TClass *cl) { fClass = cl;}
    TClass* operator()(const void *obj){ return obj == 0 ? fClass : fInfo->IsA(obj); }
  private:
    ROOTClassEnhancerInfo* fInfo;
    TClass* fClass;
  };
#endif

  class ROOTEnhancerCont : public std::vector<ROOTClassEnhancerInfo*>  {
  public: 
    ROOTEnhancerCont() {}
    ~ROOTEnhancerCont()  {
      for(std::vector<ROOTClassEnhancerInfo*>::iterator j=begin(); j!= end(); ++j)
        delete (*j);
      clear();
    }
  };

  std::vector<ROOTClassEnhancerInfo*>& rootEnhancerInfos()  {
    static ROOTEnhancerCont s_cont;
    return s_cont;
  }

  ROOTClassEnhancer::ROOTClassEnhancer(const ROOT::Reflex::Type& cl)  {
    fClass = CleanType(cl);
    fName  = CintName(fClass);
  }

  ROOTClassEnhancer::~ROOTClassEnhancer() {
  }

  void ROOTClassEnhancer::Setup() {
    ROOTClassEnhancerInfo* p = new ROOTClassEnhancerInfo(fClass);
    fEnhancerinfo = p;
    p->Setup();
  }

  void ROOTClassEnhancer::CreateInfo() {
    if ( fEnhancerinfo ) {
     ROOTClassEnhancerInfo* p = (ROOTClassEnhancerInfo*)fEnhancerinfo;
     p->CreateInfo();
    }
  }


  /// Access streamer info from a void (polymorph) pointer
  TClass* accessType(const TClass* cl, const void* /* ptr */)  {
    return (TClass*)cl;
  }

  ROOTClassEnhancerInfo::ROOTClassEnhancerInfo(Type& t) : 
    fTclass(0), fLastClass(0), fLastType(0)
  {
    fType = CleanType(t);
    fName = CintName(fType);
    rootEnhancerInfos().push_back(this);
    fMyType = &t.TypeInfo();
    fIsVirtual = TypeGet().IsVirtual();
    fClassInfo = 0;
    fIsa_func = 0;
    fDictionary_func = 0;
 }

  ROOTClassEnhancerInfo::~ROOTClassEnhancerInfo() {
    fSub_types.clear();
    if ( fClassInfo ) delete fClassInfo;
    #if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
      // fIsa_func is deleted by ROOT
    #else
      if ( fIsa_func ) Free_function((void*)fIsa_func);
    #endif
    if ( fDictionary_func ) Free_function((void*)fDictionary_func);
  }

  void ROOTClassEnhancerInfo::Setup() {
    std::string nam = TypeGet().Name(SCOPED);
    if ( Cintex::Debug() > 1 )  {
      std::cout << "Enhancing:" << nam << std::endl;
    }
    if ( ! IsSTLext(nam) && (IsSTL(nam) || IsSTLinternal(nam)) )  {
      //--- create TGenericClassInfo Instance
      //createInfo();
      return;
    }
    else    {
      Type int_t  = Type::ByName("int");
      Type void_t = Type::ByName("void");
      Type char_t = Type::ByName("char");
      Type signature;
      void* ctxt = this;

      //--- adding TClass* IsA()
      signature = FunctionTypeBuilder( PointerBuilder(TypeBuilder("TClass")));
      addFunction("IsA", signature, stub_IsA, ctxt, 0);
      //--- adding void Data_ShowMembers(void *, TMemberInspector&, char*)
      signature = FunctionTypeBuilder( void_t, 
                                      ReferenceBuilder(TypeBuilder("TMemberInspector")),
                                      PointerBuilder(char_t));
      addFunction("ShowMembers", signature, stub_ShowMembers, ctxt, VIRTUAL);
      signature = FunctionTypeBuilder( void_t, ReferenceBuilder(TypeBuilder("TBuffer")));
      addFunction("Streamer", signature, stub_Streamer, ctxt, VIRTUAL);
      addFunction("StreamerNVirtual", signature, stub_StreamerNVirtual, ctxt, 0);
    }
    //--- create TGenericClassInfo Instance
    //createInfo();
  }

  void ROOTClassEnhancerInfo::CreateInfo() {
    //---Check is the the dictionary is already defined for the class
    VoidFuncPtr_t dict = TClassTable::GetDict(Name().c_str());
    if ( dict ) return;

    void* context = this;
#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
    fIsa_func = new IsAProxy(this);
#else
    fIsa_func = (IsAFunc_t)Allocate_1arg_function(context, stub_IsA2);
#endif
    fDictionary_func = Allocate_void_function(context, stub_Dictionary);

    ::ROOT::TGenericClassInfo* info = new ::ROOT::TGenericClassInfo( 
              Name().c_str(),                     // Class Name 
              "xxx",                              // declaration file Name 
              1,                                  // declaration line number
              TypeGet().TypeInfo(),                  // typeid 
              ROOT::DefineBehavior(0,0),          // default behavior
              0,                                  // show members function 
              fDictionary_func,                  // dictionary function 
              fIsa_func,                         // IsA function
              0,                                  // pragma bits
              TypeGet().SizeOf()                     // sizeof
           );
    info->SetImplFile("yyy", 1);
    //----Fill the New and Deletete functions
    Member getfuncs = TypeGet().MemberByName("__getNewDelFunctions");
    if( getfuncs ) {
      NewDelFunctions* newdelfunc = (NewDelFunctions*)( getfuncs.Invoke().Address() );
      if ( newdelfunc ) {
        info->SetNew(newdelfunc->New);
        info->SetNewArray(newdelfunc->NewArray);
        info->SetDelete(newdelfunc->Delete);
        info->SetDeleteArray(newdelfunc->DeleteArray);
        info->SetDestructor(newdelfunc->Destructor);
      }
    }
    fClassInfo = info;
  }

  struct fornamespace {};
  void ROOTClassEnhancer::CreateClassForNamespace(const std::string& Name ) {

    ROOT::CreateClass(Name.c_str(),         // Name
                      0,                    // version
                      typeid(fornamespace), // typeid
                      0,                    // TVirtualIsAProxy *isa,
                      0,                    // ShowMembersFunc_t show,
                      "xxx",                // definition file
                      "yyy",                // implementation file
                      1,                    // definition line number
                      1 );                  // implementation line number
  }

  void ROOTClassEnhancerInfo::addFunction( const std::string& Name, 
                                           const Type & sig,
                                           StubFunction stubFP, 
                                           void*  stubCtx,
                                           int mods)
  {
    fType.AddFunctionMember( Name.c_str(), sig, stubFP, stubCtx, 0, PUBLIC | mods );
  }

  inline static ROOTClassEnhancerInfo& context(void* ctxt)  {
    if ( ctxt )  {
      return *(ROOTClassEnhancerInfo*)ctxt;
    }
    throw std::runtime_error("Invalid stub context passes to emultated function!");
  }


  void* ROOTClassEnhancerInfo::stub_IsA(void* obj, const vector<void*>&, void* ctx) {
    return context(ctx).IsA(obj);
  }
  void* ROOTClassEnhancerInfo::stub_IsA2(void* ctx, void* obj) {
    return context(ctx).IsA(obj);
  }

  struct DynamicStruct  {    virtual ~DynamicStruct() {}  };<commit_msg>Do not use xxx, yyy as dummy decl and impl file names, but empty strings<commit_after>// @(#)root/cintex:$Name:  $:$Id: ROOTClassEnhancer.cxx,v 1.6 2005/11/17 14:12:33 roiser Exp $
// Author: <NAME> 2005

// Copyright CERN, CH-1211 Geneva 23, 2004-2005, All rights reserved.
//
// Permission to use, copy, modify, and distribute this software for any
// purpose is hereby granted without fee, provided that this copyright and
// permissions notice appear in all copies and derivatives.
//
// This software is provided "as is" without express or implied warranty.

#include "ROOTClassEnhancer.h"
#include "CINTdefs.h"
#include "CINTFunctional.h"
#include "Cintex/Cintex.h"
#include "TROOT.h"
#include "TClass.h"
#include "TClassEdit.h"
#include "TClassTable.h"
#include "TClassStreamer.h"
#include "TCollectionProxy.h"
#include "TVirtualCollectionProxy.h"
#include "TMemberInspector.h"
#include "RVersion.h"
#include "Reflex/Reflex.h"
#include "Reflex/Tools.h"
#include "Reflex/Builder/TypeBuilder.h"
#include "Reflex/Builder/CollectionProxy.h"
#include "Api.h"

#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
  #include "TVirtualIsAProxy.h"
#endif

using namespace ROOT::Reflex;
using namespace ROOT::Cintex;
using namespace std;

namespace ROOT { namespace Cintex {

  class IsAProxy;

  class ROOTClassEnhancerInfo  {

    Type                     fType;
    string                   fName;
    TClass*                  fTclass;
    TClass*                  fLastClass;
    std::map<const std::type_info*,TClass*> fSub_types;
    const  std::type_info*   fLastType;
    const  std::type_info*   fMyType;
    bool                     fIsVirtual;
    ROOT::TGenericClassInfo* fClassInfo;
  #if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
    IsAProxy*                fIsa_func;
  #else
    IsAFunc_t                fIsa_func;
  #endif
    VoidFuncPtr_t            fDictionary_func;

  public:
    ROOTClassEnhancerInfo(Type& t);
    virtual ~ROOTClassEnhancerInfo();

    virtual void Setup(void);
    virtual void CreateInfo(void);
    TClass* tclass() {  
      if ( fTclass == 0 ) {
        fTclass = ROOT::GetROOT()->GetClass( Name().c_str() /*, kFALSE */);
      }
      return fTclass; 
    }
    const Type&   TypeGet() const { return fType; }
    const string& Name() const { return fName; }
    ROOT::TGenericClassInfo* info() const { return fClassInfo; }

    void addFunction( const std::string& Name, const ROOT::Reflex::Type& sig,
                        ROOT::Reflex::StubFunction stubFP, void*  stubCtx, int );
    TClass* IsA(const void* obj);
    static void* stub_IsA2(void* ctxt, void* obj);
    static void* stub_IsA(void*, const std::vector<void*>&, void*);
    static void* stub_Streamer(void*, const std::vector<void*>&, void*);
    static void* stub_StreamerNVirtual(void*, const std::vector<void*>&, void*);
    static void* stub_Dictionary(void*, const std::vector<void*>&, void*);
    static void* stub_ShowMembers(void*, const std::vector<void*>&, void*);
    static void  stub_ShowMembers(TClass*, const ROOT::Reflex::Type&, void*, TMemberInspector&, char*);
    static void  stub_Dictionary( void* ctx );
    static TClass* default_CreateClass(Type typ, ROOT::TGenericClassInfo* info);
  };

#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
  class IsAProxy : public TVirtualIsAProxy {
  public:
    IsAProxy( ROOTClassEnhancerInfo* info ) : fInfo(info), fClass(0) {}
    void SetClass(TClass *cl) { fClass = cl;}
    TClass* operator()(const void *obj){ return obj == 0 ? fClass : fInfo->IsA(obj); }
  private:
    ROOTClassEnhancerInfo* fInfo;
    TClass* fClass;
  };
#endif

  class ROOTEnhancerCont : public std::vector<ROOTClassEnhancerInfo*>  {
  public: 
    ROOTEnhancerCont() {}
    ~ROOTEnhancerCont()  {
      for(std::vector<ROOTClassEnhancerInfo*>::iterator j=begin(); j!= end(); ++j)
        delete (*j);
      clear();
    }
  };

  std::vector<ROOTClassEnhancerInfo*>& rootEnhancerInfos()  {
    static ROOTEnhancerCont s_cont;
    return s_cont;
  }

  ROOTClassEnhancer::ROOTClassEnhancer(const ROOT::Reflex::Type& cl)  {
    fClass = CleanType(cl);
    fName  = CintName(fClass);
  }

  ROOTClassEnhancer::~ROOTClassEnhancer() {
  }

  void ROOTClassEnhancer::Setup() {
    ROOTClassEnhancerInfo* p = new ROOTClassEnhancerInfo(fClass);
    fEnhancerinfo = p;
    p->Setup();
  }

  void ROOTClassEnhancer::CreateInfo() {
    if ( fEnhancerinfo ) {
     ROOTClassEnhancerInfo* p = (ROOTClassEnhancerInfo*)fEnhancerinfo;
     p->CreateInfo();
    }
  }


  /// Access streamer info from a void (polymorph) pointer
  TClass* accessType(const TClass* cl, const void* /* ptr */)  {
    return (TClass*)cl;
  }

  ROOTClassEnhancerInfo::ROOTClassEnhancerInfo(Type& t) : 
    fTclass(0), fLastClass(0), fLastType(0)
  {
    fType = CleanType(t);
    fName = CintName(fType);
    rootEnhancerInfos().push_back(this);
    fMyType = &t.TypeInfo();
    fIsVirtual = TypeGet().IsVirtual();
    fClassInfo = 0;
    fIsa_func = 0;
    fDictionary_func = 0;
 }

  ROOTClassEnhancerInfo::~ROOTClassEnhancerInfo() {
    fSub_types.clear();
    if ( fClassInfo ) delete fClassInfo;
    #if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
      // fIsa_func is deleted by ROOT
    #else
      if ( fIsa_func ) Free_function((void*)fIsa_func);
    #endif
    if ( fDictionary_func ) Free_function((void*)fDictionary_func);
  }

  void ROOTClassEnhancerInfo::Setup() {
    std::string nam = TypeGet().Name(SCOPED);
    if ( Cintex::Debug() > 1 )  {
      std::cout << "Enhancing:" << nam << std::endl;
    }
    if ( ! IsSTLext(nam) && (IsSTL(nam) || IsSTLinternal(nam)) )  {
      //--- create TGenericClassInfo Instance
      //createInfo();
      return;
    }
    else    {
      Type int_t  = Type::ByName("int");
      Type void_t = Type::ByName("void");
      Type char_t = Type::ByName("char");
      Type signature;
      void* ctxt = this;

      //--- adding TClass* IsA()
      signature = FunctionTypeBuilder( PointerBuilder(TypeBuilder("TClass")));
      addFunction("IsA", signature, stub_IsA, ctxt, 0);
      //--- adding void Data_ShowMembers(void *, TMemberInspector&, char*)
      signature = FunctionTypeBuilder( void_t, 
                                      ReferenceBuilder(TypeBuilder("TMemberInspector")),
                                      PointerBuilder(char_t));
      addFunction("ShowMembers", signature, stub_ShowMembers, ctxt, VIRTUAL);
      signature = FunctionTypeBuilder( void_t, ReferenceBuilder(TypeBuilder("TBuffer")));
      addFunction("Streamer", signature, stub_Streamer, ctxt, VIRTUAL);
      addFunction("StreamerNVirtual", signature, stub_StreamerNVirtual, ctxt, 0);
    }
    //--- create TGenericClassInfo Instance
    //createInfo();
  }

  void ROOTClassEnhancerInfo::CreateInfo() {
    //---Check is the the dictionary is already defined for the class
    VoidFuncPtr_t dict = TClassTable::GetDict(Name().c_str());
    if ( dict ) return;

    void* context = this;
#if ROOT_VERSION_CODE >= ROOT_VERSION(5,1,1)
    fIsa_func = new IsAProxy(this);
#else
    fIsa_func = (IsAFunc_t)Allocate_1arg_function(context, stub_IsA2);
#endif
    fDictionary_func = Allocate_void_function(context, stub_Dictionary);

    ::ROOT::TGenericClassInfo* info = new ::ROOT::TGenericClassInfo( 
              Name().c_str(),                     // Class Name 
              "",                              // declaration file Name 
              1,                                  // declaration line number
              TypeGet().TypeInfo(),                  // typeid 
              ROOT::DefineBehavior(0,0),          // default behavior
              0,                                  // show members function 
              fDictionary_func,                  // dictionary function 
              fIsa_func,                         // IsA function
              0,                                  // pragma bits
              TypeGet().SizeOf()                     // sizeof
           );
    info->SetImplFile("", 1);
    //----Fill the New and Deletete functions
    Member getfuncs = TypeGet().MemberByName("__getNewDelFunctions");
    if( getfuncs ) {
      NewDelFunctions* newdelfunc = (NewDelFunctions*)( getfuncs.Invoke().Address() );
      if ( newdelfunc ) {
        info->SetNew(newdelfunc->New);
        info->SetNewArray(newdelfunc->NewArray);
        info->SetDelete(newdelfunc->Delete);
        info->SetDeleteArray(newdelfunc->DeleteArray);
        info->SetDestructor(newdelfunc->Destructor);
      }
    }
    fClassInfo = info;
  }

  struct fornamespace {};
  void ROOTClassEnhancer::CreateClassForNamespace(const std::string& Name ) {

    ROOT::CreateClass(Name.c_str(),         // Name
                      0,                    // version
                      typeid(fornamespace), // typeid
                      0,                    // TVirtualIsAProxy *isa,
                      0,                    // ShowMembersFunc_t show,
                      "",                // definition file
                      "",                // implementation file
                      1,                    // definition line number
                      1 );                  // implementation line number
  }

  void ROOTClassEnhancerInfo::addFunction( const std::string& Name, 
                                           const Type & sig,
                                           StubFunction stubFP, 
                                           void*  stubCtx,
                                           int mods)
  {
    fType.AddFunctionMember( Name.c_str(), sig, stubFP, stubCtx, 0, PUBLIC | mods );
  }

  inline static ROOTClassEnhancerInfo& context(void* ctxt)  {
    if ( ctxt )  {
      return *(ROOTClassEnhancerInfo*)ctxt;
    }
    throw std::runtime_error("Invalid stub context passes to emultated function!");
  }


  void* ROOTClassEnhancerInfo::stub_IsA(void* obj, const vector<void*>&, void* ctx) {
    return context(ctx).IsA(obj);
  }
  void* ROOTClassEnhancerInfo::stub_IsA2(void* ctx, void* obj) {
    return context(ctx).IsA(obj);
  }

  struct DynamicStruct  {    virtual ~DynamicStruct() {}  };
{'fraction_non_alphanumeric': 0.12269175252600667, 'fraction_numerical': 0.006868747200238913, 'mean_word_length': 2.4274991470487888, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 59, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3200186', 'n_tokens_mistral': 6418, 'n_tokens_neox': 6182, 'n_words': 1346}