Skip to content

Commit 4937843

Browse files
committed
Introduce Cosmopolitan Templates Library (CTL)
1 parent b003888 commit 4937843

File tree

16 files changed

+7053
-11
lines changed

16 files changed

+7053
-11
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ all: o
174174
o: o/$(MODE)
175175
o/$(MODE): \
176176
o/$(MODE)/ape \
177+
o/$(MODE)/ctl \
177178
o/$(MODE)/dsp \
178179
o/$(MODE)/net \
179180
o/$(MODE)/libc \
@@ -255,6 +256,7 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio
255256
include libc/proc/BUILD.mk # │ You can now use threads
256257
include libc/dlopen/BUILD.mk # │ You can now use processes
257258
include libc/thread/BUILD.mk # │ You can finally call malloc()
259+
include ctl/BUILD.mk #
258260
include third_party/zlib/BUILD.mk #
259261
include libc/stdio/BUILD.mk #
260262
include tool/hello/BUILD.mk #
@@ -299,6 +301,7 @@ include tool/viz/lib/BUILD.mk
299301
include tool/args/BUILD.mk
300302
include test/math/BUILD.mk
301303
include test/posix/BUILD.mk
304+
include test/ctl/BUILD.mk
302305
include test/libcxx/BUILD.mk
303306
include test/tool/args/BUILD.mk
304307
include third_party/linenoise/BUILD.mk
@@ -451,6 +454,7 @@ COSMOPOLITAN_OBJECTS = \
451454
LIBC_THREAD \
452455
LIBC_PROC \
453456
THIRD_PARTY_NSYNC_MEM \
457+
CTL \
454458
LIBC_MEM \
455459
THIRD_PARTY_DLMALLOC \
456460
LIBC_DLOPEN \
@@ -519,6 +523,7 @@ COSMOPOLITAN_H_PKGS = \
519523

520524
COSMOCC_PKGS = \
521525
$(COSMOPOLITAN_H_PKGS) \
526+
CTL \
522527
THIRD_PARTY_AARCH64 \
523528
THIRD_PARTY_LIBCXX \
524529
THIRD_PARTY_LIBCXXABI \

ctl/.clang-format

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
BasedOnStyle: Mozilla
3+
IndentWidth: 4
4+
ColumnLimit: 80
5+
---
6+
Language: Cpp
7+
AllowShortFunctionsOnASingleLine: false
8+
AlignTrailingComments: false
9+
AlignEscapedNewlines: DontAlign
10+
AlwaysBreakTemplateDeclarations: true
11+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
12+
---

