// @(#)root/meta:$Id$
// Author: Fons Rademakers   08/02/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TBaseClass.h"
#include "TClass.h"
#include "TInterpreter.h"
#include <limits.h>

#include "TVirtualMutex.h" // For R__LOCKGUARD

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//  Each class (see TClass) has a linked list of its base class(es).    //
//  This class describes one single base class.                         //
//  The base class info is obtained via the CINT api.                   //
//     see class TCling.                                                 //
//                                                                      //
//  The base class information is used a.o. in to find all inherited    //
//  methods.                                                            //
//                                                                      //
//////////////////////////////////////////////////////////////////////////


ClassImp(TBaseClass)

//______________________________________________________________________________
TBaseClass::TBaseClass(BaseClassInfo_t *info, TClass *cl) :
   TDictionary(), fInfo(info), fClass(cl), fDelta(INT_MAX),
   fProperty(-1), fSTLType(-1)
{
   // Default TBaseClass ctor. TBaseClasses are constructed in TClass
   // via a call to TCling::CreateListOfBaseClasses().

   if (fInfo) SetName(gCling->BaseClassInfo_FullName(fInfo));
}

//______________________________________________________________________________
TBaseClass::~TBaseClass()
{
   // TBaseClass dtor deletes adopted CINT BaseClassInfo object.

   gCling->BaseClassInfo_Delete(fInfo);
}

//______________________________________________________________________________
void TBaseClass::Browse(TBrowser *b)
{
   // Called by the browser, to browse a baseclass.

   TClass *c = GetClassPointer();
   if (c) c->Browse(b);
}

//______________________________________________________________________________
TClass *TBaseClass::GetClassPointer(Bool_t load)
{
   // Get pointer to the base class TClass.

   if (!fClassPtr) {
      if (fInfo) fClassPtr = TClass::GetClass(gCling->BaseClassInfo_ClassInfo(fInfo),load);
      else fClassPtr = TClass::GetClass(fName, load);
   }
   return fClassPtr;
}

//______________________________________________________________________________
Int_t TBaseClass::GetDelta()
{
   // Get offset from "this" to part of base class.

   // Initialized to INT_MAX to signal that it's unset; -1 is a valid value
   // meaning "cannot calculate base offset".
   if (fDelta == INT_MAX) {
      R__LOCKGUARD(gInterpreterMutex);
      if (Property() & kIsVirtualBase)
         fDelta = -1;
      else if (fInfo)
         fDelta = (Int_t)gCling->BaseClassInfo_Offset(fInfo);
   }
   return fDelta;
}

//______________________________________________________________________________
const char *TBaseClass::GetTitle() const
{
   // Get base class description (comment).

   TClass *c = ((TBaseClass *)this)->GetClassPointer();
   return c ? c->GetTitle() : "";
}

//______________________________________________________________________________
ROOT::ESTLType TBaseClass::IsSTLContainer()
{
   // Return which type (if any) of STL container the data member is.

   // fSTLType is -1 if not yet evaulated.
   // fSTLType is -2 if no fInfo was available.

   if (fSTLType < 0) {
      if (!fInfo) {
         fSTLType = -2;
      } else {
         const char *type = gCling->BaseClassInfo_TmpltName(fInfo);
         if (!type)                                    fSTLType = ROOT::kNotSTL;
         else if (!strcmp(type, "vector"))             fSTLType = ROOT::kSTLvector;
         else if (!strcmp(type, "list"))               fSTLType = ROOT::kSTLlist;
         else if (!strcmp(type, "forward_list"))       fSTLType = ROOT::kSTLforwardlist;
         else if (!strcmp(type, "deque"))              fSTLType = ROOT::kSTLdeque;
         else if (!strcmp(type, "map"))                fSTLType = ROOT::kSTLmap;
         else if (!strcmp(type, "multimap"))           fSTLType = ROOT::kSTLmultimap;
         else if (!strcmp(type, "set"))                fSTLType = ROOT::kSTLset;
         else if (!strcmp(type, "multiset"))           fSTLType = ROOT::kSTLmultiset;
         else if (!strcmp(type, "unordered_set"))      fSTLType = ROOT::kSTLunorderedset;
         else if (!strcmp(type, "unordered_multiset")) fSTLType = ROOT::kSTLunorderedmultiset;
         else if (!strcmp(type, "unordered_map"))      fSTLType = ROOT::kSTLunorderedmap;
         else if (!strcmp(type, "unordered_multimap")) fSTLType = ROOT::kSTLunorderedmultimap;
         else                                          fSTLType = ROOT::kNotSTL;
      }
   }
   if (fSTLType == -2) return ROOT::kNotSTL;
   return (ROOT::ESTLType) fSTLType;
}

//______________________________________________________________________________
Long_t TBaseClass::Property() const
{
   // Get property description word. For meaning of bits see EProperty.
   if (fProperty == -1 && fInfo) {
      R__LOCKGUARD(gInterpreterMutex);
      fProperty = gCling->BaseClassInfo_Property(fInfo);
   }
   return fProperty;
}

