notcurses/include/ncpp/Reader.hh
Marek Habersack 9ca8d9c9c6 Correctly cast ncpp::Plane to ncplane*
Fixes: https://github.com/dankamongmen/notcurses/issues/616

SIGSEGV was caused by an invalid cast.

Short explanation: PEBKAC

Long explanation: `Selector.hh`, `Plot.hh` and `MultiSelector.hh` did
not include `Plane.hh`, they merely declared `class Plane;` because
inclusion of `Plane.hh` would cause circular dependencies to appear and
the compiler would be unhappy.  On top of that, yours truly wrenched the
compiler's hands and caused it to believe that a pointer to `Plane` is
really a pointer to `ncplane*` which was quite a silly thing to do as
the compiler, not having included `Plane.hh` and thus not knowing full
definition of the type, wasn't able to look up the type cast operator in
`Plane`.

Don't abuse `reinterpret_cast`, kids!
2020-05-23 20:26:13 -04:00

62 lines
1.3 KiB
C++

#ifndef __NCPP_READER_HH
#define __NCPP_READER_HH
#include <notcurses/notcurses.h>
#include "Root.hh"
#include "NCAlign.hh"
#include "NotCurses.hh"
#include "Utilities.hh"
namespace ncpp
{
class Plane;
class NCPP_API_EXPORT Reader : public Root
{
public:
explicit Reader (Plane *p, int y, int x, const ncreader_options *opts)
: Reader (Utilities::to_ncplane (p), y, x, opts)
{}
explicit Reader (Plane const* p, int y, int x, const ncreader_options *opts)
: Reader (const_cast<Plane*>(p), y, x, opts)
{}
explicit Reader (Plane &p, int y, int x, const ncreader_options *opts)
: Reader (reinterpret_cast<Plane*>(&p), y, x, opts)
{}
explicit Reader (Plane const& p, int y, int x, const ncreader_options *opts)
: Reader (const_cast<Plane*>(&p), y, x, opts)
{}
explicit Reader (ncplane* n, int y, int x, const ncreader_options *opts)
{
reader = ncreader_create (n, y, x, opts);
if (reader == nullptr)
throw init_error ("Notcurses failed to create a new reader");
}
~Reader ()
{
if (!is_notcurses_stopped ())
ncreader_destroy (reader, nullptr);
}
char* get_contents () const noexcept
{
return ncreader_contents(reader);
}
Plane* get_plane () const noexcept
{
return Plane::map_plane (ncreader_plane (reader));
}
private:
ncreader *reader;
};
}
#endif