Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Boole.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include "Boole.h"
27#include "String.h"
28
29#define _Class _Boole
30
31#pragma mark - Object
32
36static Object *copy(const Object *self) {
37
38 return (Object *) self;
39}
40
44static String *description(const Object *self) {
45
46 const Boole *this = (Boole *) self;
47
48 return $(alloc(String), initWithCharacters, this->value ? "true" : "false");
49}
50
51#pragma mark - Boole
52
53static Boole *_False;
54
59static Boole *False(void) {
60
61 static Once once;
62
63 do_once(&once, {
64 _False = (Boole *) $((Object *) alloc(Boole), init);
65 _False->value = false;
66 });
67
68 return _False;
69}
70
71static Boole *_True;
72
77static Boole *True(void) {
78
79 static Once once;
80
81 do_once(&once, {
82 _True = (Boole *) $((Object *) alloc(Boole), init);
83 _True->value = true;
84 });
85
86 return _True;
87}
88
93static Boole *valueof(_Bool value) {
94 return value ? $$(Boole, True) : $$(Boole, False);
95}
96
97#pragma mark - Class lifecycle
98
102static void initialize(Class *clazz) {
103
104 ((ObjectInterface *) clazz->interface)->copy = copy;
105 ((ObjectInterface *) clazz->interface)->description = description;
106
107 ((BooleInterface *) clazz->interface)->False = False;
108 ((BooleInterface *) clazz->interface)->True = True;
109 ((BooleInterface *) clazz->interface)->valueof = valueof;
110}
111
115static void destroy(Class *clazz) {
116
118 release(_True);
119}
120
125Class *_Boole(void) {
126 static Class *clazz;
127 static Once once;
128
129 do_once(&once, {
130 clazz = _initialize(&(const ClassDef) {
131 .name = "Boole",
132 .superclass = _Object(),
133 .instanceSize = sizeof(Boole),
134 .interfaceOffset = offsetof(Boole, interface),
135 .interfaceSize = sizeof(BooleInterface),
137 .destroy = destroy,
138 });
139 });
140
141 return clazz;
142}
143
144#undef _Class
static void destroy(Class *clazz)
Definition: Boole.c:115
static Boole * _True
Definition: Boole.c:71
static Boole * _False
Definition: Boole.c:53
static void initialize(Class *clazz)
Definition: Boole.c:102
A wrapper for placing boolean primitives into collections, etc.
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Immutable UTF-8 strings.
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
A wrapper for placing boolean primitives into collections, etc.
Definition: Boole.h:41
_Bool value
The backing _Bool.
Definition: Boole.h:57
Boole * valueof(_Bool value)
Definition: Boole.c:93
Class * _Boole(void)
The Boole archetype.
Definition: Boole.c:125
Boole * False(void)
Definition: Boole.c:59
Boole * True(void)
Definition: Boole.c:77
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
String * description(const Object *self)
Definition: Array.c:66
Immutable UTF-8 strings.
Definition: String.h:69
String * initWithCharacters(String *self, const char *chars)
Initializes this String by copying chars.
Definition: String.c:310