size of 0 after queue insertion?

Started by
0 comments, last by Storyyeller 14 years ago
I have a std queue. For some reason, the size is occasionally 0 after calling push. How is this possible? For some reason the assertion in the UserInputQueue::PushbackTick function is failing. mybuffer has a size of 0 after push is called.

#pragma once
#include <queue>
#include "BOOST/shared_ptr.hpp"

struct TickInput
{
    int xmove;
    bool jump;
    bool shoot;
    bool suicide;

    TickInput();
    void Reset();
};

class UserInputQueue
{
    std::queue<TickInput> mybuffer;
    TickInput currenttick;

    unsigned int left,right,jump;

    void ChangeKeyCount(unsigned int& count, bool down);

    public:
    UserInputQueue();

    bool Empty() const;
    const TickInput& GetInput() const;
    void PopTick();

    void Shoot();
    void Suicide();
    void Jump(bool down);
    void KeyLeft(bool down);
    void KeyRight(bool down);

    void PushbackTick();
    void PushbackTickCopy(const TickInput& newtick);
};


#include "inputstreams.h"
#include "utilityheaders/debug.h"

TickInput::TickInput(): xmove(0), jump(false), shoot(false), suicide(false) {}
void TickInput::Reset() {xmove=0;jump=false;shoot=false; suicide=false;}

void UserInputQueue::ChangeKeyCount(unsigned int& count, bool down)
{
    if(down)
    {
        ++count;
    }
    else
    {
        assert(count>0);
        --count;
    }
}

UserInputQueue::UserInputQueue():left(0),right(0), jump(0) {}

bool UserInputQueue::Empty() const {return mybuffer.empty();}
const TickInput& UserInputQueue::GetInput() const {assert(!Empty()); return mybuffer.front();}
void UserInputQueue::PopTick() {mybuffer.pop();}

void UserInputQueue::Shoot() {currenttick.shoot=true;}
void UserInputQueue::Suicide() {currenttick.suicide=true;}

void UserInputQueue::Jump(bool down) {ChangeKeyCount(jump, down);}
void UserInputQueue::KeyLeft(bool down) {ChangeKeyCount(left, down);}
void UserInputQueue::KeyRight(bool down) {ChangeKeyCount(right, down);}

void UserInputQueue::PushbackTick()
{
    if(left) {--currenttick.xmove;}
    if(right) {++currenttick.xmove;}
    if(jump) {currenttick.jump = true;}
    mybuffer.push(currenttick);
    currenttick.Reset();
    assert(!Empty());
}

void UserInputQueue::PushbackTickCopy(const TickInput& newtick)
{
    mybuffer.push(newtick);
}

I trust exceptions about as far as I can throw them.
Advertisement
Nevermind, I figured out the problem. I accidentally called Pop when it was already empty. Rather then causing an error, this somehow made the size become negative.
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement