SmartFoxServer 3 C++ Client API 3.1.0-beta
Client API for SmartFoxServer 3
Loading...
Searching...
No Matches
sfs3::entities::match::MatchExpression Class Reference

The MatchExpression class represents a matching expression used to compare custom variables or predefined properties when searching for users or Rooms. More...

#include <MatchExpression.h>

Classes

struct  Condition
 A single condition of a matching expression. More...

Public Types

using Value = std::variant<bool, double, std::string>
 The value a condition compares a variable or property against.

Public Member Functions

 MatchExpression (std::string varName, BoolMatch condition, bool value)
 Creates a new MatchExpression instance checking a boolean condition.
 MatchExpression (std::string varName, NumberMatch condition, double value)
 Creates a new MatchExpression instance checking a numeric condition.
 MatchExpression (std::string varName, StringMatch condition, std::string value)
 Creates a new MatchExpression instance checking a string condition.
MatchExpressionAnd (std::string varName, BoolMatch condition, bool value) &
 Concatenates a new boolean condition to this expression using the logical AND operator.
MatchExpressionAnd (std::string varName, NumberMatch condition, double value) &
 Concatenates a new numeric condition to this expression using the logical AND operator.
MatchExpressionAnd (std::string varName, StringMatch condition, std::string value) &
 Concatenates a new string condition to this expression using the logical AND operator.
MatchExpressionOr (std::string varName, BoolMatch condition, bool value) &
 Concatenates a new boolean condition to this expression using the logical OR operator.
MatchExpressionOr (std::string varName, NumberMatch condition, double value) &
 Concatenates a new numeric condition to this expression using the logical OR operator.
MatchExpressionOr (std::string varName, StringMatch condition, std::string value) &
 Concatenates a new string condition to this expression using the logical OR operator.
MatchExpression && And (std::string varName, BoolMatch condition, bool value) &&
MatchExpression && And (std::string varName, NumberMatch condition, double value) &&
MatchExpression && And (std::string varName, StringMatch condition, std::string value) &&
MatchExpression && Or (std::string varName, BoolMatch condition, bool value) &&
MatchExpression && Or (std::string varName, NumberMatch condition, double value) &&
MatchExpression && Or (std::string varName, StringMatch condition, std::string value) &&
const std::vector< Condition > & conditions () const
 Returns the conditions making up this expression, in evaluation order.
std::string toString () const
 Returns a string representation of the matching expression, e.g.

Detailed Description

The MatchExpression class represents a matching expression used to compare custom variables or predefined properties when searching for users or Rooms.

Matching expressions are built like "if" statements 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: an expression can compare predefined properties of the Room and user entities (see the RoomProperties and UserProperties constants), but also custom Room or User Variables.

Additionally:

  • any number of conditions can be linked together with the logical AND and OR operators by chaining calls to And() and Or();
  • searching through nested data structures such as SFSObject and SFSArray can be done via a very simple dot-syntax.

Example
A simple matching expression made of two concatenated conditions: it compares the custom "rank" and "country" User Variables to the passed values. This expression could be used during the creation of a Game Room, to filter the users that the server should take into account when sending the invitations to join the game (only italian users with a ranking greater than 5 - whatever this number means to our game):

auto exp = MatchExpression("rank", NumberMatch::GREATER_THAN, 5).And("country", StringMatch::EQUALS, "Italy");

Example
An expression made of three concatenated conditions which compare two predefined Room properties and the custom "isGameStarted" Room Variable to the passed values; this could be used to retrieve all the Game Rooms still waiting for players to join them:

auto exp = MatchExpression(RoomProperties::IS_GAME, BoolMatch::EQUALS, true)
               .And(RoomProperties::HAS_FREE_PLAYER_SLOTS, BoolMatch::EQUALS, true)
               .And("isGameStarted", BoolMatch::EQUALS, false);

Example
An expression comparing a nested property in a complex data structure; an SFSObject called "avatarData" (could be a User Variable for example) contains the "shield" object (a nested SFSObject) which in turn contains, among others, the "inUse" property, which could be used to retrieve all users whose avatars are currently equipped with a shield:

auto exp = MatchExpression("avatarData.shield.inUse", BoolMatch::EQUALS, true);

Example
Similar to the previous one, but involving an SFSArray. The "avatarData" object contains the "weapons" SFSArray, from which the expression retrieves the fourth element (that .3 means "give me the element at index == 3") that we know being the weapon the user avatar has in his right hand. Again, this element is an SFSObject containing, among the others, the "name" property, which can be compared to the passed string. This example could be used to retrieve all users whose avatars have the Narsil sword in the right hand:

auto exp = MatchExpression("avatarData.weapons.3.name", StringMatch::EQUALS, "Narsil");

NOTE on evaluation order: the server evaluates the conditions strictly left to right, with no operator precedence: each condition is combined with the result accumulated so far. This means that A OR B AND C is evaluated as (A OR B) AND C, and not as A OR (B AND C) as the usual precedence rules would suggest.

