⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 field.hpp

📁 联通接收发送新程序
💻 HPP
字号:

/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */
/*
  Name: field.hpp                        Version: 2.0.0
  Created by HanBing                     Date: 2000-08-08
  Comment:
  Modified:
2)  2000-08-28	HanBing	-	Modifed the class CField be array_size;
1)  2000-08-08	HanBing	-	Create;
*/

#ifndef __FIELD__
#define __FIELD__   

/* Define Data types for Field */
typedef enum DataBaseTypes
{
  VoidT,
  IntT,
  FloatT,
  CharT
} DataType;

/*
  Class: CField                        Version: 2.0
  Created by HanBing                   Date: 2000-08-08
  Implementation File: field.cpp
  Comment:
  Modified
2)  2000-08-28	HanBing	-	Modifed to be array_size;
1)  2000-08-08	HanBing	-	Create;
*/
class CField
{
  public:
    /*
      Name: CField() - constructor
      Create by HanBing					            Date: 2000-08-08
      Define in Class: CField	              File: field.hpp
      Comment:
      Parameters:   pcName  - input - the Field's name
                    type    - input - the Field's type
                    len     - input - the Field's length, for type CHAR
                    key     - input - if the Field is not a key, key = 0
                                      else key = the key's order
                    Bat     - input - the Field's Array Size
      Return Value:
      Modified:
      1) 2000-08-08	HanBing	-	Create;
    */
    inline CField( const char *pcName, DataType type=VoidT, ushort len=255, ushort key=0, uint Bat = 1 )
    {
      FieldName   = NULL;
      FieldValue  = NULL;
      Null        = NULL;
      Current     = 0;
      BatSize     = 0;
      KeyOrder    = key;
      SetName( pcName );
      SetType( type, len, Bat );
    }

    inline CField( uint Bat = 1 )
    {
      FieldName   = NULL;
      FieldValue  = NULL;
      Null        = NULL;
      Current     = 0;
      BatSize     = 0;
      FieldType   = VoidT;
      FieldLen    = 0;
      KeyOrder    = 0;
      SetType( FieldType, FieldLen, Bat );
    }

    /*
      Name: ~CField() - destructor
      Comment:          Destroy the point alloced;
      Parameters:
      Return Value:
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline ~CField()
    {
      if ( FieldName )
      {
        delete FieldName;
        FieldName = NULL;
      }
      if ( FieldValue )
      {
        delete FieldValue;
        FieldValue = NULL;
      }
      if ( Null )
        delete Null;
    }

    /*  Clear the Field's Value  */
    /*
      Name: Clear()
      Comment:      Clear the Field's Value and set it be NULL;
      Parameters:
      Return Value:
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline void Clear( bool flag = true )
    {
      Current = 0;
      if ( flag )
      {
        memset( FieldValue, 0, BufLen() );
        SetAllNull();
      }
    }

/*  oprators for set values  */
    /*  oprators for copy  */
    CField& operator = ( CField& Field );

    /*
      Name: operator = ( class T )
      Comment:      Set the T type Field a T type Value;
      Parameters:   the T type Value the Field to be set
      Return Value: the Value the Field be set
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline double operator = ( double Value )
    {
      Assert( FieldType == FloatT, FieldName );
      *( (double*)FieldValue + Current ) = Value;
      SetNull( Current, false );
      return Value;
    }

    inline float operator = ( float Value )
    {
      return (float)operator = ( double(Value) );
    }

    inline long operator = ( long Value )
    {
      Assert( FieldType == IntT || FieldType == FloatT, FieldName );
      SetNull( Current, false );
      if ( FieldType == IntT )
        *( (long*)FieldValue + Current ) = Value;
      else if ( FieldType == FloatT )
        operator = ( double(Value) );
      return Value;
    }

    inline int operator = ( int Value )
    {
      return (long)operator = ( long(Value) );
    }

    inline char* operator = ( const char *Value )
    {
      Assert( FieldType == CharT, FieldName );
      SetNull( Current, false );
      if ( Value )
        return StrnCpy( (char*)FieldValue+(FieldLen+1)*Current, Value, FieldLen );
      else
        return strcpy( (char*)FieldValue+(FieldLen+1)*Current, "" );
    }

/*  functions for get value  */
    /*
      Name: TclassAt()
      Comment:      Get the Field's Tclass type Value at the position;
      Parameters:   pos - input - the position the value be get 
      Return Value: the Field' Tclass type Value
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    /*  Get long type value  */
    inline long& IntAt( uint pos )
    {
      Assert( FieldType == IntT && pos < BatSize, FieldName );
      return *( (long*)FieldValue + pos );
    }
    inline long& Int()
    {
      return *( (long*)FieldValue + Current );
    }

