Jump to content

  • Log In with Google      Sign In   
  • Create Account

rozz666

Member Since 27 May 2007
Offline Last Active Yesterday, 08:00 AM

Topics I've Started

boost spirit error handling

15 July 2010 - 07:54 PM

I'm trying to provide some error handling to my parser. As in the example here, I use qi::on_error. However, the error handling function is never called:

namespace detail
{

struct errorMessageImpl
{
template <typename Logger, typename String, typename It>
struct result { typedef void type; };

template <typename Logger, typename String, typename It>
void operator()(Logger *logger, const String& filename, It it) const
{
logger->log(error::Message::missingMainFunction(Position(filename, it.get_position().line, it.get_position().column)));
}
};

boost::phoenix::function<errorMessageImpl> errorMessage;

}

template <typename Iterator>
struct Grammar : qi::grammar<Iterator, cst::Function(), ascii::space_type>
{
Grammar(const std::string& filename, error::Logger& errorLogger)
: Grammar::base_type(function, "function")
{
identifier %= inputPos >> qi::lexeme[qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z0-9_")];
returnType %= qi::lit("void");
function %= identifier > '(' > ')' > "->" > returnType > '{' > '}' > qi::eoi;

qi::on_error<qi::fail>
(
returnType,
detail::errorMessage(&errorLogger, filename, qi::labels::_3)
);
}

qi::rule<Iterator, cst::Identifier(), ascii::space_type> identifier;
qi::rule<Iterator, void(), ascii::space_type> returnType;
qi::rule<Iterator, cst::Function(), ascii::space_type> function;
};


boost::optional<Function> parseFile(InputStream& is, error::Logger& logger)
{
typedef std::vector<char> Source;
Source source;

source.assign(std::istreambuf_iterator<char>(is.stream()), std::istreambuf_iterator<char>());

typedef boost::spirit::classic::position_iterator<Source::const_iterator, boost::spirit::classic::file_position> PosIterator;

using boost::spirit::ascii::space;
PosIterator iter(source.begin(), source.end(), is.file());
PosIterator end;

typedef rask::cst::Grammar<PosIterator> Grammar;
Grammar grammar(is.file(), logger);
cst::Function function;

bool r = boost::spirit::qi::phrase_parse(iter, end, grammar, space, function);

if (r && iter == end)
{
return function;
}
else
{
logger.log(error::Message::missingMainFunction(Position(is.file())));

return boost::none;
}
}



Even if I replace returnType with function, the error handler is still not called.
Has anyone had a similar problem and know a solution?

Visitor pattern implementation.

01 October 2009 - 11:46 PM

I found an easy way to use the Visitor pattern using CRTP. I googled around, but haven't found such implementation, so I'm asking if somebody here have used a similar aproach.
class VisitorInterface {
public:
    virtual void visit(const Object1& ) = 0;
    virtual void visit(const Object2& ) = 0;
    virtual void visit(const Object3& ) = 0;
};

template <typename Derived>
class VisitorImplementation : public VisitorInterface {
public:

    virtual void visit(const Object1& o) { static_cast<Derived&>(*this).visitImpl(o); }
    virtual void visit(const Object2& o) { static_cast<Derived&>(*this).visitImpl(o); }
    virtual void visit(const Object3& o) { static_cast<Derived&>(*this).visitImpl(o); }
};

// concrete visitor

class MyVisitor : public VisitorImplementation<MyVisitor> {
public:

    template <typename Object>
    void visitImpl(Object& o)
    {
       // ...
    }
};


You have to implement only one method - visitImpl [Edited by - rozz666 on October 2, 2009 7:03:45 AM]

[C++] SFINAE

28 May 2009 - 08:54 PM

I don't understand why this code doesn't compile. The error is with std::iterator_traits. Compiler says it can't find iterator_category for std::vector which is fine. Therfore the second template should be excluded from matching. Somehow, it casues an error. Any ideas? Don't write that it's not useful. I know it. I'm only testing.
#include <vector>
#include <algorithm>
#include <iterator>

template <typename Cont, typename Cont2>
typename Cont2::iterator copy(const Cont& c, Cont2& dest)
{
    //return std::copy(c.begin(), c.end(), std::back_inserter(dest));
    std::copy(c.begin(), c.end(), std::back_inserter(dest));
    return dest.end();
}

template <typename Cont, typename It>
It copy(const Cont& c, It dest, typename std::iterator_traits<It>::pointer = 0)
{
    return std::copy(c.begin(), c.end(), dest);
}

int main()
{
    std::vector<int> a, b;

    copy(a, b);
}



Comeau errors:
"stl_iterator_base.h", line 110: error: class
          "std::vector<int, std::allocator<int>>" has no member
          "iterator_category"
    typedef typename _Iterator::iterator_category iterator_category;
                                ^
          detected during instantiation of class
                    "std::iterator_traits<_Iterator> [with
                    _Iterator=std::vector<int, std::allocator<int>>]" at line
                    21 of "ComeauTest.c"

"ComeauTest.c", line 21: error: more than one instance of overloaded function "copy"
          matches the argument list, the choices that match are:
            function template "Cont2::iterator copy(const Cont &, Cont2 &)"
            function template "It copy(const Cont &, It,
                      std::iterator_traits<_ForwardIter2>::pointer)"
            The argument types that you used are: (std::vector<int, std::allocator<int>>,
                      std::vector<int, std::allocator<int>>)
      copy(a, b);
      ^
[Edited by - rozz666 on May 29, 2009 7:11:36 AM]

IDE for Linux

26 May 2009 - 09:22 AM

I'm looking for an IDE for C++ under Linux. What do you recommend? I like VS 2008 IDE with it's code completion, seeking out the declarations and definitions etc. I've heard that Code::Blocks is pretty good, although I haven't tried it yet. I've also heard that's it's code completion doesn't works very well. Maybe you know a plug-in simillar to VisualAssist (wishful thinking, I know :-).

Better (probably) alternative to Deferred Shading

21 May 2009 - 09:39 PM

I was thinking about an alternative to Deferred Shading that supports transparency and came with this idea. I haven't tested it yet and I'm waiting on your opinions. Preparation: - create an R int32 (or uint32, I haven't used integer render targets yet) buffer, I call it LM-Buffer (Light Mask buffer) - attach the Z-buffer from your "main" framebuffer to LM-buffer Steps: - fill the Z-buffer for early outs - draw light volumes like you do in Deferred Shading using additive blending, giving each light R value = 1 << light_index - draw the scene like in Forward Rendering, looping thru all light and checking in the LM-buffer if each light is needed This method supports up to 32 lights assuming you use only 1 32 bit channel (you can use more). If you have different types of lights, you can just put them in ranges, e.g. 0 - 16 - spot lights with shadowmaps 16 - 24 - spot light without shadowmaps 24 - 31 - omni lights 32 - directional light The lights parameters could be passed using uniform arrays or textures. Using transparent materials is straightforward, because you don't have a G-Buffer with positions, normals, etc., but you don't have to switch states for each combination of lights. I'm still thinking about shadowsmaps. Texture arrays are an option, but I'm thinking about the limits. I'll try to test it ASAP, but have no time right now. What do you think about this idea?

PARTNERS