MFC Grid manual

CLongEnumFormat Class Reference

Converts enumerated value to a text and vice versa. More...

#include <Dapfor/Common/LongEnumFormat.h>

Inheritance diagram for CLongEnumFormat:

CFormat

List of all members.


Public Types

typedef std::vector< std::pair
< long, TString > > 
Container
 Container that holds pairs of formatted and non-formatted values.

Public Member Functions

 CLongEnumFormat (const Container &container)
 Constructor.
 CLongEnumFormat (const CLongEnumFormat &fmt)
 Copy constructor.
 ~CLongEnumFormat ()
 Virtual destructor.
virtual TString FormatStl (long val, const CDataObject *pDO) const
 The function converts enumerated value to a corresponding string.
virtual bool ParseStl (const TString &str, long &val, const CDataObject *pDO) const
 The function converts a formatted string to corresponding value in an enumeration.
virtual CString FormatMfc (long val, const CDataObject *pDO) const
 The function converts non-formatted value of long type to the formatted MFC string.
virtual bool ParseMfc (const CString &str, long &val, const CDataObject *pDO) const
 The function converts the formatted MFC string to the non-formatted long-type value.
virtual CFormatClone () const
 Creates a deep copy of a format.
virtual bool IsEnum () const
 Indicates whether a format presents an enumeration.
virtual bool GetItems (Container &items) const
 Fills the container with pairs value-string, stored in this object.
virtual bool AddItem (long value, const TString &text)
 Add a new pair value-string to the internal conversion table.
virtual bool RemoveItem (long value)
 Remove the conversion pair from the internal table.
virtual TString & operator[] (long value)
 Provides an access to elements in the conversion table. If the element does not exist - an empty string will be added to the table.
virtual void Serialize (CSerializer &serializer)
 Serializes a format.

Static Public Member Functions

static int GetSerializeVertion ()
 Serialization version.

Classes

struct  Item
 Represents a pair of numeric value and a corresponding formatted string. More...

Detailed Description

Converts enumerated value to a text and vice versa.

Format example:
// This example demonstrates how to present a numeric value (enum) as text in the grid


//MyClass.h file
//Declaration of some C++ class
class CMyClass : public Dapfor::Common::CDataObject
{
public:
    //It is better to use enumerations instead of long numeric values...
    //The grid can use the same identifiers to show values returned by functions of this class.
    enum
    {
        ...
        FidCity,
    };

    // A list of numeric identifiers used by CLongEnumFormat.
    // A mapping table of these identifiers and strings 
    // is declared in MyClass.cpp file
    enum City
    {
        London = 100,
        Paris,
        Totonto,
        NewYork,
    };

public:
    CMyClass(City city);
    virtual ~CMyClass();

    //Public methods
    ...
    City    GetCity() const {return m_City;}

private:
    City    m_City;

    //Declaration of map containing a list of functions that can be called by their identifiers.
    DF_DECLARE_FIELD_MAP();
};


//MyClass.cpp file

// This declaration can be used to display numeric value (enum) as text in the grid
Dapfor::Common::CLongEnumFormat::Item cities[] =
{
    {CMyClass::London,  "London"},
    {CMyClass::Paris,   "Paris"},
    {CMyClass::Totonto, "Totonto"},
    {CMyClass::NewYork, "New York"},
};

// Field map declaration.
// To put an object of CMyClass into the grid it should declare a table 
// that supports calling specified functions by identifiers. 
// We can also customize a presentation using formats included in 
// Common library or custom formats.
DF_BEGIN_FIELD_MAP(CMyClass)
    ...
    DF_ENUM_ID  (FidCity,     "City",     &CMyClass::GetCity,     0, DF_ENUM_FORMAT(cities))
DF_END_FIELD_MAP()


CMyClass::CMyClass(City city) : m_City(city)
{
}

//Virtual destructor
CMyClass::~CMyClass()
{
    //The object ends the life cycle. We notify the remaining subscribers.
    NotifyDelete();
}



