ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Public Member Functions | Data Fields | Protected Attributes
Selector Struct Reference

Selectors are comprised of one or more SelectorSequences. More...

#include <Selector.h>

Inheritance diagram for Selector:

Public Member Functions

Class * _Selector (void)
 The Selector archetype. More...
 
Order compareTo (const Selector *self, const Selector *other)
 Compares this Selector to other, ordering by specificity. More...
 
void enumerateSelection (const Selector *self, View *view, ViewEnumerator enumerator, ident data)
 Selects from view and applies the given ViewEnumerator to all matched Views. More...
 
SelectorinitWithRule (Selector *self, const char *rule)
 Initializes this Selector with the given rule. More...
 
_Bool matchesView (const Selector *self, View *view)
 
Array * parse (const char *rules)
 Parses the null-terminated C string of Selector rules into an Array of Selectors. More...
 
Array * select (const Selector *self, View *view)
 

Data Fields

Object object
 The superclass. More...
 
char * rule
 The rule, as provided by the user. More...
 
Array * sequences
 The sequences. More...
 
int specificity
 The specificity. More...
 
Stylestyle
 The Style. More...
 

Protected Attributes

SelectorInterface * interface
 The interface. More...
 

Detailed Description

Selectors are comprised of one or more SelectorSequences.

Remarks
This implementation of Selectors is based on the W3's specification for CSS3.
See also
https://www.w3.org/TR/2011/REC-css3-selectors-20110929/

Definition at line 49 of file Selector.h.

Member Function Documentation

◆ _Selector()

Class * _Selector ( void  )

The Selector archetype.

Returns
The Selector Class.

Definition at line 398 of file Selector.c.

398 {
399 static Class *clazz;
400 static Once once;
401
402 do_once(&once, {
403 clazz = _initialize(&(const ClassDef) {
404 .name = "Selector",
405 .superclass = _Object(),
406 .instanceSize = sizeof(Selector),
407 .interfaceOffset = offsetof(Selector, interface),
408 .interfaceSize = sizeof(SelectorInterface),
410 });
411 });
412
413 return clazz;
414}
static void initialize(Class *clazz)
Definition: Selector.c:379
Selectors are comprised of one or more SelectorSequences.
Definition: Selector.h:49
SelectorInterface * interface
The interface.
Definition: Selector.h:60

◆ compareTo()

Order compareTo ( const Selector self,
const Selector other 
)

Compares this Selector to other, ordering by specificity.

Parameters
selfThe Selector.
otherThe Selector to compare.
Returns
The comparison result.

Definition at line 149 of file Selector.c.

149 {
150
151 assert(other);
152
153 if (self->specificity < other->specificity) {
154 return OrderAscending;
155 } else if (self->specificity > other->specificity) {
156 return OrderDescending;
157 }
158
159 return OrderSame;
160}
int specificity
The specificity.
Definition: Selector.h:76

◆ enumerateSelection()

void enumerateSelection ( const Selector self,
View view,
ViewEnumerator  enumerator,
ident  data 
)

Selects from view and applies the given ViewEnumerator to all matched Views.

Parameters
selfThe Selector.
viewThe View to select from.
enumeratorThe ViewEnumerator to apply to matched Views.
dataUser data.

Definition at line 166 of file Selector.c.

166 {
167
168 assert(enumerator);
169
170 Set *selection = $(self, select, view);
171 assert(selection);
172
173 Array *array = $(selection, allObjects);
174 assert(array);
175
176 for (size_t i = 0; i < array->count; i++) {
177 enumerator($(array, objectAtIndex, i), data);
178 }
179
180 release(array);
181 release(selection);
182}
Array * select(const Selector *self, View *view)

◆ initWithRule()

Selector * initWithRule ( Selector self,
const char *  rule 
)

Initializes this Selector with the given rule.

Parameters
selfThe Selector.
ruleThe rule.
Returns
The initialized Selector, or NULL on error.

Definition at line 188 of file Selector.c.

188 {
189
190 self = (Selector *) super(Object, self, init);
191 if (self) {
192
193 self->rule = strtrim(rule);
194 assert(self->rule);
195
196 self->sequences = $$(SelectorSequence, parse, self->rule);
197 assert(self->sequences->count);
198
199 self->specificity = specificity(self);
200 }
201
202 return self;
203}
static View * init(View *self)
Definition: Box.c:67
char * rule
The rule, as provided by the user.
Definition: Selector.h:70
Array * sequences
The sequences.
Definition: Selector.h:65
Array * parse(const char *rules)
Parses the null-terminated C string of Selector rules into an Array of Selectors.
Definition: Selector.c:274
SelectorSequences are comprised of one or more SimpleSelectors.

◆ matchesView()

_Bool matchesView ( const Selector self,
View view 
)
Parameters
selfThe Selector.
viewThe View.
Returns
True if this Selector matches the given View, false otherwise.

◆ parse()

Array * parse ( const char *  rules)

Parses the null-terminated C string of Selector rules into an Array of Selectors.

Parameters
rulesA null-terminated C string of Selector rules.
Returns
An Array of Selectors.

Definition at line 274 of file Selector.c.

274 {
275
276 MutableArray *selectors = $$(MutableArray, arrayWithCapacity, 4);
277 assert(selectors);
278
279 if (rules) {
280
281 const char *c = rules;
282 while (*c) {
283 const size_t size = strcspn(c, ",");
284 if (size) {
285 char *rule = calloc(1, size + 1);
286 assert(rule);
287
288 strncpy(rule, c, size);
289
290 Selector *selector = $(alloc(Selector), initWithRule, rule);
291 assert(selector);
292
293 $(selectors, addObject, selector);
294
295 release(selector);
296 free(rule);
297 }
298 c += size;
299 c += strspn(c, ", \t\n");
300 }
301 }
302
303 return (Array *) selectors;
304}
static SDL_Size size(const Image *self)
Definition: Image.c:181
Selector * initWithRule(Selector *self, const char *rule)
Initializes this Selector with the given rule.
Definition: Selector.c:188

◆ select()

Set * select ( const Selector self,
View view 
)
Parameters
selfThe Selector.
viewThe View.
Returns
The Set of view's descendants that match this Selector.

Field Documentation

◆ interface

SelectorInterface* Selector::interface
protected

The interface.

Definition at line 60 of file Selector.h.

◆ object

Object Selector::object

The superclass.

Definition at line 54 of file Selector.h.

◆ rule

char* Selector::rule

The rule, as provided by the user.

Definition at line 70 of file Selector.h.

◆ sequences

Array* Selector::sequences

The sequences.

Definition at line 65 of file Selector.h.

◆ specificity

int Selector::specificity

The specificity.

See also
https://www.w3.org/TR/2011/REC-css3-selectors-20110929/#specificity

Definition at line 76 of file Selector.h.

◆ style

Style* Selector::style

The Style.

Remarks
This is a weak reference to the Style if this Selector belongs to a Stylesheet.

Definition at line 82 of file Selector.h.


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