KFoundation  v2.1
A Better Foundation Library for C++
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
kfoundation::Token Class Referenceabstract

Represents a token in a stream. More...

#include <kfoundation/ObjectStreamReader.h>

Inheritance diagram for kfoundation::Token:
Inheritance graph
[legend]

Public Member Functions

 Token (const CodeRange &cr)
 Constructor. More...
 
bool is (const type_t &t) const
 Checks if the type of this token matches the given argument.
 
void validateType (const type_t &t) const
 Checks of the type of this token matches the given argument, and if not, produces a ParseException explaning the problem.
 
Ref< ObjectTokenasObject ()
 Validates if this token represents an object. More...
 
Ref< EndObjectTokenasEndObject ()
 Validates if this token represents end of an object. More...
 
Ref< AttributeTokenasAttribute ()
 Validates if this token represents an attribute. More...
 
Ref< CollectionTokenasCollection ()
 Validates if this token represents begining of a collection. More...
 
Ref< EndCollectionTokenasEndCollection ()
 Validates if this token represents end of a collection. More...
 
- Public Member Functions inherited from kfoundation::ObjectStreamReader
virtual Ref< Tokennext ()=0 throw (ParseException)
 Returns the next token in the stream (not the token after this one).
 
- Public Member Functions inherited from kfoundation::KFObject
bool equals (RefConst< KFObject > other) const
 Checks if this object is the same as the one reffered by the given refrence.
 

Static Public Member Functions

static RefConst< UString > toString (const type_t t)
 Returns a string corresponding the given parameter.
 

Public Attributes

const CodeRange codeRange
 CodeRange marking begining and end of this token.
 

Detailed Description

Represents a token in a stream.

This is an abstract class. The actual object might be of any of the following types:

Use getType() method to determine the type and cast accordingly. Most often this object is used in predictive parsing manner. For example:

 void deserialize(Ref<ObjectToken> headToken) {
     headToken->validateClass("MyClass");
     Ref<Token> token = headToken->next();
     token->validateType(Token::ATTRIBUTE);
     _name = token.AS(AttributeToken)->validateName("name")->getValue();
     token->next()->validateType(END_OBJECT);
 }

Conditional statements can be added if desired:

 void deserialize(Ref<ObjectToken> headToken) {
     headToken->validateClass("MyClass");
     Ref<Token> token = headToken->next();
     if(token.is(Token::ATTRIBUTE)) {
         Ref<AttributeToken> attrib = token.AS(Attribute);
         if(attrib->checkName("attrib1")) {
             _attrib1 = attrib->getValue();
         } else if(attrib->checkName("attrib2") {
             _attrib2 = attrib->getValue();
         } else {
             attrib->throwInvalidName();
         }
     } else {
         token->validateType(Token::OBJECT);
         ...
     }
     token->next()->validateType(END_OBJECT);
 }

validateXXX() methods cause an expection to be thrown if the given argument does not match the current token. The exception message will include a code location that helps the user to understand the problem.

See Also
ObjectDeserializer

Constructor & Destructor Documentation

kfoundation::Token::Token ( const CodeRange cr)

Constructor.

Parameters
crThe range marking the begining and the end of input containing this token.

Member Function Documentation

Ref< AttributeToken > kfoundation::Token::asAttribute ( )

Validates if this token represents an attribute.

If so casts itself into AttributeToken, otherwise throws a ParseException.

Returns
Pointer to this object casted into AttributeToken.
Exceptions
ParseExceptionif the type of this token is not AttributeToken.
Ref< CollectionToken > kfoundation::Token::asCollection ( )

Validates if this token represents begining of a collection.

If so casts itself into CollectionToken, otherwise throws a ParseException.

Returns
Pointer to this object casted into CollectionToken.
Exceptions
ParseExceptionif the type of this token is not CollectionToken.
Ref< EndCollectionToken > kfoundation::Token::asEndCollection ( )

Validates if this token represents end of a collection.

If so casts itself into EndCollectionToken, otherwise throws a ParseException.

Returns
Pointer to this object casted into EndCollectionToken.
Exceptions
ParseExceptionif the type of this token is not EndCollectionToken.
Ref< EndObjectToken > kfoundation::Token::asEndObject ( )

Validates if this token represents end of an object.

If so casts itself into EndObjectToken, otherwise throws a ParseException.

Returns
Pointer to this object casted into EndObjectToken.
Exceptions
ParseExceptionif the type of this token is not EndObjectToken.
Ref< ObjectToken > kfoundation::Token::asObject ( )

Validates if this token represents an object.

If so casts itself into ObjectToken, otherwise throws a ParseException.

Returns
Pointer to this object casted into ObjectToken.
Exceptions
ParseExceptionif the type of this token is not ObjectToken.

The documentation for this class was generated from the following files: