constants.h

来自「模拟器提供了一个简单易用的平台」· C头文件 代码 · 共 220 行

H
220
字号
/* * File: constants.h * Author: Suman Banerjee <suman@cs.umd.edu> * Date: July 31, 2001 * Terms: GPL * * myns simulator */#ifndef _CONSTANTS_H_#define _CONSTANTS_H_#include <string.h>#include <assert.h>#include <linked-list.h>#define MAX_CONSTANT_NAME_SIZE	64enum TypeInfo {  TYPE_INT,  TYPE_DOUBLE,  TYPE_UNDEFINED};struct ConstVal { TypeInfo t; union {   int i;   double d; } u; ConstVal () { t = TYPE_UNDEFINED; }; ConstVal (TypeInfo T, int I) {   assert (T == TYPE_INT);   t = TYPE_INT;   u.i = I; };    ConstVal (TypeInfo T, double D) {   assert (T == TYPE_DOUBLE);   t = TYPE_DOUBLE;   u.d = D; };   };/*void add_to_constant_list (void * const_ptr, int id);void delete_from_constant_list (int id);int get_next_constant_id (void);*/class NamedConstant {  public :    int id;    char cname[MAX_CONSTANT_NAME_SIZE];    ConstVal val;    static LinkedList<NamedConstant *, int> constant_list;    static int const_id_gen;public :  void init_constant (char * Cname) {    /* id = get_next_constant_id(); */    id = const_id_gen ++;    strcpy(cname, Cname);    constant_list.Add(this,id);  };  NamedConstant (char * Cname, int I) {        init_constant(Cname);    setval(I);  };  NamedConstant (char * Cname, double D) {        init_constant(Cname);    setval(D);  };  ~NamedConstant (void) {     void * pos = constant_list.Locate(id);     if (pos == NULL) {       /* printf ("Pos is NULL: id %d\n", id); */     }     else       constant_list.RemoveAt(pos);   };  inline void setval (int I) {    val.t = TYPE_INT;    val.u.i = I;    return;  };  inline void setval (double D) {    val.t = TYPE_DOUBLE;    val.u.d = D;    return;  };  static NamedConstant * find_constant (char * Cname) {    for (void * pos = constant_list.GetHeadPosition();       pos != NULL;       constant_list.GetNext(pos) ) {      NamedConstant * this_const = constant_list.GetAt(pos);      assert (this_const != NULL);      if (strcmp(this_const->cname,Cname) == 0) {        return this_const;      }    }    return NULL;  };  static void setval (char * Cname, int I) {    NamedConstant * this_const = find_constant(Cname);    assert (this_const != NULL);    this_const->setval(I);    return;  };  static void setval (char * Cname, double D) {    NamedConstant * this_const = find_constant(Cname);    assert (this_const != NULL);    this_const->setval(D);    return;  };  inline ConstVal getval (void) {         ConstVal ret_val;    ret_val.t = val.t;    switch (val.t) {    case TYPE_INT:      ret_val.u.i = val.u.i;      break;    case TYPE_DOUBLE:      ret_val.u.d = val.u.d;      break;    default:      assert (0);    }    return ret_val;  };  static void display_all (void) {    for (void * pos = constant_list.GetHeadPosition();       pos != NULL;       constant_list.GetNext(pos) ) {      NamedConstant * this_const = constant_list.GetAt(pos);      assert (this_const != NULL);      switch (this_const->val.t) {      case TYPE_INT:        printf ("[constant] set %s %d\n", this_const->cname, this_const->val.u.i);        break;      case TYPE_DOUBLE:        printf ("[constant] set %s %f\n", this_const->cname, this_const->val.u.d);        break;      default:        assert(0);      }    }    return;  }    /*    static void setval (char * Cname, ConstType Val) {      bool found = false;      for (void * pos = constant_list.GetHeadPosition();         pos != NULL;         constant_list.GetNext(pos) ) {      Constant<ConstType> * this_const = constant_list.GetAt(pos);      assert (this_const != NULL);      if (strcmp(this_const->cname,Cname) == 0) {        this_const->val = Val;        found = true;        break;      }    }    assert (found == true);    return;    };*/    /* static void setval (char * Cname, ConstType Val); */};/*void constant_setval(char * Cname, int ival, double dval, bool is_int);*/#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?