SmartFoxServer 2X C++ API
Sfs2X::Entities::Match::MatchExpression Class Reference

Match Expressions are built like "if" conditions in any common programming language. More...

#include <MatchExpression.h>

Inherits enable_shared_from_this< MatchExpression >.

Public Member Functions

 MatchExpression (boost::shared_ptr< string > varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Constructor More...
 
 MatchExpression (string varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Constructor More...
 
boost::shared_ptr< MatchExpressionAnd (boost::shared_ptr< string > varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Concatenate the current expression with a new one using the logical AND operator More...
 
boost::shared_ptr< MatchExpressionAnd (string varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Concatenate the current expression with a new one using the logical AND operator More...
 
boost::shared_ptr< MatchExpressionOr (boost::shared_ptr< string > varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Concatenate the current expression with a new one using the logical OR operator More...
 
boost::shared_ptr< MatchExpressionOr (string varName, boost::shared_ptr< IMatcher > condition, boost::shared_ptr< unsigned char > varValue)
 Concatenate the current expression with a new one using the logical OR operator More...
 
boost::shared_ptr< string > VarName ()
 Get the name of the variable or property that is being matched. More...
 
boost::shared_ptr< IMatcherCondition ()
 Get the condition used for matching More...
 
boost::shared_ptr< void > VarValue ()
 The value used to test the condition in the expression More...
 
boost::shared_ptr< LogicOperatorLogicOp ()
 Get the current logic operator, could be null if the expression has no other concatenated expressions More...
 
bool HasNext ()
 Check if the current expression is concatenated to another one via a logic operator More...
 
boost::shared_ptr< MatchExpressionNext ()
 Get the next expression chained to the current one. More...
 
boost::shared_ptr< MatchExpressionRewind ()
 Rewinds the cursor to the first expression in the chain and return the MatchExpression at the top of the chain of expressions More...
 

Detailed Description

Match Expressions are built like "if" conditions in any common programming language.

Overview

Match Expressions are built like "if" conditions in any common programming language. They work like queries in a database and can be used to search for Rooms or Users using custom criteria. These expressions are extremely easy to create and concatenate and they can be used for many different filtering operations within the SFS2X framework.

This is a quick example:

boost::shared_ptr<int> value(new int());
*value = 5;
boost::shared_ptr<MatchExpression> exp(new MatchExpression("rank", NumberMatch::GREATER_THAN, value));
boost::shared_ptr<string> country(new string("Italy"));
exp->And("country", StringMatch::EQUALS, country);

Expressions are made of three elements:

  • Variable name
  • Match operator
  • Value

Additionally any number of expressions can be linked together with a logical AND / OR operator, just like in regular code. In the above example we have created an expression that will check for a rank value > 5 and a country value == "Italy".

The search options are not just limited to User/Room Variables name. In fact the Matching engine provides two extra classes, RoomProperties and UserProperties, where you can access many specific attributes of the Room and User class.

This is an example of matching specific Room properties and Variables:

// Prepare match expression
boost::shared_ptr<bool> value(new bool());
*value = true;
boost::shared_ptr<MatchExpression> exp(new MatchExpression(RoomProperties::IS_GAME, BoolMatch::EQUALS, value));
boost::shared_ptr<bool> value1(new bool());
*value1 = true;
exp->And(RoomProperties::HAS_FREE_PLAYER_SLOTS, BoolMatch::EQUALS, value1);
// Search Rooms
boost::shared_ptr<IRequest> requestFindRoomsRequest(new FindRoomsRequest(exp));
m_ptrSmartFox->Send(requestFindRoomsRequest);

Advanced features

the Match expression offer advanced capabilities of searching through nested data structures such as SFSObject and SFSArray. This is done via a very simple dot-syntax. Here's an example of how it works:

boost::shared_ptr<string> value(new string("Rome"));
boost::shared_ptr<MatchExpression> exp(new MatchExpression("europe.italy.capital", StringMatch::EQUALS, value));
The above example goes down deep into an SFSObject called <em>europe</em>, taking the <em>italy</em> object (another SFSObject) and finally reading its String field <em>capital</em> and matching it with another String.
Here is one more examples using SFSObject and SFSArray:
boost::shared_ptr<string> value(new string("Milan"));
boost::shared_ptr<MatchExpression> exp(new MatchExpression("europe.italy.majorCities.3.name", StringMatch::EQUALS, value));

From the italy object we obtain a majorCities SFSArray and we grab the third item in it (the .3 expression means &#x27;give me the element at index == 3&#x27;). The item is again an SFSObject whose name property we finally compare to a String.

The power of <b>Match Expression</b> doesn't end here. You can run multiple passes of matching if you need complex searches to be performed. For example you can run a first match and obtain a list of filtered Rooms and then use it to apply another expression to further refine your search, and so on and so forth. 
<para/>

The power of Match Expression doesn't end here. You can run multiple passes of matching if you need complex searches to be performed. For example you can run a first match and obtain a list of filtered Rooms and then use it to apply another expression to further refine your search, and so on and so forth.

See also
RoomProperties, UserProperties

Constructor & Destructor Documentation

◆ MatchExpression() [1/2]

Sfs2X::Entities::Match::MatchExpression::MatchExpression ( boost::shared_ptr< string >  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Constructor

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against

◆ MatchExpression() [2/2]

Sfs2X::Entities::Match::MatchExpression::MatchExpression ( string  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Constructor

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against

Member Function Documentation

◆ And() [1/2]

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::And ( boost::shared_ptr< string >  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Concatenate the current expression with a new one using the logical AND operator

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against
Returns
a new MatchExpression

◆ And() [2/2]

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::And ( string  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Concatenate the current expression with a new one using the logical AND operator

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against
Returns
a new MatchExpression

◆ Condition()

boost::shared_ptr< IMatcher > Sfs2X::Entities::Match::MatchExpression::Condition ( )

Get the condition used for matching

See also
BoolMatch, NumberMatch, StringMatch

◆ HasNext()

bool Sfs2X::Entities::Match::MatchExpression::HasNext ( )

Check if the current expression is concatenated to another one via a logic operator

Returns
A boolean
See also
LogicOperator

◆ LogicOp()

boost::shared_ptr< LogicOperator > Sfs2X::Entities::Match::MatchExpression::LogicOp ( )

Get the current logic operator, could be null if the expression has no other concatenated expressions

See also
LogicOperator

◆ Next()

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::Next ( )

Get the next expression chained to the current one.

Returns
the next expression chained to the current one.

◆ Or() [1/2]

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::Or ( boost::shared_ptr< string >  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Concatenate the current expression with a new one using the logical OR operator

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against
Returns
Pointer to a MatchExpression instance

◆ Or() [2/2]

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::Or ( string  varName,
boost::shared_ptr< IMatcher condition,
boost::shared_ptr< unsigned char >  varValue 
)

Concatenate the current expression with a new one using the logical OR operator

Parameters
varNamename of the variable/property to match
conditionthe match condition
varValuethe value to match against
Returns
Pointer to a MatchExpression instance

◆ Rewind()

boost::shared_ptr< MatchExpression > Sfs2X::Entities::Match::MatchExpression::Rewind ( )

Rewinds the cursor to the first expression in the chain and return the MatchExpression at the top of the chain of expressions

Returns
Pointer to a MatchExpression instance

◆ VarName()

boost::shared_ptr< string > Sfs2X::Entities::Match::MatchExpression::VarName ( )

Get the name of the variable or property that is being matched.

This can be the name of a User/Room variable or a property from the classes listed below.

See also
RoomProperties, UserProperties

◆ VarValue()

boost::shared_ptr< void > Sfs2X::Entities::Match::MatchExpression::VarValue ( )

The value used to test the condition in the expression