# WIP and C++

**URL:** <https://forum.sierrawireless.com/t/wip-and-c/6762>\
**Category:** Development tools\
**Created:** [September 26, 2013, 6:25pm UTC](https://forum.sierrawireless.com/t/wip-and-c/6762 "2013-09-26T18:25:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwardofleek](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edwardofleek](https://forum.sierrawireless.com/u/edwardofleek)\
**Post date:** [September 26, 2013, 6:25pm UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/1 "2013-09-26T18:25:51Z")

</div>

Hi

Any ideas on how to use the WIP library with C++ ??

```auto
extern "C"
 {
	#include "adl_global.h"
	#include "wip.h"
class myClass
{
    public:
void thing(void){
  int a =WIP_CEV_OPEN; //Problem description: Symbol 'WIP_CEV_OPEN' could not be resolved
}
private:
}
```

---

<div class="post-metadata">

**Author:** ![daav](https://sea1.discourse-cdn.com/flex025/user_avatar/forum.sierrawireless.com/daav/32/2767_2.png) [@daav](https://forum.sierrawireless.com/u/daav)\
**Post date:** [September 27, 2013, 6:46am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/2 "2013-09-27T06:46:28Z")

</div>

Seems to be related to the fact that WIP\_CEV\_OPEN enum definition is enclosed in wip\_event\_t structure definition (cf. wip\_channel.h)  
To use this constant in C++, you’ll need to reference it as:

```auto
int a = wip_event_t::WIP_CEV_OPEN
```

---

<div class="post-metadata">

**Author:** ![edwardofleek](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edwardofleek](https://forum.sierrawireless.com/u/edwardofleek)\
**Post date:** [September 27, 2013, 7:55am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/3 "2013-09-27T07:55:49Z")

</div>

Yes thanks - you are correct.

I hadn’t thought of that and had, as a temporary workaround, redefined the events again as a typedef enum which was not a good way

---

<div class="post-metadata">

**Author:** ![edwardofleek](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edwardofleek](https://forum.sierrawireless.com/u/edwardofleek)\
**Post date:** [September 27, 2013, 10:40am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/4 "2013-09-27T10:40:03Z")

</div>

Hi

Then I got stuck again - how do we bind the callbacks - my attempt below isn’t correct ☹

Thanks in advance

Ted

```auto
class udpClass
{
    public:

	udpClass ( void );	
	void udp_start(u32 updPort);

    private:

	// Callback pointers
	static void udp_event_cbh(wip_event_t *event, void* ctx);	// Channel (socket) callback handler
};

void udpClass::udp_event_cbh(wip_event_t *event, void* ctx)
{}

udp_state_t udpClass::udp_start(u32 updPort){
       udpChannel=wip_UDPCreateOpts(&udpClass::udp_event_cbh, NULL,WIP_COPT_PORT, updPort,WIP_COPT_END);
}
```

---

<div class="post-metadata">

**Author:** ![daav](https://sea1.discourse-cdn.com/flex025/user_avatar/forum.sierrawireless.com/daav/32/2767_2.png) [@daav](https://forum.sierrawireless.com/u/daav)\
**Post date:** [September 30, 2013, 6:48am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/5 "2013-09-30T06:48:38Z")

</div>

As ADL or WIP are written in C, you have to provide C callbacks… AFAIK there is no way to provide class methods (even static ones) as callbacks to a C API…  
You’ll have to implement the callback in an extern “C” block of a cpp file.

---

<div class="post-metadata">

**Author:** ![edwardofleek](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edwardofleek](https://forum.sierrawireless.com/u/edwardofleek)\
**Post date:** [September 30, 2013, 7:54am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/6 "2013-09-30T07:54:10Z")

</div>

Hi Daav

Thanks for the information - as you can probably tell I’m not used to mixing C and C++.

I now understand - I think this C++ limitation means that it’s impossible to build a proper class which wrappers C which uses callbacks.

Shame - I was hoping to save a lot of time by creating a C++ WIP interface to enable many identical socket objects - never mind I’ll have to go back to the old C way

Ted

---

<div class="post-metadata">

**Author:** ![daav](https://sea1.discourse-cdn.com/flex025/user_avatar/forum.sierrawireless.com/daav/32/2767_2.png) [@daav](https://forum.sierrawireless.com/u/daav)\
**Post date:** [October 1, 2013, 7:17am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/7 "2013-10-01T07:17:47Z")

</div>

I think that you can do it anyhow: most (and even maybe all) of the WIP callbacks can bear an opaque “context” pointer.  
You should be able to use one single generic C callback for all your instances, and use the context pointer to carry your current object instance.

---

<div class="post-metadata">

**Author:** ![edwardofleek](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@edwardofleek](https://forum.sierrawireless.com/u/edwardofleek)\
**Post date:** [October 2, 2013, 7:56am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/8 "2013-10-02T07:56:42Z")

</div>

Hi Daav

Thanks - yes from a C perspective the context method works well.

I don’t think a WIP context could point to a C++ class member - I’m not sure of any other methods I could use to connect a WIP context to a C++ class member

---

<div class="post-metadata">

**Author:** ![MrLula](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@MrLula](https://forum.sierrawireless.com/u/MrLula)\
**Post date:** [March 8, 2014, 8:24am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/9 "2014-03-08T08:24:28Z")

</div>

It works - you just need static casts.

Here’s how we do it, in this case with a timer callback that uses a similar API:

```auto
extern "C"
{
static void FooBarTimerWrapper(u8 ID, void * Context)
{
    Foo* fooInstance = static_cast<Foo*>(Context);
    fooInstance->Bar();
}
}
```

Which gets set up from inside of a Foo instance like this:

```auto
void Foo::TriggerTimerCallback(int delay)
{
    adl_tmrSubscribeExt(ADL_TMR_CYCLIC_OPT_NONE, delay, ADL_TMR_TYPE_TICK, FooBarTimerWrapper, this, false);
}
```

---

<div class="post-metadata">

**Author:** ![davidc](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@davidc](https://forum.sierrawireless.com/u/davidc)\
**Post date:** [March 10, 2014, 5:37am UTC](https://forum.sierrawireless.com/t/wip-and-c/6762/10 "2014-03-10T05:37:16Z")

</div>

Hiya,

Just be aware that with timer contexts that if you cancel the timer (and thus don’t call the handler), there is no way to free the context if you don’t keep another link to it somewhere (which in my view sort of defeats the purpose of a context pointer) - so you will get a memory leak.

It would be nice if there was some sort of timer finalizer that could be called if the timer was cancelled…

ciao, Dave