//______________________________________________________________________________
void TBaseClass::Streamer(TBuffer& b) {
   // Stream an object of TBaseClass. Triggers the calculation of the
   // cache variables to store them.
   if (b.IsReading()) {
      b.ReadClassBuffer(Class(), this);
   } else {
      // Writing.
      // Calculate cache properties first.
      GetDelta();
      Property();
      IsSTLContainer();
      b.WriteClassBuffer(Class(), this);
   }
}
 TBaseClass.cxx:1
 TBaseClass.cxx:2
 TBaseClass.cxx:3
 TBaseClass.cxx:4
 TBaseClass.cxx:5
 TBaseClass.cxx:6
 TBaseClass.cxx:7
 TBaseClass.cxx:8
 TBaseClass.cxx:9
 TBaseClass.cxx:10
 TBaseClass.cxx:11
 TBaseClass.cxx:12
 TBaseClass.cxx:13
 TBaseClass.cxx:14
 TBaseClass.cxx:15
 TBaseClass.cxx:16
 TBaseClass.cxx:17
 TBaseClass.cxx:18
 TBaseClass.cxx:19
 TBaseClass.cxx:20
 TBaseClass.cxx:21
 TBaseClass.cxx:22
 TBaseClass.cxx:23
 TBaseClass.cxx:24
 TBaseClass.cxx:25
 TBaseClass.cxx:26
 TBaseClass.cxx:27
 TBaseClass.cxx:28
 TBaseClass.cxx:29
 TBaseClass.cxx:30
 TBaseClass.cxx:31
 TBaseClass.cxx:32
 TBaseClass.cxx:33
 TBaseClass.cxx:34
 TBaseClass.cxx:35
 TBaseClass.cxx:36
 TBaseClass.cxx:37
 TBaseClass.cxx:38
 TBaseClass.cxx:39
 TBaseClass.cxx:40
 TBaseClass.cxx:41
 TBaseClass.cxx:42
 TBaseClass.cxx:43
 TBaseClass.cxx:44
 TBaseClass.cxx:45
 TBaseClass.cxx:46
 TBaseClass.cxx:47
 TBaseClass.cxx:48
 TBaseClass.cxx:49
 TBaseClass.cxx:50
 TBaseClass.cxx:51
 TBaseClass.cxx:52
 TBaseClass.cxx:53
 TBaseClass.cxx:54
 TBaseClass.cxx:55
 TBaseClass.cxx:56
 TBaseClass.cxx:57
 TBaseClass.cxx:58
 TBaseClass.cxx:59
 TBaseClass.cxx:60
 TBaseClass.cxx:61
 TBaseClass.cxx:62
 TBaseClass.cxx:63
 TBaseClass.cxx:64
 TBaseClass.cxx:65
 TBaseClass.cxx:66
 TBaseClass.cxx:67
 TBaseClass.cxx:68
 TBaseClass.cxx:69
 TBaseClass.cxx:70
 TBaseClass.cxx:71
 TBaseClass.cxx:72
 TBaseClass.cxx:73
 TBaseClass.cxx:74
 TBaseClass.cxx:75
 TBaseClass.cxx:76
 TBaseClass.cxx:77
 TBaseClass.cxx:78
 TBaseClass.cxx:79
 TBaseClass.cxx:80
 TBaseClass.cxx:81
 TBaseClass.cxx:82
 TBaseClass.cxx:83
 TBaseClass.cxx:84
 TBaseClass.cxx:85
 TBaseClass.cxx:86
 TBaseClass.cxx:87
 TBaseClass.cxx:88
 TBaseClass.cxx:89
 TBaseClass.cxx:90
 TBaseClass.cxx:91
 TBaseClass.cxx:92
 TBaseClass.cxx:93
 TBaseClass.cxx:94
 TBaseClass.cxx:95
 TBaseClass.cxx:96
 TBaseClass.cxx:97
 TBaseClass.cxx:98
 TBaseClass.cxx:99
 TBaseClass.cxx:100
 TBaseClass.cxx:101
 TBaseClass.cxx:102
 TBaseClass.cxx:103
 TBaseClass.cxx:104
 TBaseClass.cxx:105
 TBaseClass.cxx:106
 TBaseClass.cxx:107
 TBaseClass.cxx:108
 TBaseClass.cxx:109
 TBaseClass.cxx:110
 TBaseClass.cxx:111
 TBaseClass.cxx:112
 TBaseClass.cxx:113
 TBaseClass.cxx:114
 TBaseClass.cxx:115
 TBaseClass.cxx:116
 TBaseClass.cxx:117
 TBaseClass.cxx:118
 TBaseClass.cxx:119
 TBaseClass.cxx:120
 TBaseClass.cxx:121
 TBaseClass.cxx:122
 TBaseClass.cxx:123
 TBaseClass.cxx:124
 TBaseClass.cxx:125
 TBaseClass.cxx:126
 TBaseClass.cxx:127
 TBaseClass.cxx:128
 TBaseClass.cxx:129
 TBaseClass.cxx:130
 TBaseClass.cxx:131
 TBaseClass.cxx:132
 TBaseClass.cxx:133
 TBaseClass.cxx:134
 TBaseClass.cxx:135
 TBaseClass.cxx:136
 TBaseClass.cxx:137
 TBaseClass.cxx:138
 TBaseClass.cxx:139
 TBaseClass.cxx:140
 TBaseClass.cxx:141
 TBaseClass.cxx:142
 TBaseClass.cxx:143
 TBaseClass.cxx:144
 TBaseClass.cxx:145
 TBaseClass.cxx:146
 TBaseClass.cxx:147
 TBaseClass.cxx:148
 TBaseClass.cxx:149
 TBaseClass.cxx:150
 TBaseClass.cxx:151
 TBaseClass.cxx:152
 TBaseClass.cxx:153
 TBaseClass.cxx:154
 TBaseClass.cxx:155
 TBaseClass.cxx:156
 TBaseClass.cxx:157
 TBaseClass.cxx:158