Re: Sorry - here you go (was Re: [ROOT] Advice for Good Programming : Part 2:)

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Tue Sep 04 2001 - 16:06:22 MEST


Hi 

On Tue, 4 Sep 2001 19:16:12 +0530 (IST)
"B.V.L.S.Prasad" <shiva@mbu.iisc.ernet.in> wrote
concerning "Re: Sorry - here you go (was Re: [ROOT] Advice for Good Programming : Part 2:)":
> Hi christian,
> 	I have tried and compiled the tar ball you have sent. I am using
> makecint not root...I dont think it is a problem.

I'm not using ROOT stuff in that makefile. I'm creating the dictionary
using CINT - nothing else.  I only used root-config to get the include
path to G__ci.h, that's it. 
 
> I just used:
> 
> makecint -mk makebio -o bio -H BioAtomLibrary.h BioAtomLibraryLinkDef.h -C++
> BioAtomLibrary.cxx
> 
> then
> make -f makebio
> 
> These are my observations:
> 
> First I compiled it on mandrake 8.0, gcc-2.96 (supposed to be
> buggy), its not getting compiled...some problem.

Do submit a bug report if it keeps saying you should do that. 

> Then I searched here and there for RedHat 6.2, and finally located
> and did the same things. It got compiled and gave an interpretor
> with name bio as desired. 

That's as far as I got too, expect that I made a shared lib, and
loaded it into CINT with the .L command. 

> for checking it, I typed bio: and gave some commands like this to access the
> data which is the primary purpose:
> 
> bio> {
> end with '}'> setAtomDefaults();
> end with '}'> cout<<Atom["Ca"].getAtomicNumber()<<endl;
> end with '}'> }
> Error: Can't call
> map<string,BioAtomLibrary,less<string>,__malloc_alloc_template<0>
> >::operator[]((char*)0x850e6b8) in current scope FILE:/tmp/filea2sIty_cint
> LINE:3
> Possible candidates are...
> filename       line:size busy function type and name  (in
> map<string,BioAtomLibrary,less<string>,__malloc_alloc_template<0> >)
> Error: non class,struct,union object Atom["Ca"] used with . or ->
> FILE:/tmp/filea2sIty_cint LINE:3
> (class G__CINT_ENDL)136702840
> !!!Dictionary position rewinded... !!!Error recovered!!!
> bio>

I saw the same problem.  My point was more to the actual compilation
than the usage. 
 
> I even did, class BioAtomLibrary to check whether it is compiled or
> not... it successfully gave the public member functions.
> 
> why I am not able to use those 2 lines of code to access the data.. 

I have no idea - sorry.  Try writting Masa and/or roottalk. 

Might I suggest, that instead of the global variable Atom and the
function setAtomDefaults(), have a static member of BioAtomLibrary and
a static function, like: 

  typedef map<string,BioAtom> BioAtomLibrary;

  class BioAtom 
  { 
  public:
    static BioAtomLibrary Library;
  private: 
    static bool isInitialised;
    ...
  public: 
    ...
    static Initialise() { 
      if (isInitialised) return;
      isInitialised = true;
      Atom["H"]  = BioAtom(1,"H","Hydrogen",1.008,0.78,0);
      ...
    } 
    ...
  };

with 

  #pragma link C++ class   BioAtom;
  #pragma link C++ typedef BioAtomLibrary;

This is much cleaner from a programing point of view, since you hide
the array in the scope of a class.  You can then access elements in
the list with 

  BioAtom::Initialise();
  BioAtom& hydrogen = BioAtom::Library["H"];
  int hydrogenNumber =  BioAtom::Library["H"].getAtomicNumber(); 
  
and so on.  If you in a particular piece of code would prefer to have
a direct reference to the library, you can do 

  BioAtomLibrary& Library = BioAtom::Library; 

and then use Library directly: 

  int hydrogenNumber =  Library["H"].getAtomicNumber(); 

It may be that this approach will solve your problems too :-) 

Yours, 

Christian Holm Christensen -------------------------------------------
Address: Sankt Hansgade 23, 1. th.           Phone:  (+45) 35 35 96 91 
         DK-2200 Copenhagen N                Cell:   (+45) 28 82 16 23
         Denmark                             Office: (+45) 353  25 305 
Email:   cholm@nbi.dk                        Web:    www.nbi.dk/~cholm



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:59 MET