How I use wxArray: a shy attempt to explain... Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
GeertVc
Super wx Problem Solver
Super wx Problem Solver
Posts: 273
Joined: Fri Sep 09, 2005 4:42 pm
Location: Belgium

How I use wxArray: a shy attempt to explain...

Post by GeertVc »

Hi,

I had problems using the wxArray class. I got some hints via the C++ programming forum (http://forums.wxwidgets.org/viewtopic.php?t=3483). That was enough to try it out myself.

Here's what I've done:
  • Created a structure:

    Code: Select all

        typedef struct
    	{
                wxString ComponentName;
                wxString ComponentDir;
                wxString SubsystemName;
                wxString RootName;
                size_t   LevelComponentName;
                size_t   LevelComponentDir;
                size_t   LevelSubsystemName;
                size_t   LevelRootName;
            } ComponentDefinition;
  • Used the macro (see documentation) to instantiate an array where I will put pointers to my structure.

    Code: Select all

    WX_DEFINE_ARRAY_PTR( ComponentDefinition *, CDefArray );
    This is in fact a new ADT, with CDefArray as name (just like int, char,...)
  • Created a variable of the type CDefArray:

    Code: Select all

    CDefArray CompDefinitionArray;
    This is all done in the header file, but I guess you can do this also in the cpp file.
  • In my C-file function, I used it as follows:

    Code: Select all

    CompDefinitionArray.Add( new ComponentDefinition );
    ComponentDefinition* test = CompDefinitionArray.Item( Counter );
    
    I first add a new 'instance' of the structure, by giving it as a parameter to the Add function of the wxArray class. In fact, you create memory for the structure at that time and you pass the address to the Add function (just like defined in the prototype).

    Then I created a local pointer pointing to such structure and assigned it the value of one of the pointers located in the wxArray (indicated by a number: Counter).

    At that moment, you can access the piece of memory that has been assigned for the structure which is pointed to by the returned pointer from the wxArray, or now by the pointer variable 'test', if you like.
  • From then onwards, it's not difficult anymore to access the fields of the structure.

    It's done as follows:

    Code: Select all

    test->RootName = TempString.Mid( 0, EndPos );
    The field RootName of the structure ComponentDefinition is a wxString type and I simply assign it a substring.

    This way, you can access all the fields of the structure you defined.

    Retrieving information is equally simple. If you want to see if the content of the array (in fact, all the locations where the pointers in the array are pointing at) is correct, you can use something like this (here, I didn't recall the integers which are part of the structure, only the string fields):

    Code: Select all

        for ( size_t Counter = 0; Counter < NrOfElements; Counter++ )
        {
            ComponentDefinition* test = CompDefinitionArray.Item( Counter );
    	    
            LOGTEXT( _T( test->RootName 
                       + "\\" 
                       + test->SubsystemName
                       + "\\"
                       + test->ComponentDir
                       + "\\"
                       + test->ComponentName
                       + "\n"
                       )
                    );
        }
    
HTH,

Best rgds,

--Geert
In commemoration of my beloved Mother...
vasek
Experienced Solver
Experienced Solver
Posts: 77
Joined: Wed Jul 27, 2005 2:52 pm
Location: Ostrava, Czech Republic
Contact:

Post by vasek »

Code: Select all

        
LOGTEXT( _T( test->RootName 
                   + "\\" 
                   + test->SubsystemName 
                   + "\\" 
                   + test->ComponentDir 
                   + "\\" 
                   + test->ComponentName 
                   + "\n" 
                   ) 
                ); 
wouldn't compile under Unicode, where _T("const string") expands as L"const string" and therefore

Code: Select all

_T(test->RootName + ... )
makes no sense.
GeertVc
Super wx Problem Solver
Super wx Problem Solver
Posts: 273
Joined: Fri Sep 09, 2005 4:42 pm
Location: Belgium

Post by GeertVc »

vasek wrote:

Code: Select all

_T(test->RootName + ... )
makes no sense.
Alternative?

Best rgds,

--Geert
In commemoration of my beloved Mother...
User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton »

Code: Select all

LOGTEXT( _T( test->RootName 
                   + "\\" 
                   + test->SubsystemName 
                   + "\\" 
                   + test->ComponentDir 
                   + "\\" 
                   + test->ComponentName 
                   + "\n" 
                   ) 
                );
---->

Code: Select all

LOGTEXT(  test->RootName 
                   + _T("\\") 
                   + test->SubsystemName 
                   + _T("\\") 
                   + test->ComponentDir 
                   + _T("\\") 
                   + test->ComponentName 
                   + _T("\n") 
                   
                );
[Mostly retired moderator, still check in to clean up some stuff]
Post Reply