1

I'm a newbie in C and, for my apprenticeship, I have to try to convert an Arduino code to a C code, but I'm in trouble. Into one of headers, I have this template:

template <class LinkType> class TPixy
{
public:
  TPixy(uint16_t arg=PIXY_DEFAULT_ARGVAL);
  ~TPixy();

  uint16_t getBlocks(uint16_t maxBlocks=1000);
  int8_t setServos(uint16_t s0, uint16_t s1);
  int8_t setBrightness(uint8_t brightness);
  int8_t setLED(uint8_t r, uint8_t g, uint8_t b);
  void init();

  Block *blocks;

private:
  boolean getStart();
  void resize();

  LinkType link;
  boolean  skipStart;
  BlockType blockType;
  uint16_t blockCount;
  uint16_t blockArraySize;
};

But C language doesn't have template, so I have to convert it to something "compatible" considering that there is a typedef in another header that calls this template:

typedef TPixy<LinkUART> PixyUART;

I thought I could convert the template to a macro by using #define, but I do not know if it's right, and if it were, I do not know how to do.

How can I resolve the problem? Can you help me?

  • Please, remove the 'C++' tag if you intend to write C, or change the title if C++ would be an option. – Scheff's Cat Feb 17 '17 at 11:05
  • Instantiate every template by yourself (making new structure for every type that you use instead `LinkType`). – Yuriy Ivaskevych Feb 17 '17 at 11:07
  • @quetzalcoatl I meant he can manually write every needed instantiation. So remove `template....` and instead do different structs: `struct IntPixy{...}`, `struct LinkUARTPixy{...}` and so on. – Yuriy Ivaskevych Feb 17 '17 at 11:14

1 Answers1

1

This template looks very simple, so you should be able to simply remove the template<LinkType> keyword, and simply replace all LinkType with your LinkUART.

However, please note that this is a class. You will have much more to do to convert it all to plain C, since there is no support for classes there.

In short, remove the template keyword, rename the TPixy class to PixyUART, cut all member functions out (watch out for ctor and dtor!), make them standalone functions that take a pointer to this class, i.e.:

int8_t setBrightness();

into

int8_t PixyUART_setBrightness(PixyUART* obj);

Inside their implementation, change all this-> to obj->.

You should end up with a class that has only fields now. Change the class keyword to struct and add typedefs and any C glitter as needed, and basically, it should be it.

More work may be needed, but that's mandatory start point.

Again, watch out for ctors and dtors. You will need to use them manually, since malloc/free will not call them automatically.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • thank you, @quetzalcoatl. I'm trying to follow your suggestion. The `struct` now it looks like this: `struct PixyUART { TPixy(uint16_t arg=PIXY_DEFAULT_ARGVAL); ~TPixy(); };` I have cut off the members function and I have modify them like this: `uint16_t PixyUART_getBlocks(PixyUART* uint16_t maxBlocks); uint16_t maxBlocks->1000;` Is that correct? I still have to modify ctor and dtor (I don't know what you mean by saing "You will need to use them manually". Sorry) – Elia Schoen Feb 17 '17 at 12:15
  • @EliaSchoen: No, this is not correct. You have to LEAVE all FIELDS inside the struct. You lost all `blocks`, `link`, `skipStart` etc **fields**. These all **must** remain inside the class (now `struct`). You may want to read http://stackoverflow.com/questions/5872915/converting-a-c-class-to-a-c-struct-and-beyond - it shows some basics, but mind that this is not all that you may need to use. With `malloc/free` I meant that in C++, `new/delete` automatically call ctor/dtor. In C, `malloc/free` does not. You must manually to call ctor after malloc and dtor before free (if ctor/dtor exists at all) – quetzalcoatl Feb 17 '17 at 12:28
  • @EliaSchoen: and last word of advise, if you have any classes in your C++ code that have `virtual` methods and/or nontrivial inheritance, you may have a tough time translating it directly to C. You will probably have to redesign the code a bit. However, all of that is outside of this current question. Search for translating C++ classes and methods into C structs and functions (there's a lot of materials on that), or ask about that specifically in a new question. – quetzalcoatl Feb 17 '17 at 12:32