    /*  Get double type value  */
    inline double& FloatAt( uint pos )
    {
      Assert( FieldType == FloatT && pos < BatSize, FieldName );
      return *( (double*)FieldValue + pos );
    }
    inline double& Float()
    {
      return *( (double*)FieldValue + Current );
    }

    /*  Get string type value  */
    inline const char* CharAt( uint pos )
    {
      Assert( FieldType == CharT && pos < BatSize, FieldName );
      return (char*)FieldValue + (FieldLen+1) * pos;
    }
    inline const char* Char()
    {
      return (char*)FieldValue + (FieldLen+1) * Current;
    }

    /*
      Name: Buff()
      Comment:      Get the Field's buffer pointer;
      Parameters:
      Return Value: the Field' buffer pointer
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline uchar* Buff()
    {
      return (uchar*)FieldValue;
    };

    /*  Get the Field's one row buffer size  */
    /*
      Name: LineLen()
      Comment:      Get the Field's one row buffer size;
      Parameters:
      Return Value: the Field' one row buffer size
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline uint LineLen()
    {
      uint len = 0;
      switch ( FieldType )
      {
        case CharT:
          len = FieldLen + 1;
          break;
        case IntT:
          len = sizeof( long );
          break;
        case FloatT:
          len = sizeof( double );
          break;
      }
      return len;
    }


    /*
      Name: BufLen()
      Comment:      Get the Field's buffer size;
      Parameters:
      Return Value: the Field' buffer size
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline uint BufLen()
    {
      return ( LineLen() * BatSize );
    }

    /*
      Name: State()
      Comment:      Get the Field's buffer store "NULL" attribute;
      Parameters:
      Return Value: the Field's "null" attribute buffer pointer
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline short* State()
    {
      return Null;
    };

/*  functions for set attribute  */
    /*  Set the Field's name  */
    /*
      Name: SetName()
      Comment:      Set the Field's name;
      Parameters:   pcName  - input - the Field's name
      Return Value:
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline void SetName( const char *pcName )
    {
      if ( pcName )
      {
        if ( FieldName )
          delete FieldName;
        FieldName = new char [ strlen( pcName ) + 1 ];
        strcpy( FieldName, pcName );
      }
    }

    /*
      Name: SetType()
      Comment:      Set the Field's type and length;
      Parameters:   type  - input - the Field's type
                    len   - input - the Field's length
      Return Value:
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline void SetType( DataType type, ushort len, uint Bat )
    {
      if ( type == FieldType && len == FieldLen && Bat == BatSize )
        return;
      if ( FieldValue )
        delete FieldValue;
      if ( Null )
        delete Null;
      FieldType   = type;
      FieldLen    = len;
      BatSize     = Bat;
      Null        = new short [ BatSize ];
      FieldValue  = new char [ BufLen() ];
      Clear();
    }

    /*
      Name: SetSize()
      Comment:      Set the Field's Array size
      Parameters:   batsize - input - the Field's array size
      Return Value:
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline void SetSize( uint batsize )
    {
      if ( BatSize != batsize )
        SetType( FieldType, FieldLen, batsize );
    };

    /*
      Name: SetKey()
      Comment:      Set the Field's key
      Parameters:   key - input - if the Field is not a key, key = 0
                                  else key = the key's order
      Return Value:
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline void SetKey( ushort key )
    {
      KeyOrder = key;
    };

    /*
      Name: Set()
      Comment:      Set the Field's key
      Parameters:   key - input - if the Field is not a key, key = 0
                                  else key = the key's order
      Return Value:
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline void Set( const char *pcName=NULL, DataType type=VoidT, ushort len=255, ushort key=0, uint size = 1 )
    {
      SetName( pcName );
      SetType( type, len, size );
      SetKey( key );
    }

/*  functions for set the null attribute  */
    /*
      Name: SetNull()
      Comment:      Set the Field's "NULL" attribute at the position;
      Parameters:   b_null  - input - the Field's "NULL" attribute;
                    pos     - input - the field's position to be set
                                      default is current position;
      Return Value:
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline void SetNull( uint pos, bool b_null = true )
    {
      Assert( pos < BatSize, FieldName );
      if ( b_null )
        Null[ pos ] = -1;
      else
        Null[ pos ] = 0;
    };
    inline void SetNull()
    {
      Null[ Current ] = -1;
    };

    /*
      Name: SetAllNull()
      Comment:      Set all Field in array be NULL;
      Parameters:
      Return Value:
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline void SetAllNull()
    {
      for ( uint i = 0; i < BatSize; i++ )
        SetNull( i, true );
    }

/*  functions for get attribute  */
    /*
      Name: Name()
      Comment:      Get the Field's name;
      Parameters:
      Return Value: the Field's name;
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline const char* Name()
    {
      return FieldName;
    };

    /*
      Name: Type()
      Comment:      Get the Field's type;
      Parameters:
      Return Value: the Field's type;
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline DataType Type()
    {
      return FieldType;
    };

    /*
      Name: Len()
      Comment:      Get the Field's Length;
      Parameters:
      Return Value: the Field's Length;
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline ushort Len()
    {
      return FieldLen;
    };

    /*
      Name: Key()
      Comment:      Get the Field's Key order;
      Parameters:
      Return Value: if the Field is not a key, return 0
                    else return the key's order
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline ushort Key()
    {
      return KeyOrder;
    };

    /*
      Name: CurInd()
      Comment:      Get the Field's Current position;
      Parameters:
      Return Value: the field's current position, base is 0
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline uint CurInd()
    {
      return Current;
    };

    /*
      Name: Size()
      Comment:      Get the Field's Array Size;
      Parameters:
      Return Value: the field's array size
      Modified:
    1) 2000-08-28	HanBing	-	Create;
    */
    inline uint Size()
    {
      return BatSize;
    };

