Sign in to follow this  
Storyyeller

size of 0 after queue insertion?

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this