Forward struct declaration in C

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Forward struct declaration in C

Darius Blaszyk
Hi,

I'm porting a piece of software to FPC but I got stuck at a forward struct
declaration. The situation is as folows.

Header 1 declares:
    struct TaskHandle__;
    typedef void (*CallBackProcPtr)(struct TaskHandle__ *task);

And Header2 provdes:
    typedef struct TaskHandle__ { int unused; } *TaskHandle

However header 2 already references header 1 because it already contains
some other type definitions, so I cannot reference header 2 in header 1
because of a circular dependency. Can FPC do something similar to C and
define a forward record?

Regards, Darius

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Forward struct declaration in C

Sven Barth-2
On 22.02.2013 09:57, [hidden email] wrote:

> Hi,
>
> I'm porting a piece of software to FPC but I got stuck at a forward struct
> declaration. The situation is as folows.
>
> Header 1 declares:
>      struct TaskHandle__;
>      typedef void (*CallBackProcPtr)(struct TaskHandle__ *task);
>
> And Header2 provdes:
>      typedef struct TaskHandle__ { int unused; } *TaskHandle
>
> However header 2 already references header 1 because it already contains
> some other type definitions, so I cannot reference header 2 in header 1
> because of a circular dependency. Can FPC do something similar to C and
> define a forward record?

A: You could move the declaration of TaskHandle__ to a common unit which
is used by the other two units.

B: As the TaskHandle__ type seems rather simple move it to the Header 1
unit with a comment "was originally declared in Header 1" (because
Header 2 already uses Header 1)

C: You could treat the "task" parmeter for the callback simply as a
pointer... (of course you reduce static typing with this)

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Forward struct declaration in C

Michael Van Canneyt
In reply to this post by Darius Blaszyk


On Fri, 22 Feb 2013, [hidden email] wrote:

> Hi,
>
> I'm porting a piece of software to FPC but I got stuck at a forward struct
> declaration. The situation is as folows.
>
> Header 1 declares:
>    struct TaskHandle__;
>    typedef void (*CallBackProcPtr)(struct TaskHandle__ *task);
>
> And Header2 provdes:
>    typedef struct TaskHandle__ { int unused; } *TaskHandle
>
> However header 2 already references header 1 because it already contains
> some other type definitions, so I cannot reference header 2 in header 1
> because of a circular dependency. Can FPC do something similar to C and
> define a forward record?

No. What you can do is merge the headers in a single unit:
PTaskHandle__ = ^TTaskHandle__;
and define TTaskHandle__  in the same block

The CallBackProcPtr only needs a pointer, so you could also simply define a pointer.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Forward struct declaration in C

Darius Blaszyk
In reply to this post by Sven Barth-2
Thanks Sven and Michael,

I will do as you proposed and simply use a pointer for now. Later once the
code has been ported I will have a look to refactor the code.

Regards, Darius




> On 22.02.2013 09:57, [hidden email] wrote:
>> Hi,
>>
>> I'm porting a piece of software to FPC but I got stuck at a forward
>> struct
>> declaration. The situation is as folows.
>>
>> Header 1 declares:
>>      struct TaskHandle__;
>>      typedef void (*CallBackProcPtr)(struct TaskHandle__ *task);
>>
>> And Header2 provdes:
>>      typedef struct TaskHandle__ { int unused; } *TaskHandle
>>
>> However header 2 already references header 1 because it already contains
>> some other type definitions, so I cannot reference header 2 in header 1
>> because of a circular dependency. Can FPC do something similar to C and
>> define a forward record?
>
> A: You could move the declaration of TaskHandle__ to a common unit which
> is used by the other two units.
>
> B: As the TaskHandle__ type seems rather simple move it to the Header 1
> unit with a comment "was originally declared in Header 1" (because
> Header 2 already uses Header 1)
>
> C: You could treat the "task" parmeter for the callback simply as a
> pointer... (of course you reduce static typing with this)
>
> Regards,
> Sven
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal