//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2007-13  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  P2Pmsg definitions and prototypes
//
#pragma once

#ifndef NO_DEBUG_NEW
#define new DEBUG_NEW
#endif

#include "wtypes.h"
#include "comutil.h"
#include "P2Peer.h"
#include "P2PTL.h"

///////////////////////////////////////////////////////////////////////////////
//  P2Pmsg pre-definitions
typedef  UINT    P2Pos;
#define  INT08   char
#define  UINT08  UCHAR
#define  TIME32  INT32
#define  TIME64  INT64
#define  VBLaddr UINT
#define  VBLsize UINT

#define  SIZE08_MAX ((UINT08)~0)
#define  SIZE16_MAX ((UINT16)~0)
#define  SIZE32_MAX ((UINT32)~0)
#define  SIZE64_MAX ((UINT64)~0)

const  INT32  INT32_NULL = ((UINT32)~0);
const UINT32 UINT32_NULL = ((UINT32)~0);
const  INT64  INT64_NULL = ((UINT64)~0);
const UINT64 UINT64_NULL = ((UINT64)~0);

///////////////////////////////////////////////////////////////////////
//  Convenience characters

#define T_Space     _T(' ')
#define T_Hash      _T('#')
#define T_Period    _T('.')
#define T_Colon     _T(':')
#define T_BackSlash _T('\\')
#define T_ForeSlash _T('/')
#define T_LF        _T('\n')
#define T_CR        _T('\r')
#define T_CRLF      _T("\r\n")

#define SelectObject_T(str)      SelectObject(_T(str))
#define SelectItem_T(str)        SelectItem(_T(str))
#define DeclareField_T(str,data) DeclareField(_T(str),data)
#define DeclareNode_T(str,data)  DeclareNode(_T(str),data)

///////////////////////////////////////////////////////////////////////
//  VBLockHdr definitions
//  NOTES: Internalised memory blocks MUST be prefixed with this
//         object
#define VBLock_AddrMask 0x03           // Bits addressing  (Bits 0-1)
#define VBLock_Addr08      0           //    8Bit  addressing
#define VBLock_Addr16      1           //   16Bit  addressing
#define VBLock_Addr32      2           //   32Bit  addressing
#define VBLock_Addr64      3           //   64Bit  addressing

#define VBLock_TypeMask 0x3C           // Types            (Bits 2-5)
#define VBLock_Item        4           //   Item
#define VBLock_Heap        8           //   Heap
#define VBLock_Root       12           //   Root
#define VBLock_BSTR       16           //   BSTR
#define VBLock_Field      20           //   Field
#define VBLock_Node       24           //   Node
#define VBLock_Blob       28           //   Blob
#define VBLock_Data       32           //   Data
#define VBLock_Name       36           //   Name
#define VBLock_Stack      40           //   Stack
#define VBLock_List       44           //   List
#define VBLock_Vect       48           //   Vector
#define VBLock_Attr       52           //   Attributes
#define VBLock_Spare2     56           //   Spare1
#define VBlock_Spare3     60           //   Spare2

#define VBLock_Alloc      64           // Allocated        (Bit  6)
#define VBLock_Linked    128           // Linked           (Bit  7)

#pragma pack(push,1)
typedef struct
{
    UCHAR  uVBLock;
    union
    {
      UINT08     nSize08;
      UINT16     nSize16;
      UINT32     nSize32;
      UINT64     nSize64;
    } u;                               // Absolute size of block
} VBLockHdr;
#pragma pack(pop)

//
//  P3PmagData constraints
typedef unsigned char P2Pcons_t;
#define VBLockAttr_READ     (1<<0)     // Read
#define VBLockAttr_WRITE    (1<<1)     // Write
#define VBLockAttr_AMEND    (1<<2)     // Ammend
#define VBLockAttr_DROP     (1<<3)     // Delete
#define VBLockAttr_NULLABLE (1<<4)
#define VBLockAttr_NULL     (1<<7)     // Null entry
#define VBLockAttr_DEFAULT ((1<<0)|(1<<1)|(1<<2)|(1<<4))

///////////////////////////////////////////////////////////////////////////////
//  P2PmsgData container and definitions
//  NOTES: Base class for VBLockData structures
//       : Intended for standalone or derived instanciation
typedef unsigned char P2Pvar_t;        // P2PmsgData variable type
#define VBLockData_NULL       0
                                       // Entry type Builtin
#define VBLockData_INT08      1        //   char
#define VBLockData_UINT08     2        //   unsigned char
#define VBLockData_INT16      3        //   short
#define VBLockData_UINT16     4        //   unsigned short
#define VBLockData_INT32      5        //   int
#define VBLockData_UINT32     6        //   unsigned int
#define VBLockData_INT64      7        //   long
#define VBLockData_UINT64     8        //   unsigned long
#define VBLockData_FLOAT      9        //   float
#define VBLockData_DOUBLE    10        //   double
#define VBLockData_TIME32    11        //   time 32 bits
#define VBLockData_TIME64    12        //   time 64 bits
#define VBLockData_BOOL      13        //   bool
                                       // Entry type BSTR - ASCII
#define VBLockData_BSTR08    16        //   08 bits
#define VBLockData_BSTR08var 17        //   16 bits variable
#define VBLockData_BSTR16    18        //   32 bits
#define VBLockData_BSTR16var 19        //   08 bits variable
#define VBLockData_BSTR32    20        //   16 bits
#define VBLockData_BSTR32var 21        //   32 bits variable
                                       // Entry type WSTR - UNICODE
#define VBLockData_WSTR08    24        //   08 bits
#define VBLockData_WSTR08var 25        //   16 bits variable
#define VBLockData_WSTR16    26        //   32 bits
#define VBLockData_WSTR16var 27        //   08 bits variable
#define VBLockData_WSTR32    28        //   16 bits
#define VBLockData_WSTR32var 29        //   32 bits variable
                                       // Entry type Blob
#define VBLockData_BLOB08    32        //   08 bits
#define VBLockData_BLOB08var 33        //   08 bits variable
#define VBLockData_BLOB16    34        //   16 bits
#define VBLockData_BLOB16var 35        //   16 bits variable
#define VBLockData_BLOB32    36        //   32 BLOB
#define VBLockData_BLOB32var 37        //   32 BLOB variable
                                       // Reserved for future use
#define VBLockData_GUID      47        //   16 byte GUID
#define VBLockData_EODefs    48

