SMFSW / Queue

Queue handling library (designed on Arduino)
BSD 3-Clause "New" or "Revised" License
93 stars 20 forks source link

Limit on data types a Queue may hold? #7

Closed JRVeale closed 6 years ago

JRVeale commented 6 years ago

Hi,

Are there limits on the data types that one may use in the Queues created by this library?

Seeing as initialisation of the Queue involves setting the size of members of the Queue I can see it wouldn't be a good idea to use this library with items whose size isn't known at compile (aka an Arduino String).

But how about queueing objects which are members of a class? Fiddling around with your library I can get expected behaviour when queueing ints, etc. But get 'invalid conversion' errors when using the same code with class objects.

Is this behaviour you would expect? I'd appreciate any feedback you may have!

SMFSW commented 6 years ago

The limit would be 65535 in terms of records (coded on an uint16_t), but depending the size of a record, you would have to make some computations yourself to know if it would fit with the available RAM of the µc you are using.

That's right, Queue may not be used if you don't know the size of a record (unless you fix some arbitrary maximum size).

Queue wasn't especially designed to accept classes. It handles basic types (int, floats...), structures, but can also handle pointers. If you declare a Queue of pointers, you may affect your Cpp string class pointers into the queue.

I will try to add an example with queue of pointers to String class soon.

Thanks for your feedback. Hope this answers your questions! SMFSW

JRVeale commented 6 years ago

Thanks for your rapid response and clarification.

An example of the library working with structs could also be quite useful, if you can, as when I found that it wasn't working with classes I tried a struct instead. I got the same error, however. (No complaints when pushing to the queue, just when pulling from it).

Thanks for this library: I've been using it a lot recently (in conjunction with QList for when I need to queue Strings).

SMFSW commented 6 years ago

The 3 examples coming with the library are already using structs. Can you send a quick example of code that gives you 'invalid conversion' at compile time, it could help if something's wrong with the library. Thanks! SMFSW

JRVeale commented 6 years ago

Oh yes, of course they are! That's what gave me the idea of converting the data in my class into a struct and using just that in the Queue in the first place.

I'll have to whip up a stripped down version of my code that has the same error. But will put that together now. There's always the chance that I'm just misusing the library though!

Thanks

JRVeale commented 6 years ago

Ah, I think the mistake was on my end after all. I seem to have both structs and classes working within the Queue at this point.

The change I made was from this: MyClass my_object(); my_queue.pull(&my_object); Which would yield invalid conversion from 'Command (8)()' to 'void*' [-fpermissive].

To this: MyClass my_object; //not calling the constructor here? my_queue.pull(&my_object);

I see the difference, but my knowledge of cpp is too limited to understand why it's a problem. And I foresee this meaning that I can't pull from the Queue to set the values of an pre-existing instance of the class (without creating a temporary instance as a go-between that is)?

SMFSW commented 6 years ago

True, by using (), you instanciate the object using the constructor. When not using (), you just declare the object that will hold the datas when using pull method at the object address.

If you need to modify an instance of a class without passing through a temp object (which is not the object you want to modify), I guess you should probably try to queue pointers to your objects and store them in the queue instead of storing the objects themselves, this way, you would point directly to the object you want to modify.

MyClass * my_object; You then can access your object directly by: my_object->fct()

I did not have the time yet to make an example using classes and another using pointers, but I will try to make that in the next few days. I just have to think of consistent examples using String class, which seems of a common use with queues.

JRVeale commented 6 years ago

That certainly clears things up for me, and I will bear queuing pointers in mind if the need ever arises.

Thank you very much.

SMFSW commented 6 years ago

Queue of String class pointers added in exemple sketches