    /*
      Name: IsNull()
      Comment:      Get whether the Field's value at the position is NULL;
      Parameters:   pos - input - the position get the NULL attribute
      Return Value: if the Field's value is NULL, return true
                    else return false
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline bool IsNull( uint pos )
    {
      Assert( pos < BatSize, FieldName );
      return ( Null[ pos ] ? true : false );
    }
    inline bool IsNull()
    {
      return ( Null[ Current ] ? true : false );
    }

    /*
      Name: Next()
      Comment:      go to the field's next value in the array;
      Parameters:
      Return Value: the Field' current position
      Modified:
    1) 2000-08-08	HanBing	-	Create;
    */
    inline int Next()
    {
      if ( ++Current >= BatSize )
        Clear();
      return Current;
    }

    /*  Display the Field's name and value at the position  */
    void PrintName()
    {
      if ( FieldName )
        cout << "'" << FieldName << "'";
      else
        cout << "NULL";
    }

    void Print( uint pos );

    inline void Print()
    {
      PrintName();
      cout << " = ";
      Print( Current );
    }

    /*  Display the Field's name and value all in array  */
    void PrintAll();

  private:
    char *FieldName;
    void *FieldValue;
    DataType FieldType;
    ushort  FieldLen;
    ushort  KeyOrder;
    uint    BatSize;
    uint    Current;
    short *Null;
    inline uint GoTo( uint pos )
    {
      Assert(  pos < BatSize, FieldName );
      return ( Current = pos );
    }
    inline int GoFirst()
    {
      return ( Current = 0 );
    };
};

#endif //__FIELD__

⌨️ 快捷键说明

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