#pragma pack(push,1)
template < typename SIZE__ >
struct VBLob__var
{ 
    SIZE__  nBlobSize;
    SIZE__  nBlobUsed;
    char    cBlob;
};
typedef VBLob__var <UINT08> VBLob08;
typedef VBLob__var <UINT16> VBLob16;
typedef VBLob__var <UINT32> VBLob32;
typedef VBLob__var <UINT64> VBLob64;

template < typename SIZE__ >
struct VBLin__var
{ 
    SIZE__  nBlobSize;
    UINT    aVBLockAddr;
};
typedef VBLin__var <UINT08> VBLin08;
typedef VBLin__var <UINT16> VBLin16;
typedef VBLin__var <UINT32> VBLin32;
typedef VBLin__var <UINT64> VBLin64;

typedef struct
{
    UCHAR  uVBLockType; //Data
    UCHAR  uVBLockAttr;
    union
    {
      INT08          vInt08;
      UINT08         vuInt08;
      INT16          vInt16;
      UINT16         vuInt16;
      INT32          vInt32;
      UINT32         vuInt32;
      TIME32         vTime32;
      INT64          vInt64;
      UINT64         vuInt64;
      TIME64         vTime64;
      float          vFloat;
      double         vDouble;
      bool           vBool;
      GUID           vGUID;
      VBLob08        vBlob08;
      VBLob16        vBlob16;
      VBLob32        vBlob32;
      VBLob64        vBlob64;
      UINT           aVBLockData;
      char           aAlloc[126];
    } u;
} VBLockData;
#pragma pack(pop)

class P3PmsgField;
class P3PmsgObject;
class P2PmsgKERNEL_EXT P3PmsgData
{
      friend P3PmsgField;
      void
        RenderThisSafe ( );
        P3PmsgData ( const P3PmsgField *pField ); 

    // Constructors and destructor
    public:
        P3PmsgData ( );

        P3PmsgData ( INT08 vuInt08 );
        P3PmsgData ( UINT08 vuInt08 );
        P3PmsgData ( INT16 vInt16 );
        P3PmsgData ( UINT16 vuInt16 );
        P3PmsgData ( INT32 vInt32 );
        P3PmsgData ( UINT32 vuInt32 );
        P3PmsgData ( INT64 vInt64 );
        P3PmsgData ( UINT64 vuInt64 );
        P3PmsgData ( bool vbool );
        P3PmsgData ( long vLong );
        P3PmsgData ( unsigned long vuLong );
        P3PmsgData ( float vFloat );
        P3PmsgData ( double vDouble );
        P3PmsgData ( LPCTSTR vString
                   , int nLength = 0, UCHAR uVBLockData = VBLockData_BSTR16 );
        P3PmsgData ( int nLengthMax
                   , LPCTSTR vString
                   , int nLength = 0, UCHAR uVBLockData = VBLockData_BSTR16var );
        P3PmsgData ( const void *vObject
                   , int nSizeof, UCHAR uVBLockData = VBLockData_BLOB16 );
        P3PmsgData ( const GUID& oGUID );

      virtual
       ~P3PmsgData ( );
      void
        Connect ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nDataSize );
      void
        Recreate( P2Pvar_t nP2Pvar, const void *pvdata, int iDataSize ); 

    // Operators
    public:
      P3PmsgData&
        operator = ( const P3PmsgData& rhs );

    // Data exposure
    public:
      char&
        c_char ( );
      char
        c_char ( ) const;
      short&
        c_short ( );
      short
        c_short ( ) const;
      int&
        c_int ( );
      int
        c_int ( ) const;
      int
        c_int ( int iValueDefault ) const;
      unsigned int&
        c_uint ( );
      unsigned int
        c_uint ( ) const;
      unsigned int&
        c_time ( );
      unsigned int
        c_time ( ) const;
      __int64&
        c_time64 ( );
      __int64
        c_time64 ( ) const;
      float&
        c_float ( );
      float
        c_float ( ) const;
      double&
        c_double ( );
      double
        c_double ( ) const;
      bool&
        c_bool ( );
      bool
        c_bool ( ) const;
      LPCTSTR
        c_str ( ) const;
      void*
        c_vBlob ( ) const;
      void*
        c_vBlob ( const void *pvSrc, size_t nCount );
      void*
        c_vGUID ( ) const;
      void*
        c_vGUID ( const void *pvGUID, size_t nSize = 16 );
      void*
        c_memcpy ( const void *pvSrc, size_t nCount );
      void*
        c_memcpy ( void *pvDst, size_t nCount ) const;
      void*
        c_memset ( int c, size_t nCount ); 
      int
        c_strcmp ( LPCSTR lpszCompare ) const;
      int
        c_stricmp ( LPCSTR lpszCompareNocase ) const;
      size_t
        c_size ( ) const;
      size_t
        c_size_max ( ) const;
      P3PmsgData&
        r_data ( ) { return *this; }
      const P3PmsgObject*
        p_Object ( ) const;

    // Operations
    public:
      void
        Nullify ( );
      LPCTSTR
        ToString ( LPCTSTR lpszFormat = 0 );
      LPCTSTR
        ToStringDefs ( LPCTSTR lpszFormat = 0 );
      LPCTSTR
        ToStringType ( LPCTSTR lpszFormat = 0 ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
    /*virtual void
        Print ( FILE *fd, int iDepthOS = 0  );*/

    // Properties
    public:
      virtual UCHAR
        SetAttr ( UCHAR ucAttrAdd, UCHAR ucAttrRemove = 0 );
      virtual UCHAR
        GetAttr ( ) const;
      virtual UINT
        Sizeof ( ) const;
      bool
        IsDirty ( ) const;
      bool
        IsNull ( ) const;
      UCHAR
        DataType ( ) const;

    // Attributes
    private:
      mutable 
      P3PmsgObject *m_pObject;
      mutable
      TCHAR         m_szToString[64];
      TCHAR         m_szToStrDef[64];
      bool          m_bDataDirty;
};

P2PmsgKERNEL_EXT _variant_t
P2PmsgData_var ( const P3PmsgData& oData );
P2PmsgKERNEL_EXT BOOL
P2PmsgData_var ( P3PmsgData& oData, const _variant_t& var );
P2PmsgKERNEL_EXT BOOL
P3PmsgData_var ( P2PmsgHANDLE hVBListData, P2Pos nP2Pos, _variant_t& var );

