structs

This is a discussion on structs within the C++ Programming forums, part of the General Programming Boards category; I'm trying to remove device specific defines from my files. I would like to put all device specific names in ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    structs

    I'm trying to remove device specific defines from my files. I would like to put all device specific names in a single header file. Currently I check a device type value and based on this value I will read/write a certain value defined in a header. I would like to make this read/write value generic.
    For example:
    if (type == 1)
    read(a);
    elseif (type ==2)
    read(b);
    else
    read(c);

    Every time I add a new type I have to find every instance and add a case.

    I would prefer doing:
    read(x);
    every time;

    I was thinking I could create a generic struct with a list of all the variables I need.
    Based on this I could initialize the structure once s.t. x = a, b, or c as needed. This values are determined once at the begininng and never need to change later.

    I would prefer not creating a function to initialize the structure based on my device either. Is there a way to create a generic structure and have multiple instances predefined and generic pointer that can point to any structure I choose:

    Something like this:

    Code:
    struct generic
    {
         int x = 0;
         int y = 0;
    } A, B;
    
    A.x = 1;
    A.y= 2;
    B.x = 1;
    B.y= 2;
    And then have a pointer to type generic that I can give the address of A or B to? I can't seem to get it working. I wanted to do this all in a header file so it is reusable and have my app. have a pointer that points to either A or B as needed. Is this possible and how can it be done?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    This is a standard application of runtime polymorphism. Derived classes, virtual functions, yadda yadda.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is also standard practice in writing drivers to have some sort of "device" structure (or class) that defines which device is which. For example, you would store the base-address of the memory mapped IO registers of a piece of hardware within the struct, and you'd have a second struct for a second card.

    If you have a defined maximum number of units, you could use an array of structs, thus avoiding the need to allocate new "device structs". Of course, if these structs are large, you may prefer to allocate dynamically.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    I already have a device structure with everything generic already in there. The problem is every time I read or write a register I need to check the device type and run a different line of code for each register read/write based on the address I need to access. I have a generic base address but the offset is device specific. I would like to have this new set up generics that I can define dynamically and only once. This way each new board would be easy to add.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    The only solution that comes to mind is a function that initializes the structure based on the device type but I would prefer doing this in a header file.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without more details of exactly what the differences are in your "devices", it's hard to recommend a solution that will work right for your situation.

    Are these identical devices, but different locations, or are they "similar but not quite equivalent"?

    Are the devices known at compile time, or only once you have the software running?

    Presumably initializing a device is a relatively rare occurance (compared to USING the device for whatever purpose the device exists), so I'm not sure why you feel that it needs to be done in a header-file. Calling a function should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    The devices are almost equivalent but addresses differ. The device type is not known at compile time. I don't need to do it in a header file but the driver and application both use the header file. The function would then need to be called by my driver, my application and my DLL. It just seems much easier to select a set of predefined values instead of defining them repeatedly.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    That and the driver is in C and the app in C++. I'm not sure if that would cause problems for a shared file. Could I have both share a C file?

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    I thought of a way but I would need to initialize values in a structure. Is this possible?

    E.G.:

    Code:
    struct Regs
    {
         int c[2] = {3, 4};
    };
    Then I could use my device type as an index into the structure variables. Is there any way to initialize a structure in a header file?

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    This is what I decided to go with. Anyone see a problem:

    Code:
    struct DM64x_DevType
    {
    	struct DM64x_REGS *pDM64x_REGS;
    };
    
    struct DM64x_REGS
    {
    	UINT32 a;
    	UINT32 b;
    };				
    
    static struct DM64x_REGS DM642  =  {0, 5};
    static struct DM64x_REGS DM6437 = {1, 6};
    static struct DM64x_REGS DM648  =  {2, 4};
    
    extern struct DM64x_DevType RegDefines;
    
    /*
    I declare one instance of the first struct and include this header anywhere this structure is accessed
    */

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You'll have three separate DM64x_REGS instances in every file you include the header. Can't you just make them extern and define them in one source file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from CornedBee's suggestion, yes, it seems sensible.

    Not sure why you need a pointer to struct inside a struct - or is there more "stuff" inside the first struct, but you don't show that here?

    Presumably, you pass a pointer to the struct DM64x_DevType to each function? If you have a pointer with a pointer inside it, it takes more effort for the code to get to the actual data, than if your register addresses where straight inside the first struct [even if it's another struct, as long as it's not a pointer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21