Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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 destroy(Class *clazz) {
103
105 release(_True);
106}
107
111static void initialize(Class *clazz) {
112
113 ((ObjectInterface *) clazz->interface)->copy = copy;
114 ((ObjectInterface *) clazz->interface)->description = description;
115
116 ((BooleInterface *) clazz->interface)->False = False;
117 ((BooleInterface *) clazz->interface)->True = True;
118 ((BooleInterface *) clazz->interface)->valueof = valueof;
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 Array * init(Array *self)
Definition Array.c:420
static void destroy(Class *clazz)
Definition Boole.c:102
static Boole * _True
Definition Boole.c:71
static Boole * True(void)
Definition Boole.c:77
static Boole * _False
Definition Boole.c:53
Class * _Boole(void)
Definition Boole.c:125
static String * description(const Object *self)
Definition Boole.c:44
static Boole * valueof(bool value)
Definition Boole.c:93
static Object * copy(const Object *self)
Definition Boole.c:36
static void initialize(Class *clazz)
Definition Boole.c:111
static Boole * False(void)
Definition Boole.c:59
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:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
Class * _Object(void)
Definition Object.c:136
static String * initWithCharacters(String *self, const char *chars)
Definition String.c:638
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
Boole * valueof(bool value)
Definition Boole.c:93
bool value
The backing bool.
Definition Boole.h:57
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
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
String * description(const Object *self)
Definition Array.c:115
UTF-8 strings.
Definition String.h:69