Boost StateChart and Template parameters
Hello List,
I would like to have a templated parse function within each of my
states. Unfortunatly I am unable to successfully create a version that
compiles, perhaps if someone could show how to get it to work.
Here is the code:
namespace xml {
class prolog;
template<typename T>
class Entity {
public:
virtual boost::regex regex() const = 0;
virtual void parse(T,T) = 0;
};
class document
: public boost::statechart::state_machine<
document,
prolog
> {
public:
virtual ~document(
) {};
private:
friend class prolog;
static boost::regex character, white_space,
name_start_character,
name_character, name,
names, nmtoken, nmtokens, comment,
process_instruction_target,
process_instruction, CDATA, equal,
version_info, encoding_name,
encoding_declaration,
standalone_document_declaration,
xml_declaration, prolog;
protected:
document(
) {
initiate();
};
template<typename T>
boost::regex regex(
) const {
return state_cast
class prolog : public Entity ///<-----needs argument here but compains the 'prolog' is NOT a template
You should either make a decision about Entity template argument here, or make the above state (and all the FMS) a template. [...]
virtual void parse( T begin, T end ) { };
What is "T" here? Again, probably you meant to make all the FSM templated?
Igor R wrote:
class prolog : public Entity ///<-----needs argument here but compains the 'prolog' is NOT a template
You should either make a decision about Entity template argument here, or make the above state (and all the FMS) a template.
[...]
virtual void parse( T begin, T end ) { };
What is "T" here? Again, probably you meant to make all the FSM templated?
Hello Igor. Short version : parse(T begin,T end) should iterators from asio::streambuf or any other Biderection iterators. Long version: Well all that I wanted was to have parse(T begin,T end) available on all states. class entity { public: virtual boost::regex& regex() const = 0; template<typename T> virtual void parse(T,T) = 0; }; I am not allowed to use 'template' on virtual methods (compiler error) so I moved the template parameter to be applied to the whole class: template<typename T> class entity { public: virtual boost::regex& regex() const = 0; virtual void parse(T,T) = 0; }; But then I needed to pass a template parameter to class prolog because it is derived from class entity and therefore it needs to tell entity what type entity needs to be. And then when I needed to instantiate prolog the document class needed chainging also. Any Advice? Should I just make the whole thing a template? If I do so will there be a copy of the static data for each instantiated type? Etienne
Never mind, I just decided to remove the templates completely and just replace entity with: class entity { public: typedef boost::asio::buffers_iterator< boost::asio::streambuf::const_buffers_type > iterator; virtual boost::regex& regex() const = 0; virtual void parse(iterator,iterator) = 0; }; Thank you. Etienne
Hello List
After all of that I get caught with this Compiler error:
xml.hpp:77: error: passing ‘const xml::entity’ as ‘this’ argument of
‘virtual void
xml::entity::parse(boost::asio::buffers_iterator
Hi Etienne
So a short question is 'How do you pass data from "outside" the state machine into the state machine'.
That's what events are for. Have you had a look at the tutorial?
Is it worth it?
I guess I don't understand what high level goal you want to achieve and how Boost.Statechart is supposed to fit in. Could you please elaborate? Thanks, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
Andreas Huber wrote:
Hi Etienne
So a short question is 'How do you pass data from "outside" the state machine into the state machine'.
That's what events are for. Have you had a look at the tutorial? Yes, I have.
Is it worth it?
I guess I don't understand what high level goal you want to achieve and how Boost.Statechart is supposed to fit in. Could you please elaborate?
XML Specification: document := prolog element Misc* IE 3 distinct states. First state 'prolog': xml declaration (maximum of 1 occurence) OR comment (no upper limit) OR process instruction (no upper limit) OR white space (no upper limit) Including 'start element' and 'empty element' as to detect state transition from prolog to element according to the above definition. The event to fire that of is well a range of bytes that will match 'prolog' and if it does not match anything or matches xml declaration more than once and error should be thrown. So at a high level the state machine is keeping track on what I would expect to be my next set of valid xml entity constructs. The only way that the state machine knows that it must 'advance' to the next state is when I pass the iterators for the content is needs to search and it matches the conditions for it to advance.
Thanks,
"Etienne Philip Pretorius"
XML Specification:
document := prolog element Misc*
IE 3 distinct states.
First state 'prolog': xml declaration (maximum of 1 occurence) OR comment (no upper limit) OR process instruction (no upper limit) OR white space (no upper limit)
Including 'start element' and 'empty element' as to detect state transition from prolog to element according to the above definition.
The event to fire that of is well a range of bytes that will match 'prolog' and if it does not match anything or matches xml declaration more than once and error should be thrown.
So at a high level the state machine is keeping track on what I would expect to be my next set of valid xml entity constructs.
The only way that the state machine knows that it must 'advance' to the next state is when I pass the iterators for the content is needs to search and it matches the conditions for it to advance.
Hmmm, you are probably using the wrong tool. Although you can use Boost.Statechart for parsing, I would definitely not recommend it. It is too heavy-weight for such a task. Boost.Spirit will almost certainly lead to better results. HTH, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
Andreas Huber wrote:
Hmmm, you are probably using the wrong tool. Although you can use Boost.Statechart for parsing, I would definitely not recommend it. It is too heavy-weight for such a task. Boost.Spirit will almost certainly lead to better results.
HTH,
Thank you. I did look at boost Spirit and did not see how I could use it with asio::async_read_until ... so here I am. I did a lot of regex and they are now starting to work... but I need to keep track of the state changes - so I will probably to it via another mechanism. On another note, any good online documentation about C++ traits and facets? Thank you, Etienne
participants (3)
-
Andreas Huber
-
Etienne Philip Pretorius
-
Igor R