NOTE on chaining: And() and Or() append to the expression they are called on and hand it back, so a chain always yields the whole expression, not its last condition. Assign the result of a chain started on a temporary to a value (auto exp = ...), never to a reference: the temporary is destroyed at the end of the full expression and binding a reference to it would leave that reference dangling.

See also
RoomProperties
UserProperties
BoolMatch
NumberMatch
StringMatch

Member Typedef Documentation

◆ Value

using sfs3::entities::match::MatchExpression::Value = std::variant<bool, double, std::string>

The value a condition compares a variable or property against.

The three alternatives correspond one-to-one to the three matcher kinds: a BoolMatch condition carries a bool, a NumberMatch condition a double (every numeric type is sent to the server as a double) and a StringMatch condition a std::string.

Constructor & Destructor Documentation

◆ MatchExpression() [1/3]

sfs3::entities::match::MatchExpression::MatchExpression ( std::string varName,
BoolMatch condition,
bool value )

Creates a new MatchExpression instance checking a boolean condition.

Parameters
varNameName of the variable or property to match.
conditionThe matching condition.
valueThe value to compare against the variable or property during the matching.

◆ MatchExpression() [2/3]

sfs3::entities::match::MatchExpression::MatchExpression ( std::string varName,
NumberMatch condition,
double value )

Creates a new MatchExpression instance checking a numeric condition.

Parameters
varNameName of the variable or property to match.
conditionThe matching condition.
valueThe value to compare against the variable or property during the matching.

◆ MatchExpression() [3/3]

sfs3::entities::match::MatchExpression::MatchExpression ( std::string varName,
StringMatch condition,
std::string value )

Creates a new MatchExpression instance checking a string condition.

Parameters
varNameName of the variable or property to match.
conditionThe matching condition.
valueThe value to compare against the variable or property during the matching.

Member Function Documentation

◆ And() [1/6]

MatchExpression & sfs3::entities::match::MatchExpression::And ( std::string varName,
BoolMatch condition,
bool value ) &

Concatenates a new boolean condition to this expression using the logical AND operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ And() [2/6]

MatchExpression & sfs3::entities::match::MatchExpression::And ( std::string varName,
NumberMatch condition,
double value ) &

Concatenates a new numeric condition to this expression using the logical AND operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ And() [3/6]

MatchExpression & sfs3::entities::match::MatchExpression::And ( std::string varName,
StringMatch condition,
std::string value ) &

Concatenates a new string condition to this expression using the logical AND operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ Or() [1/6]

MatchExpression & sfs3::entities::match::MatchExpression::Or ( std::string varName,
BoolMatch condition,
bool value ) &

Concatenates a new boolean condition to this expression using the logical OR operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ Or() [2/6]

MatchExpression & sfs3::entities::match::MatchExpression::Or ( std::string varName,
NumberMatch condition,
double value ) &

Concatenates a new numeric condition to this expression using the logical OR operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ Or() [3/6]

MatchExpression & sfs3::entities::match::MatchExpression::Or ( std::string varName,
StringMatch condition,
std::string value ) &

Concatenates a new string condition to this expression using the logical OR operator.

Parameters
varNameThe name of the additional variable or property to match.
conditionThe additional matching condition.
valueThe value to compare against the additional variable or property during the matching.
Returns
This expression, so that further conditions can be chained.

◆ And() [4/6]

MatchExpression && sfs3::entities::match::MatchExpression::And ( std::string varName,
BoolMatch condition,
bool value ) &&
inline
See also
And(std::string, BoolMatch, bool)

◆ And() [5/6]

MatchExpression && sfs3::entities::match::MatchExpression::And ( std::string varName,
NumberMatch condition,
double value ) &&
inline
See also
And(std::string, NumberMatch, double)

◆ And() [6/6]

MatchExpression && sfs3::entities::match::MatchExpression::And ( std::string varName,
StringMatch condition,
std::string value ) &&
inline
See also
And(std::string, StringMatch, std::string)

◆ Or() [4/6]

MatchExpression && sfs3::entities::match::MatchExpression::Or ( std::string varName,
BoolMatch condition,
bool value ) &&
inline
See also
Or(std::string, BoolMatch, bool)

◆ Or() [5/6]

MatchExpression && sfs3::entities::match::MatchExpression::Or ( std::string varName,
NumberMatch condition,
double value ) &&
inline
See also
Or(std::string, NumberMatch, double)

◆ Or() [6/6]

MatchExpression && sfs3::entities::match::MatchExpression::Or ( std::string varName,
StringMatch condition,
std::string value ) &&
inline
See also
Or(std::string, StringMatch, std::string)

◆ conditions()

const std::vector< Condition > & sfs3::entities::match::MatchExpression::conditions ( ) const
inline

Returns the conditions making up this expression, in evaluation order.

The list always holds at least one condition — the one passed to the constructor — and only the first of them has an empty logicOp.

◆ toString()

std::string sfs3::entities::match::MatchExpression::toString ( ) const

Returns a string representation of the matching expression, e.g.

(${ISG} == true) AND (isGameStarted == false).


The documentation for this class was generated from the following files:
  • entities/match/MatchExpression.h
  • entities/match/MatchExpression.cpp