#define DataBSTR08(vString) P3PmsgData(vString,0,VBLockData_BSTR08)
#define DataBSTR16(vString) P3PmsgData(vString,0,VBLockData_BSTR16)
#define DataBSTR32(vString) P3PmsgData(vString,0,VBLockData_BSTR32)
#define AllocBLOB08(nSizeBlob) P3PmsgData((void *)0,nSizeBlob,VBLockData_BLOB08)
#define AllocBLOB16(nSizeBlob) P3PmsgData((void *)0,nSizeBlob,VBLockData_BLOB16)
#define AllocBLOB32(nSizeBlob) P3PmsgData((void *)0,nSizeBlob,VBLockData_BLOB32)
#define DataBLOB08(vBlob) P3PmsgData((void *)&vBlob,sizeof(vBlob),VBLockData_BLOB08)

class P2PmsgKERNEL_EXT P3PmsgTime : public P3PmsgData
{
    // Constructors and destructor
    public:
        P3PmsgTime ( );
        P3PmsgTime ( __int64 iTime );
        P3PmsgTime ( const P3PmsgTime& rhs );
      virtual
       ~P3PmsgTime ( );
    // Operators
      P3PmsgTime&
        operator = ( const P3PmsgTime& rhs );
};

///////////////////////////////////////////////////////////////////////////////
//  P2PmsgName containers and definitions
//  NOTES: Base class for VBLockName structures
//       : Intended for standalone or derived instanciation
#pragma pack(push,1)
typedef struct
{
    UCHAR  uVBLockAttr;
    union
    {
      VBLob08  vBlob08;
      VBLin08  vBlin08;
      char     aAlloc[127];
    } u;
} VBLockName;
#pragma pack(pop)

class P2PmsgKERNEL_EXT P3PmsgName
{
      friend P3PmsgField;
      void
        RenderThisSafe ( );
        P3PmsgName ( const P3PmsgField *pField ); 

    // Constructors and destructure
    public:
        P3PmsgName ( );

        P3PmsgName ( LPCTSTR lpszName, int nSize = 0 );
      virtual
       ~P3PmsgName ( );

    // Operators
    public:
      P3PmsgName&
        operator = ( const P3PmsgName& rhs );
      bool
        operator < ( const P3PmsgName& rhs ) const;

    // Name exposure
    public:
      LPCTSTR
        c_name ( LPCTSTR lpszName, int nSize = 0 );
      LPCTSTR
        c_name ( ) const;
      UCHAR
        c_size ( ) const;
      int
        c_strcmp ( LPCSTR lpszCompare ) const;
      int
        c_stricmp ( LPCSTR lpszCompareNocase ) const;
      int
        c_wcscmp ( LPCTSTR lpszCompare ) const;
      int
        c_wcsicmp ( LPCTSTR lpszCompareNocase ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
    /*virtual void
        Print ( FILE *fd, int iDepthOS = 0  );*/

    // Properties
    public:
      virtual UINT
        Sizeof ( ) const;
      bool
        IsDirty ( ) const;

    // Attributes
    private:
      P3PmsgObject *m_pObject;
      bool          m_bNameDirty;
};

///////////////////////////////////////////////////////////////////////////////
//  P2PmsgObject containers and definitions
//  NOTES: Base class for VBLock objects

#define ROBJ r_Object()

class P2PmsgKERNEL_EXT P3PmsgObject
{
    // Constructors and destructure
    public:
        P3PmsgObject ( );

        P3PmsgObject ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgObject ( );

      void
        Nullify ( );
      void
        Connect ( const P3PmsgObject& rhs );
      void
        Connectx ( P2PmsgHANDLE hVBList, VBLaddr xVBLock, VBLsize nVBLockSize );
      void
        Connecta ( P2PmsgHANDLE hVBList, VBLaddr aVBLock, VBLsize nVBLockSize );

    // Operators
    public:
      P3PmsgObject&
        operator = ( const P3PmsgObject& rhs );

        operator bool ( ) const;

    // Memory management
    public:
      VBLaddr
        Alloc ( UCHAR uVBLockType, VBLsize nVBLockSize, bool bZero = true );
      VBLaddr
        Free ( VBLaddr aVBLockAddr );
      void*
        Msg2Phys ( VBLaddr aVBLockAddr ) const;
      VBLsize
        Msg2Size ( VBLaddr aVBLockAddr ) const;
      VBLsize
        GetVBLockSize ( ) const;

    // Addressing and allocations
    public:
      virtual P2Pos
        GetP2Pos ( ) const;
      virtual char*
        GetVBLock  ( ) const;
      virtual VBLaddr
        GetVBLocknn( ) const;
      virtual P3PmsgObject
        GetParent ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      BOOL
        AssertCommon ( const P3PmsgObject& rhs ) const;
    /*virtual void
        Print ( FILE *fd, int iDepthOS = 0  );*/

    // Properties
    public:
      bool
        IsVoid ( ) const;
      bool
        IsData ( ) const;
      bool
        IsName ( ) const;
      bool
        IsField ( ) const;
      bool
        IsNode ( ) const;
      bool
        IsList ( ) const;
      bool
        IsVect ( ) const;
      bool
        IsAttr ( ) const;
      bool
        IsStck ( ) const;

    // Attributes
    public:
      P2PmsgHANDLE  m_hVBList;
      INT32         m_oVBLock[88];
      VBLaddr       m_xVBLock;
      VBLaddr       m_aVBLock;
      VBLsize       m_nVBLockSize;
      UCHAR         m_uVBLock;
};

///////////////////////////////////////////////////////////////////////
//  VBLockField container and definitions
//  NOTES: Base class for VBLockField structures
//       : Intended for standalone or derived instanciation
#pragma pack(push,1)
typedef struct
{
    UCHAR       uAccessAttr;
    VBLockName  oVBLockName;
    VBLockData  oVBLockData;
} VBLockField;
struct VBLock_;
#pragma pack(pop)

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgFieldHdl;

const UCHAR AttrField_HIDDEN  = 1;     // Hidden, known access only
const UCHAR AttrField_NOCOPY  = 2;     // Not copyable, time and space dependant
const UCHAR AttrField_SORT    = 4;     // Sorted

const UCHAR AttrField_DEFAULT = 0;
const UCHAR AttrField_TRIGGER = (AttrField_HIDDEN|AttrField_NOCOPY);

class P3PmsgList;
class P3PmsgVect;
class P3PmsgNode;
class P3PmsgCurs;
class P3PmsgAttr;
class P3PmsgStck;
class P2PmsgKERNEL_EXT P3PmsgField : public P3PmsgName, public P3PmsgData
{
      friend P3PmsgList; friend P3PmsgVect; friend P3PmsgNode;
      friend P3PmsgAttr;
      friend P3PmsgStck;
      friend P3PmsgCurs;
      void
        RenderThisSafe ( );
    public:
      typedef enum
      {
        AttrCMD_Get         = 0,
        AttrCMD_Create      = 1,
        //AttrCMD_DropOnEmpty = 3,
      } AttrCMD_e;

