/*
 * Copyright (c) 1995, 1996 Gunther Schadow.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef PG_UNITATOM_H_
#define PG_UNITATOM_H_

#include <pg_config.h>

#pragma interface

#include <Code.h>
#include <BaseUnit.h>
#include <odbm.h>

class UnitAtom : public Code, protected odbm
{
 public:
  /*
   * Unit structure
   */
  struct unit_s {
    char   *name;                    // KEY
      
    double (*cnv_from)(double);      // not persistent
    double (*cnv_to)(double);

    char   *cnv_name;                // STRING start
    // index into table of conversion functions

    struct base_unit_vector base;    // SCALAR start
    double coeff_mantissa;
    int    coeff_exponent;

    ostream& show(ostream &) const;
  };
  
  static const unit_s null_unit;

public:
  UnitAtom();
  UnitAtom(const UnitAtom&);
  UnitAtom(const char*);
  ~UnitAtom();

  operator const char *() const;

  UnitAtom& operator = (const UnitAtom&); 
  UnitAtom& operator = (const char*); 

  const unit_s *operator -> () const;

  bool lookup(const char *);
  bool exists(const char *) const;
  bool exists() const;

 private:
  void   set(const char *);

  static odbm_file odbmf;
  static u_int odbm_obj_cnt;

 protected:
  unit_s unit;

  /*
   * Conversion Function
   */
  struct cnv_func {
    char *name;
    double (*from)(double);
    double (*to)(double);

    static cnv_func tab[];
    static const cnv_func *lookup(const char*);
    static void resolve(unit_s &u);
  };

  UnitAtom(int mode); // UnitAtom for write access to the odbm
};

#ifndef OUTLINE
# include "UnitAtom.icc"
#endif

#endif /* !PG_UNITATOM_H_ */
