From: Mikko Rasa Date: Sun, 2 Sep 2012 14:29:02 +0000 (+0300) Subject: Turn VertexFormat into a class and hide the data member X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ba5c731b8aa6430bb10e38848d42d8bfb59694a6;p=libs%2Fgl.git Turn VertexFormat into a class and hide the data member Outsiders have no need to access it directly --- diff --git a/source/vertexformat.cpp b/source/vertexformat.cpp index 02410857..65b276ba 100644 --- a/source/vertexformat.cpp +++ b/source/vertexformat.cpp @@ -50,41 +50,9 @@ VertexFormat::~VertexFormat() delete[] data; } -unsigned VertexFormat::stride() const -{ - unsigned s = 0; - for(const unsigned char *i=begin(); i!=end(); ++i) - s += get_component_size(*i); - return s; -} - -int VertexFormat::offset(VertexComponent comp, unsigned index) const -{ - if((comp0) || (comp=8) || index>=53) - throw out_of_range("VertexFormat::offset"); - - unsigned type = get_component_type(comp)+index; - unsigned size = get_component_size(comp); - unsigned offs = 0; - for(const unsigned char *i=begin(); i!=end(); ++i) - { - if(get_component_type(*i)==type) - { - if(get_component_size(*i)>=size) - return offs; - else - return -1; - } - else - offs += get_component_size(*i); - } - - return -1; -} - -VertexFormat operator,(const VertexFormat &f, VertexComponent c) +VertexFormat VertexFormat::operator,(VertexComponent c) const { - VertexFormat r = f; + VertexFormat r = *this; if(r.data) { const unsigned char n = ++r.data[0]; @@ -107,11 +75,11 @@ VertexFormat operator,(const VertexFormat &f, VertexComponent c) return r; } -VertexFormat operator,(const VertexFormat &f, unsigned i) +VertexFormat VertexFormat::operator,(unsigned i) const { - if(!f.data) + if(!data) throw invalid_operation("VertexFormat::operator,"); - VertexFormat r = f; + VertexFormat r = *this; unsigned char *c = r.data+r.data[0]; if((*c0) || (*c=8) || i>=53) throw invalid_argument("VertexFormat::operator,"); @@ -120,6 +88,38 @@ VertexFormat operator,(const VertexFormat &f, unsigned i) return r; } +unsigned VertexFormat::stride() const +{ + unsigned s = 0; + for(const unsigned char *i=begin(); i!=end(); ++i) + s += get_component_size(*i); + return s; +} + +int VertexFormat::offset(VertexComponent comp, unsigned index) const +{ + if((comp0) || (comp=8) || index>=53) + throw out_of_range("VertexFormat::offset"); + + unsigned type = get_component_type(comp)+index; + unsigned size = get_component_size(comp); + unsigned offs = 0; + for(const unsigned char *i=begin(); i!=end(); ++i) + { + if(get_component_type(*i)==type) + { + if(get_component_size(*i)>=size) + return offs; + else + return -1; + } + else + offs += get_component_size(*i); + } + + return -1; +} + void operator>>(const LexicalConverter &conv, VertexComponent &c) { const string &str = conv.get(); diff --git a/source/vertexformat.h b/source/vertexformat.h index 35b5de3d..fb47484b 100644 --- a/source/vertexformat.h +++ b/source/vertexformat.h @@ -25,16 +25,21 @@ enum VertexComponent ATTRIB4 }; -struct VertexFormat +class VertexFormat { +private: unsigned char *data; +public: VertexFormat(); VertexFormat(VertexComponent); VertexFormat(const VertexFormat &); VertexFormat &operator=(const VertexFormat &); ~VertexFormat(); + VertexFormat operator,(VertexComponent c) const; + VertexFormat operator,(unsigned i) const; + bool empty() const { return !data || !data[0]; } const unsigned char *begin() const { return data ? data+1 : 0; } const unsigned char *end() const { return data ? data+1+data[0] : 0; } @@ -42,11 +47,9 @@ struct VertexFormat int offset(VertexComponent, unsigned = 0) const; }; -VertexFormat operator,(const VertexFormat &f, VertexComponent c); inline VertexFormat operator,(VertexComponent c1, VertexComponent c2) { return (VertexFormat(c1), c2); } -VertexFormat operator,(const VertexFormat &f, unsigned i); inline VertexFormat operator,(VertexComponent c, unsigned i) { return (VertexFormat(c), i); }