    // Constructors and destructor
    public:
        P3PmsgField ( );

        P3PmsgField ( const P3PmsgField& rhs );

        P3PmsgField ( LPCTSTR lpszName, int nSize = 0 );

        P3PmsgField ( LPCTSTR lpszName, const P3PmsgData& oData );

        P3PmsgField ( const P2PmsgFieldHdl& hField );

        P3PmsgField ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nFieldSize );

        P3PmsgField ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgField ( );

      void
        Nullify ( ); 
      virtual void
        Connect ( P2PmsgHANDLE hVBList, VBLaddr aVBLock, VBLsize nFieldSize );

    // Operators
    public:
      P3PmsgField&
        operator = ( const P3PmsgField& rhs );
      P3PmsgField&
        operator = ( const P3PmsgData& rhs );
      P3PmsgField&
        operator = ( const P3PmsgName& rhs );
      P3PmsgField&
        operator = ( const P2PmsgFieldHdl& rhs );
      bool
        operator == ( LPCTSTR lpszName );
      virtual P3PmsgField&
        operator [] ( LPCTSTR lpszName );

        operator P3PmsgData& ( );

        operator bool ( ) const;

    // Chained reference exposures
    public:
      P3PmsgName&
        r_name ( ) const;
      P3PmsgData&
        r_data ( ) const;
      P3PmsgAttr&
        r_Attr ( AttrCMD_e eAttrCmd = AttrCMD_Get ) const;
      P3PmsgStck&
        r_Stck ( ) const;
      const P3PmsgObject&
        r_Object ( ) const;

    // Memory management
    public:
      virtual void
        Drop ( );

    // Addressing and allocations
    public:
      P2Pos
        GetP2Pos ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1  );

    // Properties
    public:
      P2PmsgFieldHdl
        GetP2PmsgFieldHdl ( );
      UCHAR
        SetAccess ( UCHAR uAccessAdd, UCHAR uAccessRemove = 0 );
      UCHAR
        GetAccess ( UCHAR uAccessMask = ~(UCHAR)0 );
      virtual UINT
        Sizeof ( ) const;
      virtual bool
        IsVoid ( ) const;
      virtual bool
        IsDirty ( ) const;
      virtual bool
        IsStacked ( ) const;
      virtual bool
        IsAttributed ( ) const;
      P2PmsgHANDLE
        GetP2PmsgHandle ( ) const;

    // Attributes
    protected:
      mutable
      P3PmsgObject  m_oObject;
      mutable
      P3PmsgAttr   *m_pP3PmsgAttr;
      mutable
      P3PmsgStck   *m_pP3PmsgStck;
      bool          m_bFieldDirty;
};

P2PmsgKERNEL_EXT P3PmsgField&
P2PmsgField_Merge ( P3PmsgField& oField, const P3PmsgField& rhs );

///////////////////////////////////////////////////////////////////////////////
//  VBLockList containers and definitions
//  NOTES: Base class for VBLockList structures
//       : Intended for standalone or derived instanciation
#pragma pack(push,1)
template < typename ADDR__, typename SIZE__ >
struct VBLockList__
{ 
    ADDR__  aFirst;
    ADDR__  aLast;
    SIZE__  nItems;
};
typedef VBLockList__ <UINT08,UINT08> VBLockList08;
typedef VBLockList__ <UINT16,UINT16> VBLockList16;
typedef VBLockList__ <UINT32,UINT32> VBLockList32;
typedef VBLockList__ <UINT64,UINT64> VBLockList64;

typedef struct
{
    UCHAR  uVBLockAttr;
    union
    {
      VBLockList08 oList08;
      VBLockList16 oList16;
      VBLockList32 oList32;
    } u;
    VBLockField oVBLockField;
} VBLockList;
#pragma pack(pop)

#define MAX_P3PmsgData_Curs 3

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgListHdl;

class P2PmsgKERNEL_EXT P3PmsgList : public P3PmsgField
{
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P3PmsgList ( );

        P3PmsgList ( const P3PmsgList& rhs );

        P3PmsgList ( LPCTSTR lpszName, const P3PmsgData& oData );

        P3PmsgList ( const P3PmsgField& oField );

        P3PmsgList ( const P2PmsgListHdl& hList );

        P3PmsgList ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nListSize );

        P3PmsgList ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgList ( );

      void
        Connect ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nListSize );

    // List operations
    public:
      UINT
        GetHeadPos ( ) const;
      P3PmsgData&
        GetNext ( UINT& aPos );
      P3PmsgList&
        AddListHead ( const P3PmsgData& oData );
      void
        DropHead ( );
      P3PmsgData&
        GetTail ( );
      UINT
        GetTailPos ( ) const;
      P3PmsgData&
        GetPrev ( UINT& aPos );
      P3PmsgList&
        AddListTail ( const P3PmsgData& oData );
      void
        DropTail ( );
      UINT
        GetCount ( );

    // Operators
    public:
      P3PmsgList&
        operator  = ( const P3PmsgList& rhs );
      P3PmsgList&
        operator  = ( const P3PmsgField& rhs );
      P3PmsgList&
        operator  = ( const P3PmsgData& rhs );
      P3PmsgList&
        operator = ( const P2PmsgListHdl& rhs );
      P3PmsgList&
        operator = ( const P3PmsgObject& rhs );
      P3PmsgList&
        operator += ( const P3PmsgList& rhs );
      P3PmsgList&
        operator += ( const P3PmsgData& rhs );

        operator P3PmsgData& ( );

        operator bool ( );

    // Navigation and 
    public:
      bool
        Delete ( UINT aPos );
      void
        Truncate ( );
      virtual void
        Drop ( );
      UCHAR
        GetVBLockType ( POSITION oCURS );

    // Addressing and allocations
    public:
      virtual UINT
        GetVBLockListSize ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1  );

    // Properties
    public:
      P2PmsgListHdl
        GetP2PmsgListHdl ( );
      virtual UINT
        Sizeof ( UCHAR uVBLock ) const;
      virtual bool
        IsDirty ( );

    // Attributes
    protected:
      UINT          m_nCurs;
      P3PmsgData   *m_pP3PmsgData[MAX_P3PmsgData_Curs];
      bool          m_bListDirty;
};

