vuurrobins devblog

A place to dump my development stuff.

I’m currently busy with creating the DSOL code for reading key presses, and I’ve run into some interesting stuff (I think) about how to create read only class variables.

The user needs a way to read the keypresses. I want to avoid using functions for reading the keypresses, because that would lead to constructs like Keypad().released().keyA(); while Keypad.released.keyA; just seemed… better. so I tried to use variables.
The key presses are all boolean values, and because the user shouldn’t be able to change them, I wanted to make them constant. however, they still need to be updated every vblank. because the libnds functions returns a number where every bit stands for a key, I had the idea to put a private number and a public constant anonymous bitfielded struct (what a mouth full) in an union. that way, I could put the union variables (for pressed, held, released ect) in my Keys class, update the private numbers with the libnds function (declaring my update function as a friend function in the union) and the constant boolean values in the struct would also be updated, and the user wouldn’t be able to change the boolean values.

Now I wanted to do the same with the stylus position. because the libnds functions already use a struct, I don’t have to use my own anonymous bitfielded struct. however, this does means that users have to call an extra struct, like stylus.stylus.px; this is of course very ugly. I tried making the struct anonymous, but that didn’t work. I also tried to use the union as a class, but putting a constructor in the union gave some uninitialised constant variable errors, and using a union as a class also just seems… iffy.

At this point I also started to doubt the design decision. Is the advantage of using a union and constant variables worth the trouble it seems to give? wont the user be confused by using a const variable even tho the value will change (although the variables are declared const volatile). Questions I probably should have asked before I started coding, but meh. I haven’t figured out what I will do, but those functions are starting to look better and better.

O yea, singletons. seeing as there should only be 1 keys object, it would make sense to make it a singleton by making the constructors private, create 1 private object and a public fucntion to return that object. I tried this by placing the object in the heap. but, as I am using my own templated wrappers around malloc(), the compiler gave me warnings about an outside function using a private constructor. thus I have to make my wrapper functions as friend functions, something that apparantly isn’t that easy as it seems to be when using template functions. now I can try to get this right, or I can just declare the object as a global var, instead of putting it on the heap. the object is being used troughout the entire program, so it would actually make more sense that way. I’ll have some time to think about this as I recreate the union and the constant variables/functions.

moral of the story:
If you have a new and great idea, its either really a great idea that nobody thought of (or really used right) or its not as great as it seems, and thats the reason why nobody uses it, making it just look new.

Tagged with .

One Response to “reading values, unions and singletons”

  1. LCD TV says:

    You made some good points there. I did a search on the topic and found most people will agree with your blog.