Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
URLResponse.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 "MutableDictionary.h"
27#include "String.h"
28#include "URLResponse.h"
29
30#define _Class _URLResponse
31
32#pragma mark - Object
33
37static Object *copy(const Object *self) {
38
39 URLResponse *this = (URLResponse *) self;
40
41 URLResponse *that = $(alloc(URLResponse), init);
42
43 if (this->httpHeaders) {
44 that->httpHeaders = (Dictionary *) $((Object *) this->httpHeaders, copy);
45 }
46
47 that->httpStatusCode = this->httpStatusCode;
48
49 return (Object *) that;
50}
51
55static void dealloc(Object *self) {
56
57 URLResponse *this = (URLResponse *) self;
58
59 release(this->httpHeaders);
60
61 super(Object, self, dealloc);
62}
63
64#pragma mark - URLResponse
65
71 return (URLResponse *) super(Object, self, init);
72}
73
78static void setValueForHTTPHeaderField(URLResponse *self, const char *value, const char *field) {
79
80 if (self->httpHeaders == NULL) {
82 }
83
84 String *object = str(value);
85 String *key = str(field);
86
87 $((MutableDictionary *) self->httpHeaders, setObjectForKey, object, key);
88
89 release(object);
90 release(key);
91}
92
93#pragma mark - Class lifecycle
94
98static void initialize(Class *clazz) {
99
100 ((ObjectInterface *) clazz->interface)->copy = copy;
101 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
102
103 ((URLResponseInterface *) clazz->interface)->init = init;
104 ((URLResponseInterface *) clazz->interface)->setValueForHTTPHeaderField = setValueForHTTPHeaderField;
105}
106
112 static Class *clazz;
113 static Once once;
114
115 do_once(&once, {
116 clazz = _initialize(&(const ClassDef) {
117 .name = "URLResponse",
118 .superclass = _Object(),
119 .instanceSize = sizeof(URLResponse),
120 .interfaceOffset = offsetof(URLResponse, interface),
121 .interfaceSize = sizeof(URLResponseInterface),
123 });
124 });
125
126 return clazz;
127}
128
129#undef _Class
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
#define super(type, obj, method,...)
Mutable key-value stores.
Immutable UTF-8 strings.
static void initialize(Class *clazz)
Definition: URLResponse.c:98
A protocol-agnostic abstraction for receiving resources via URLs.
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
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
Immutable key-value stores.
Definition: Dictionary.h:60
Mutable key-value stores.
void setObjectForKey(MutableDictionary *self, const ident obj, const ident key)
Sets a pair in this MutableDictionary.
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
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739
void setValueForHTTPHeaderField(URLREquest *self, const char *value, const char *field)
A protocol-agnostic abstraction for URLSessionTask responses.
Definition: URLResponse.h:42
Class * _URLResponse(void)
The URLResponse archetype.
Definition: URLResponse.c:111
Dictionary * httpHeaders
The HTTP response headers.
Definition: URLResponse.h:58
int httpStatusCode
The HTTP response status code.
Definition: URLResponse.h:63