///////////////////////////////////////////////////////////////////////////////
//  VBLockVect containers and definitions
//  NOTES: Base class for VBLockVect structures
//       : Intended for standalone or derived instanciation
//#define MAX_P3PmsgData_Curs 3
#pragma pack(push,1)
template < typename ADDR__, typename SIZE__ >
struct VBLockVect__
{ 
    ADDR__  aExtra;
    SIZE__  nItems;
    ADDR__  aAlloc[32];
};
typedef VBLockVect__ <UINT08,UINT08> VBLockVect08;
typedef VBLockVect__ <UINT16,UINT16> VBLockVect16;
typedef VBLockVect__ <UINT32,UINT32> VBLockVect32;
typedef VBLockVect__ <UINT64,UINT64> VBLockVect64;

typedef struct
{
    UCHAR  uVBLockAttr;
    union
    {
      VBLockVect08 oVect08;
      VBLockVect16 oVect16;
      VBLockVect32 oVect32;
    } u;
    VBLockField oVBLockField;
} VBLockVect;
#pragma pack(pop)

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgVectHdl;

class P3PmsgNode;
class P2PmsgKERNEL_EXT P3PmsgVect : public P3PmsgField
{
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P3PmsgVect ( );

        P3PmsgVect ( const P3PmsgVect& rhs );

        P3PmsgVect ( int nElems, LPCTSTR lpszName, const P3PmsgData& oData );

        P3PmsgVect ( int nElems, const P3PmsgField& oField );

        P3PmsgVect ( const P2PmsgListHdl& hList );

        P3PmsgVect ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nListSize );

        P3PmsgVect ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgVect ( );

      void
        Connect ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nListSize );

    // Operators
    public:
      P3PmsgVect&
        operator  = ( const P3PmsgVect& rhs );
      P3PmsgVect&
        operator  = ( const P3PmsgField& rhs );
      P3PmsgVect&
        operator  = ( const P3PmsgData& rhs );
      P3PmsgVect&
        operator = ( const P2PmsgVectHdl& rhs );

        operator P3PmsgData& ( );

        operator bool ( );

    // Navigation and 
    public:
      bool
        Delete ( int nElem );
      void
        Truncate ( );
      P3PmsgField&
        InsertAt ( int nElem, const P3PmsgField& oField );
      P3PmsgNode&
        InsertAt ( int nElem, const P3PmsgNode& oNode );
      UCHAR
        GetVBLockType ( POSITION oCURS );
      int
        Goto ( int nElem );

    // Addressing and allocations
    public:
      virtual UINT
        GetVBLockVectSize ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1  );

    // Exposure
    public:
      virtual P3PmsgData&
        r_data ( ) const;
      P3PmsgData&
        r_data ( int nElem );
      virtual P3PmsgName&
        r_name ( ) const;
      P3PmsgName&
        r_name ( int nElem );
      P3PmsgField&
        r_field ( int nElem );
      P3PmsgList&
        r_list ( int nElem );
      P3PmsgVect&
        r_vect ( int nElem );
      P3PmsgNode&
        r_node ( int nElem );

    // Properties
    public:
      bool
        IsData ( int nElem );
      bool
        IsName ( int nElem );
      bool
        IsField ( int nElem );
      bool
        IsList ( int nElem );
      bool
        IsVect ( int nElem );
      bool
        IsNode ( int nElem );

      P2PmsgVectHdl
        GetP2PmsgVectHdl ( );
      virtual UINT
        Sizeof ( UCHAR uVBLock ) const;
      virtual bool
        IsDirty ( );

    // Attributes
    protected:
      UINT          m_nCurs;
      P3PmsgField  *m_pP3PmsgType;
      UINT          m_nElemMax;
      int           m_nElem;
      P3PmsgData   *m_pP3PmsgData[MAX_P3PmsgData_Curs];
      bool          m_bVectDirty;
      UINT          m_xVect;
      int           m_nVectSize;
      UINT          m_aVect;
};

///////////////////////////////////////////////////////////////////////////////
//  VBLockNode containers and definitions
//  NOTES: Base class for VBLockNode structures
//       : Intended for standalone or derived instanciation
#pragma pack(push,1)
template < typename ADDR__, typename SIZE__ >
struct VBLockNode__
{ 
    ADDR__  aFirst;
    ADDR__  aLast;
    SIZE__  nItems;
};
typedef VBLockNode__ <UINT08,UINT08> VBLockNode08;
typedef VBLockNode__ <UINT16,UINT16> VBLockNode16;
typedef VBLockNode__ <UINT32,UINT32> VBLockNode32;
typedef VBLockNode__ <UINT64,UINT64> VBLockNode64;

typedef struct
{
    UCHAR  uVBLockAddr__;
    UCHAR  uVBLockAttr;
    union
    {
      VBLockNode08 oNode08;
      VBLockNode16 oNode16;
      VBLockNode32 oNode32;
    } u;
    VBLockField oVBLockField;
} VBLockNode;
#pragma pack(pop)

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgNodeHdl;

#define P2N(name) SelectNode(_T(#name))

class P2PmsgKERNEL_EXT P3PmsgNode : public P3PmsgField
{
      friend P3PmsgCurs;
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P3PmsgNode ( );

        P3PmsgNode ( const P3PmsgNode& rhs );

        P3PmsgNode ( LPCTSTR lpszName, int nSize = 0 );

        P3PmsgNode ( LPCTSTR lpszName, const P3PmsgData& oData );

        P3PmsgNode ( const P3PmsgField& oField );

        P3PmsgNode ( const P2PmsgNodeHdl& hNode );