//Using sample:


    //grid initialization
    ...

    Dapfor::GUI::CHeader* header = new Dapfor::GUI::CHeader();
    ...
    header->Add(new Dapfor::GUI::CColumn(CMyClass::FidCity,     "City", 100));
    m_Grid.SetHeader(header);

    Dapfor::Common::CDataObject* pDO = new CMyClass(CMyClass::Paris);
    m_Grid.Add(pDO);



Constructor & Destructor Documentation

CLongEnumFormat ( const Container container  ) 

Constructor.

Parameters:
[in] container List that contains pairs of formatted and non-formatted values.

CLongEnumFormat ( const CLongEnumFormat format  ) 

Copy constructor.

Parameters:
[in] format Format to be copied.


Member Function Documentation

TString FormatStl ( long  val,
const CDataObject pDO 
) const [virtual]

The function converts enumerated value to a corresponding string.

Parameters:
[in] val value in an enumeration.
[in] pDO Pointer to CDataObject. This parameter is optional and may be zero.
Returns:
Formatted string that corresponds to value in an enumeration.

Reimplemented from CFormat.

bool ParseStl ( const TString &  str,
long &  val,
const CDataObject pDO 
) const [virtual]

The function converts a formatted string to corresponding value in an enumeration.

Parameters:
[in] str Formatted string.
[in,out] val Value of long type that accepts a result of conversion.
[in] pDO Pointer to CDataObject. This parameter is optional and may be zero.
Returns:
True, if the string has been successfully parsed. Otherwise false.

Reimplemented from CFormat.

CString FormatMfc ( long  val,
const CDataObject pDO 
) const [virtual]

The function converts non-formatted value of long type to the formatted MFC string.

Parameters:
[in] val Non-formatted MFC string to be formatted.
[in] pDO Pointer to CDataObject. This parameter is optional and may be zero.
Returns:
Formatted string that is a result of conversion.

Reimplemented from CFormat.

bool ParseMfc ( const CString &  str,
long &  val,
const CDataObject pDO 
) const [virtual]

The function converts the formatted MFC string to the non-formatted long-type value.

Parameters:
[in] str MFC string to be formatted.
[in,out] val A value of the long type that is a result of the parsing.
[in] pDO Pointer to CDataObject. This parameter is optional and may be zero.
Returns:
True, if the string has been successfully parsed. Otherwise false.

Reimplemented from CFormat.

CFormat * Clone (  )  const [virtual]

Creates a deep copy of a format.

Returns:
Deep copy of a format object.

Implements CFormat.

bool IsEnum (  )  const [virtual]

Indicates whether a format presents an enumeration.

Returns:
Always true

Reimplemented from CFormat.

bool GetItems ( Container items  )  const [virtual]

Fills the container with pairs value-string, stored in this object.

Parameters:
on] items Container to be filled.
Returns:
True if at last one pair is found

bool AddItem ( long  value,
const TString &  text 
) [virtual]

Add a new pair value-string to the internal conversion table.

Parameters:
[in] value Non-formatted value (value stored by the data object)
[in] text Formatted value (Text displayed in the grid or edit in place control)
Returns:
true if the pair has been succesfully added. Otherwise false.

bool RemoveItem ( long  value  )  [virtual]

Remove the conversion pair from the internal table.

Parameters:
[in] value Non-formatted value (value stored by the data object)
Returns:
true if the pair has been succesfully removed. Otherwise false.

TString & operator[] ( long  value  )  [virtual]

Provides an access to elements in the conversion table. If the element does not exist - an empty string will be added to the table.

Parameters:
[in] value Non-formatted value (value stored by the data object)
Returns:
Formatted string that corresponds to the value.

void Serialize ( CSerializer serializer  )  [virtual]

Serializes a format.

Parameters:
[in] serializer Object, into which a format object is serialized.

Reimplemented from CFormat.