ctl/BUILD.mk

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
2+
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
3+
4+
PKGS += CTL
5+
6+
CTL_ARTIFACTS += CTL_A
7+
CTL = $(CTL_A_DEPS) $(CTL_A)
8+
CTL_A = o/$(MODE)/ctl/ctl.a
9+
CTL_A_FILES := $(wildcard ctl/*)
10+
CTL_A_HDRS = $(filter %.h,$(CTL_A_FILES))
11+
CTL_A_SRCS = $(filter %.cc,$(CTL_A_FILES))
12+
CTL_A_OBJS = $(CTL_A_SRCS:%.cc=o/$(MODE)/%.o)
13+
14+
CTL_A_CHECKS = \
15+
$(CTL_A).pkg \
16+
$(CTL_A_HDRS:%=o/$(MODE)/%.okk) \
17+
18+
CTL_A_DIRECTDEPS = \
19+
LIBC_INTRIN \
20+
LIBC_MEM \
21+
LIBC_STR \
22+
23+
CTL_A_DEPS := $(call uniq,$(foreach x,$(CTL_A_DIRECTDEPS),$($(x))))
24+
25+
$(CTL_A): ctl/ \
26+
$(CTL_A).pkg \
27+
$(CTL_A_OBJS)
28+
29+
$(CTL_A).pkg: \
30+
$(CTL_A_OBJS) \
31+
$(foreach x,$(CTL_A_DIRECTDEPS),$($(x)_A).pkg)
32+
33+
$(CTL_A_OBJS): private \
34+
OVERRIDE_CXXFLAGS += \
35+
-Wframe-larger-than=4096 \
36+
-Walloca-larger-than=4096 \
37+
-ffunction-sections \
38+
-fdata-sections \
39+
40+
CTL_LIBS = $(foreach x,$(CTL_ARTIFACTS),$($(x)))
41+
CTL_SRCS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_SRCS))
42+
CTL_HDRS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_HDRS))
43+
CTL_CHECKS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_CHECKS))
44+
CTL_OBJS = $(foreach x,$(CTL_ARTIFACTS),$($(x)_OBJS))
45+
$(CTL_OBJS): $(BUILD_FILES) ctl/BUILD.mk
46+
47+
.PHONY: o/$(MODE)/ctl
48+
o/$(MODE)/ctl: $(CTL_CHECKS)

ctl/README.md

Lines changed: 4967 additions & 0 deletions
Large diffs are not rendered by default.

ctl/optional.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
2+
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
3+
#ifndef COSMOPOLITAN_CTL_OPTIONAL_H_
4+
#define COSMOPOLITAN_CTL_OPTIONAL_H_
5+
#include <__utility/forward.h>
6+
#include <__utility/move.h>
7+
#include <__utility/swap.h>
8+
9+
template<typename T>
10+
class Optional
11+
{
12+
public:
13+
using value_type = T;
14+
15+
~Optional() = default;
16+
17+
Optional() noexcept : present_(false)
18+
{
19+
}
20+
21+
Optional(const T& value) : present_(true), value_(value)
22+
{
23+
}
24+
25+
Optional(T&& value) : present_(true), value_(std::move(value))
26+
{
27+
}
28+
29+
Optional(const Optional& other) : present_(other.present_)
30+
{
31+
if (present_)
32+
new (&value_) T(other.value_);
33+
}
34+
35+
Optional(Optional&& other) noexcept : present_(other.present_)
36+
{
37+
if (present_)
38+
value_ = std::move(other.value_);
39+
}
40+
41+
Optional& operator=(const Optional& other)
42+
{
43+
if (this != &other) {
44+
present_ = other.present_;
45+
if (present_)
46+
value_ = other.value_;
47+
}
48+
return *this;
49+
}
50+
51+
Optional& operator=(Optional&& other) noexcept
52+
{
53+
if (this != &other) {
54+
present_ = other.present_;
55+
if (present_)
56+
value_ = std::move(other.value_);
57+
}
58+
return *this;
59+
}
60+
61+
T& value() &
62+
{
63+
if (!present_)
64+
__builtin_trap();
65+
return value_;
66+
}
67+
68+
const T& value() const&
69+
{
70+
if (!present_)
71+
__builtin_trap();
72+
return value_;
73+
}
74+
75+
T&& value() &&
76+
{
77+
if (!present_)
78+
__builtin_trap();
79+
return std::move(value_);
80+
}
81+
82+
explicit operator bool() const noexcept
83+
{
84+
return present_;
85+
}
86+
87+
bool has_value() const noexcept
88+
{
89+
return present_;
90+
}
91+
92+
void reset() noexcept
93+
{
94+
if (present_) {
95+
value_.~T();
96+
present_ = false;
97+
}
98+
}
99+
100+
template<typename... Args>
101+
void emplace(Args&&... args)
102+
{
103+
present_ = true;
104+
value_ = T(std::forward<Args>(args)...);
105+
}
106+
107+
void swap(Optional& other) noexcept
108+
{
109+
if (present_ && other.present_) {
110+
std::swap(value_, other.value_);
111+
} else if (present_) {
112+
other.emplace(std::move(value_));
113+
reset();
114+
} else if (other.present_) {
115+
emplace(std::move(other.value_));
116+
other.reset();
117+
}
118+
}
119+
120+
private:
121+
bool present_;
122+
T value_;
123+
};
124+
125+
#endif // COSMOPOLITAN_CTL_OPTIONAL_H_

0 commit comments

Comments
 (0)