        P3PmsgNode ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nNodeSize );

        P3PmsgNode ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgNode ( );

      void
        Nullify ( ); 
      void
        Connect ( P2PmsgHANDLE hVBList, UINT aVBLock, UINT nNodeSize );
      P3PmsgNode&
        AddNode ( LPCTSTR lpszName, const P3PmsgData& oData );
      P3PmsgField&
        AddField( LPCTSTR lpszName, const P3PmsgData& oData );

    // Operators
    public:
      P3PmsgNode&
        operator  = ( const P3PmsgNode& rhs );
      P3PmsgNode&
        operator  = ( const P3PmsgField& rhs );
      P3PmsgNode&
        operator  = ( const P2PmsgNodeHdl& rhs );
      P3PmsgNode&
        operator  = ( const P3PmsgObject& rhs );
      P3PmsgNode&
        operator += ( const P3PmsgNode& rhs );
      P3PmsgNode&
        operator += ( const P3PmsgList& rhs );
      P3PmsgNode&
        operator += ( const P3PmsgVect& rhs );
      P3PmsgNode&
        operator += ( const P3PmsgField& rhs );
      virtual P3PmsgField&
        operator [] ( LPCTSTR lpszName );

        operator P2PmsgNodeHdl ( );

        operator P3PmsgData& ( );

    // Memory management
    public:
      virtual void
        Drop ( );

    // Navigation and 
    public:
      P3PmsgField
        Select      ( LPCTSTR lpszItemName );
      P3PmsgObject
        SelectObject( LPCTSTR lpszObjectName );
      P3PmsgField&
        SelectItem  ( LPCTSTR lpszItemName );
      P3PmsgNode&
        SelectNode  ( LPCTSTR lpszNodeName );
      P3PmsgVect&
        SelectVect  ( LPCTSTR lpszVectName );

      P3PmsgField&
        DeclareField( LPCTSTR lpszFieldName, const P3PmsgData& oData );
      P3PmsgNode&
        DeclareNode ( LPCTSTR lpszNodeName, const P3PmsgData& oData );

      bool
        Exists ( LPCTSTR lpszItemName );
      bool
        Delete ( LPCTSTR lpszItemName );
      void
        Truncate ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1 );

    // Std::List modifiers
    public:
      P2PmsgNodeHdl
        PushBack ( const P3PmsgNode& oNode );
      P2PmsgFieldHdl
        PushBack ( const P3PmsgField& oField );
      P2PmsgVectHdl
        PushBack ( const P3PmsgVect& oVect );

    // Exposure
    public:
      P3PmsgCurs&
        r_Curs ( );

    // Properties
    public:
      P2PmsgNodeHdl
        GetP2PmsgNodeHdl ( );
      UINT
        GetCount ( ) const;
      virtual UINT
        Sizeof ( UCHAR uVBLock ) const;
      virtual bool
        IsDirty ( );

    // Attributes
    protected:
      P3PmsgCurs   *m_pCurs;
      bool          m_bNodeDirty;
};

P2PmsgKERNEL_EXT P3PmsgNode&
P2PmsgNode_Merge ( P3PmsgNode& oNode, const P3PmsgNode& rhs );
P2PmsgKERNEL_EXT void
P2PmsgNode_Swap  ( P3PmsgField& oField1, P3PmsgField& oField2 );

///////////////////////////////////////////////////////////////////////////////
//  VBLockAttr containers and definitions
//  NOTES: Base class for VBLockNode structures
//       : Intended for standalone or derived instanciation
template < typename ADDR__, typename SIZE__ >
struct VBLockAttr__
{
    ADDR__  aParent;
    ADDR__  aFirst;
    ADDR__  aLast;
    SIZE__  nItems;
};
typedef VBLockAttr__ <UINT08,UINT08> VBLockAttr08;
typedef VBLockAttr__ <UINT16,UINT16> VBLockAttr16;
typedef VBLockAttr__ <UINT32,UINT32> VBLockAttr32;
typedef VBLockAttr__ <UINT64,UINT64> VBLockAttr64;

#pragma pack(push,1)
typedef struct
{
    UCHAR  uVBLockAttr;
    union
    {
      VBLockAttr08 oAttr08;
      VBLockAttr16 oAttr16;
      VBLockAttr32 oAttr32;
    } u;
} VBLockAttr;
#pragma pack(pop)

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgAttrHdl;

#define ATTR r_Attr()

class P2PmsgKERNEL_EXT P3PmsgAttr
{
      friend P3PmsgCurs;
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P3PmsgAttr ( );

        P3PmsgAttr ( const P3PmsgAttr& rhs );

        P3PmsgAttr ( P3PmsgField *pField );

        P3PmsgAttr ( const P3PmsgObject& rhs );

      virtual
       ~P3PmsgAttr ( );

      void
        Nullify ( ); 
      void
        Connect ( P3PmsgField *pField );

    // Operators
    public:
      P3PmsgAttr&
        operator  = ( const P3PmsgAttr& rhs );
      P3PmsgAttr&
        operator  = ( const P3PmsgObject& rhs );
      P3PmsgAttr&
        operator += ( const P3PmsgNode& rhs );
      P3PmsgAttr&
        operator += ( const P3PmsgList& rhs );
      P3PmsgAttr&
        operator += ( const P3PmsgVect& rhs );
      P3PmsgAttr&
        operator += ( const P3PmsgField& rhs );
      virtual P3PmsgNode&
        operator [] ( LPCTSTR lpszName );

        operator bool ( ) const;

    // Chained reference exposures
    public:
      const P3PmsgObject&
        r_Object ( ) const;

    // Memory management
    public:
      virtual void
        Create ( );
      virtual void
        Drop ( );

    // Navigation and 
    public:
      P3PmsgField
        Select      ( LPCTSTR lpszItemName );
      P3PmsgObject
        SelectObject( LPCTSTR lpszObjectname );
      P3PmsgField&
        SelectItem  ( LPCTSTR lpszItemName );
      P3PmsgNode&
        SelectField ( LPCTSTR lpszFieldName );
      P3PmsgNode&
        SelectNode  ( LPCTSTR lpszNodeName );
      P3PmsgList&
        SelectList  ( LPCTSTR lpszListName );
      P3PmsgVect&
        SelectVect  ( LPCTSTR lpszVectName );

      P3PmsgField&
        DeclareField( LPCTSTR lpszFieldName, const P3PmsgData& oData );
      P3PmsgNode&
        DeclareNode ( LPCTSTR lpszNodeName, const P3PmsgData& oData );
      bool
        Exists ( LPCTSTR lpszItemName );
      bool
        Delete ( LPCTSTR lpszItemName );
      void
        Truncate ( );
      P3PmsgCurs&
        r_Curs ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1 );

    // Std::List modifiers
    public:
      P2PmsgFieldHdl
        PushBack ( const P3PmsgField& oField );
      P2PmsgNodeHdl
        PushBack ( const P3PmsgNode& oNode );
      P2PmsgListHdl
        PushBack ( const P3PmsgList& oList );
      P2PmsgVectHdl
        PushBack ( const P3PmsgVect& oVect );

    // Properties
    public:
      UINT
        GetCount ( ) const;
      virtual bool
        IsEmpty ( ) const;
      P3PmsgField*
        GetField ( ) const;

    // Attributes
    protected:
      P3PmsgObject  m_oObject;
      P3PmsgField  *m_pP3PmsgField;
      P3PmsgCurs   *m_pCurs;
      bool          m_bAttrDirty;
};

