patterncppMinor
Mouse input handler which exposes read-only mouse state data
Viewed 0 times
readexposesdatainputstatewhichhandleronlymouse
Problem
I have a
The problem is that C++ fails to provide a keyword otherwise similar to
Options I have identified at this point:
MouseInputHandler class that registers itself to an InputManager. When the InputManager class receives events they are forwarded to MouseInputHandler which changes its state to reflect the state of the mouse. A mouse as an input device provides a large number of different properties that should be easy to read, but impossible to write to.The problem is that C++ fails to provide a keyword otherwise similar to
const but that grants full write access to functions in the same scope.class MouseInputHandler : public InputHandler
{
public:
MouseInputHandler(ResourceContext &context)
: btnLeft(btnLeft_), btnMiddle(btnMiddle_), btnRight(btnRight_),
btnX1(btnX1_), btnX2(btnX2_), absX(absX_), absY(absY_), relX(relX_),
relY(relY_), scrollX(scrollX_), scrollY(scrollY_) {}
void handleEvent(SDL_Event &mouseEvent);
void refresh();
const Signal &btnLeft, &btnMiddle, &btnRight, &btnX1, &btnX2;
const int32_t &absX, &absY, &relX, &relY, &scrollX, &scrollY;
protected:
Signal btnLeft_, btnMiddle_, btnRight_, btnX1_, btnX2_;
int32_t absX_, absY_, relX_, relY_, scrollX_, scrollY_;
};Options I have identified at this point:
- Declaring the members private or protected and providing public const references to the non-public members that only allow read access (as in the example above). However with 11 members, the class quicly becomes a mess, with 11 const references (that also require 11 reference initializations in the constructor's initializer list).
- Adding getter methods for every member variable. More cumbersome than the const references, but with the (marginal) added benefit that the members are now accessible as functions.
- Placing the buttons and integers in (private or protected) arrays and adding an enum that gives indexes to the array elements meaningful names. To access the members, two (one getter for buttons, other for ints) getter functions that take the index and return a const referen
Solution
You could represent the mouse state using a struct:
Or a property-like get-accessor method like:
struct MouseSettings
{
Signal btnLeft_, btnMiddle_, btnRight_, btnX1_, btnX2_;
int32_t absX_, absY_, relX_, relY_, scrollX_, scrollY_;
}
class MouseInputHandler : public InputHandler
{
public:
MouseInputHandler(ResourceContext &context)
: mouseSettings(mouseSettings_)
{}
void handleEvent(SDL_Event &mouseEvent);
void refresh();
const MouseSettings& mouseSettings;
protected:
MouseSettings mouseSettings_;
};Or a property-like get-accessor method like:
const MouseSettings& getMouseSettings() { return mouseSettings_; }Code Snippets
struct MouseSettings
{
Signal btnLeft_, btnMiddle_, btnRight_, btnX1_, btnX2_;
int32_t absX_, absY_, relX_, relY_, scrollX_, scrollY_;
}
class MouseInputHandler : public InputHandler
{
public:
MouseInputHandler(ResourceContext &context)
: mouseSettings(mouseSettings_)
{}
void handleEvent(SDL_Event &mouseEvent);
void refresh();
const MouseSettings& mouseSettings;
protected:
MouseSettings mouseSettings_;
};const MouseSettings& getMouseSettings() { return mouseSettings_; }Context
StackExchange Code Review Q#42447, answer score: 7
Revisions (0)
No revisions yet.