P2PmsgKERNEL_EXT P3PmsgAttr&
P2PmsgAttr_Merge ( P3PmsgAttr& oAttr, const P3PmsgNode& rhs );
P2PmsgKERNEL_EXT P3PmsgAttr&
P2PmsgAttr_Merge ( P3PmsgAttr& oAttr, const P3PmsgAttr& rhs );

///////////////////////////////////////////////////////////////////////////////
//  VBLockStack containers and definitions
//  NOTES: Base class for VBLockNode structures
//       : Intended for standalone or derived instanciation
#pragma pack(push,1)
typedef struct
{
    UCHAR  uStackItem;                 // Pushed item attributes
    UINT   aStackItem;                 // Address of pushed item
    union
    {
      VBLockNode   oVBLockNode;
      VBLockField  oVBLockField;
      VBLockList   oVBLockList;
    } u;
} VBLockStack;
#pragma pack(pop)

typedef struct
{
    UINT uiParam1;
    UINT uiParam2;
    UINT uiParam3;
} P2PmsgStckHdl;

class P2PmsgKERNEL_EXT P3PmsgStck
{
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P3PmsgStck ( );

        P3PmsgStck ( const P3PmsgStck& rhs );

        P3PmsgStck ( const P3PmsgField *pField );

        P3PmsgStck ( const P3PmsgObject& rhs );
      virtual
       ~P3PmsgStck ( );

      void
        Nullify ( ); 
      void
        Connect ( P3PmsgField *pField );
      virtual P3PmsgStck&
        Push ( );
      virtual P3PmsgStck&
        Pop ( );

    // Operators
    public:
      P3PmsgStck&
        operator  = ( const P3PmsgStck& rhs );

    // Memory management
    public:
      virtual void
        Drop ( );

    // Navigation and 
    public:
      P3PmsgStck&
        Rename ( LPCTSTR lpszName, bool bRecurse = true );

    // Exposure
    public:
      virtual P3PmsgData&
        r_data ( );
      virtual P3PmsgName&
        r_name ( );
      virtual P3PmsgField&
        r_field ( );
      virtual P3PmsgList&
        r_list ( );
      virtual P3PmsgVect&
        r_vect ( );
      virtual P3PmsgNode&
        r_node ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1 );

    // Properties
    public:
      virtual bool
        IsEmpty ( ) const;
      P3PmsgField*
        GetField ( );

    // Attributes
    protected:
      P3PmsgField  *m_pP3PmsgField;

      P3PmsgField  *m_pStckField;
      P3PmsgNode   *m_pStckNode;
      P3PmsgList   *m_pStckList;
      P3PmsgVect   *m_pStckVect;
      bool          m_bStckDirty;
};

///////////////////////////////////////////////////////////////////////
//  P2PmsgItem's
//  NOTES: Generic P2Pmsg data item container
#pragma pack(push,1)
template < typename ADDR__ >
struct VBLockItem__
{ 
    //ADDR__  nSize;
    ADDR__  aParent;
    ADDR__  aPrev;
    ADDR__  aNext;
    ADDR__  aExtra;
    ADDR__  aStack;
};
typedef VBLockItem__ <UINT08> VBLockItem08;
typedef VBLockItem__ <UINT16> VBLockItem16;
typedef VBLockItem__ <UINT32> VBLockItem32;
typedef VBLockItem__ <UINT64> VBLockItem64;

typedef struct
{
    UCHAR     uVBLockItem;
    union
    {
      VBLockItem08 oItem08;
      VBLockItem16 oItem16;
      VBLockItem32 oItem32;
      VBLockItem64 oItem64;
    } ua;
    union
    {
      VBLockData   oVBLockData;
      VBLockNode   oVBLockNode;
      VBLockField  oVBLockField;
      VBLockList   oVBLockList;
      VBLockStack  oVBLockStack;
    } ut;
} VBLockItem;
#pragma pack(pop)

///////////////////////////////////////////////////////////////////////
//  P2PmsgCurs'or
//  NOTES: Manages P2Pmsg navigation and subsequent data exposure

#define P2PmsgCurs P3PmsgCurs
class P2PmsgKERNEL_EXT P3PmsgCurs
{
      void
        RenderCursSafe ( );

    // Constructors and destructor
    public:
        P3PmsgCurs ( const P3PmsgCurs& rhs );
        P3PmsgCurs ( P3PmsgNode& rhs );
        P3PmsgCurs ( P3PmsgAttr& rhs );
      virtual
       ~P3PmsgCurs ( );

    // Operators
    public:
      virtual P3PmsgCurs&
        operator = ( const P3PmsgCurs& rhs );
      virtual P2PmsgCurs&
        operator ++ ( );
      virtual
        operator P3PmsgName& ( );
      virtual
        operator P3PmsgField& ( );
      virtual
        operator P3PmsgList& ( );
      virtual
        operator P3PmsgVect& ( );
      virtual
        operator P3PmsgNode& ( );
      virtual
        operator P3PmsgData& ( );

    // Operations
    public:
      void
        Delete ( );

    // Navigation
    public:
      P3PmsgCurs&
        Seek ( );
      bool
        Goto ( LPCTSTR lpszItemName );
      bool
        Goto ( int nElem );

    // Exposure
    public:
      virtual const P3PmsgObject&
        r_Object ( ) const;
      virtual LPCTSTR
        c_str ( );
      virtual P3PmsgData&
        r_data ( ) const;
      virtual P3PmsgName&
        r_name ( );
      virtual P3PmsgField&
        r_field ( ) const;
      virtual P3PmsgList&
        r_list ( );
      virtual P3PmsgVect&
        r_vect ( );
      virtual P3PmsgNode&
        r_node ( ) const;
      virtual P3PmsgAttr&
        r_attr ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;

    // Properties
    public:
      bool
        IsField ( );
      bool
        IsNode ( );
      bool
        IsList ( );
      bool
        IsVect ( );
      int
        Item ( );
      virtual bool 
        IsEoCursor ( );

    // Attributes
    protected:
      P3PmsgNode   *m_pP3PmsgNode;
      P3PmsgAttr   *m_pP3PmsgAttr;
      P3PmsgField   m_oP3PmsgField;
      P3PmsgNode    m_oP3PmsgNode;
      P3PmsgList    m_oP3PmsgList;
      P3PmsgVect    m_oP3PmsgVect;
      P3PmsgField  *m_pP3PmsgFoN;
      int           m_nItem;
};

///////////////////////////////////////////////////////////////////////
//  P2PmsgRecurs
//  NOTES: Provides recursive P2PmsgNode scanning services

class P2PmsgKERNEL_EXT P2PmsgRecurs
{
      void
        RenderRecursSafe ( );

    // Constructors and destructor
    public:
        P2PmsgRecurs ( const P2PmsgRecurs& rhs );
        P2PmsgRecurs ( P3PmsgNode& rhs );
        P2PmsgRecurs ( P3PmsgAttr& rhs );
      virtual
       ~P2PmsgRecurs ( );

    // Operators
    public:
      virtual P2PmsgRecurs&
        operator = ( const P2PmsgRecurs& rhs );
      virtual P2PmsgRecurs&
        operator ++ ( );

    // Operations
    public:

    // Navigation
    public:
      int
        Push ( );
      int
        Pop ( );

    // Exposure
    public:
      virtual LPCTSTR
        c_str ( );
      virtual P3PmsgData&
        r_data ( ) const;
      virtual P3PmsgName&
        r_name ( );
      virtual P3PmsgField&
        r_field ( ) const;
      virtual P3PmsgList&
        r_list ( );
      virtual P3PmsgVect&
        r_vect ( );
      virtual P3PmsgNode&
        r_node ( ) const;

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;

    // Properties
    public:
      bool
        IsField ( );
      bool
        IsNode ( );
      bool
        IsList ( );
      bool
        IsVect ( );
      bool
        IsEoRecurs ( ) const;

    // Attributes
    protected:
      P3PmsgCurs     m_oCurs;
      P2PmsgRecurs  *m_pRecurs;
      int            m_cLevel;
};

///////////////////////////////////////////////////////////////////////
//  VBLock container and definitions
//  NOTES: Fundamental unit of memory allocation and subsequent 
//         fragmentation.  De-fragmentaion occurs through collation
//         of free'd adjacent VBLock's

#pragma pack(push,1)
template < typename SIZE__ >
struct VBLock__
{ 
    SIZE__  nPrev;
    SIZE__  nNext;
    char    cData;
};
typedef VBLock__ <UINT08> VBLock08;
typedef VBLock__ <UINT16> VBLock16;
typedef VBLock__ <UINT32> VBLock32;
typedef VBLock__ <UINT64> VBLock64;

typedef struct VBLock_
{
    VBLockHdr    oHdr;                 // Universal VBLock header
    union
    {
      VBLock08    oVBLock08;
      VBLock16    oVBLock16;
      VBLock32    oVBLock32;
      VBLock64    oVBLock64;
      VBLockItem  oItem;
      VBLockAttr  oAttr;
      VBLockNode  oNode;
      VBLockField oField;
      VBLockList  oList;
      VBLockName  oName;
      VBLockData  oData;
    } ud;                              // Contents, refer uVBLock
} VBLock;
#pragma pack(pop)

///////////////////////////////////////////////////////////////////////
//  P2Pmsg serialisation helpers
//  NOTES: Perform standard activities
P2PmsgKERNEL_EXT P3PmsgNode&
P3PmsgField_SERIALISE ( P3PmsgNode& oNode
                      , LPCTSTR lpszFieldname, const P3PmsgData& oData
                      , bool bDscAttr, LPCTSTR lpszDescription );

P2PmsgKERNEL_EXT P3PmsgAttr&
P2PmsgAttr_SERIALISE  ( P3PmsgAttr& oAttr, bool bOverwrite
                      , LPCTSTR lpszFieldname, const P3PmsgData& oData
                      , bool bDscAttr = false, LPCTSTR lpszDescription = 0 );

///////////////////////////////////////////////////////////////////////
//  P2Pmsg CMFCPropertyGridCtrl decoration helpers
//  NOTES: Perform standard activities
P2PmsgKERNEL_EXT P3PmsgField&
Decorate4Grid ( P3PmsgField& oField, UCHAR ucAttributes
              , LPCTSTR lpszGridLabel, LPCTSTR lpszGridDescription );
P2PmsgKERNEL_EXT P3PmsgField&
Decorate4Grid_FONT ( P3PmsgField& oField, UCHAR ucAttributes
                   , LPCTSTR lpszGridLabel, LPCTSTR lpszGridDescription );
P2PmsgKERNEL_EXT P3PmsgField&
Decorate4Grid_COLOR ( P3PmsgField& oField, UCHAR ucAttributes
                    , LPCTSTR lpszGridLabel, LPCTSTR lpszGridDescription );
P2PmsgKERNEL_EXT P3PmsgField&
Decorate4Grid_FILE ( P3PmsgField& oField, UCHAR ucAttributes
                   , LPCTSTR lpszGridLabel, LPCTSTR lpszGridDescription );

///////////////////////////////////////////////////////////////////////
//  P2Pmsg object helpers

P2PmsgKERNEL_EXT CString
P3Pmsg_GetPath ( const P3PmsgField *pField );
P2PmsgKERNEL_EXT CString
P3Pmsg_GetPath ( const P3PmsgAttr *pAttr );

P2PmsgKERNEL_EXT P3PmsgObject
P3Pmsg_SelectObject ( const P3PmsgObject *pObject, LPCTSTR lpszObjectPath );

///////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------
extern bool
g_bP2Pmsg_AssertValid;


/*template<class type, C_TYPE>
class P2Ptype
{
    // Constructors and destruction
    public:
        P2Ptype () { }

        P2Ptype ( const P3PmsgData& rhs )
        {
          m_oData = rhs;
        }

       ~P2Ptype () { }

    // Overloaded operators
    public:
      P2Ptype&
        operator = ( const P3PmsgData& rhs )
        {
          if ( this != &rhs )
            m_oData = rhs;
          return *this;
        }
      P2Ptype&
        operator = ( type c_type )
        {
          m_oData.#C_TYPE() = c_type;
          return P2Ptype;
        }
        operator type ()
        {
          return m_oData;
        }

    // Properties
    public:
      bool
        IsNull ( );
      bool
        IsVoid ( );

    // Attributes
    public:
        P3PmsgData m_oData;
};

typedef P2Ptype<short> P2Pshort;
typedef P2Ptype<int>   P2Pint;
        //int iJohnson = oNode["johnson"].c_int();*/