Skip to content

Commit ef98efe

Browse files
committed
2 parents 4c72117 + fe4fbfe commit ef98efe

File tree

9 files changed

+449
-1
lines changed

9 files changed

+449
-1
lines changed

web/mainmenu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class MainMenu {
3737
this._Menu.addDataItem("/윈도우즈 프로그래밍/WinUI3", "../page-WinUI3/page.html");
3838
this._Menu.addDataItem("/윈도우즈 프로그래밍/WinAPI", "../page-WinAPI/page.html");
3939

40-
this._Menu.addDataItem("/C++/문서", "../page-cpp-doc/page.html");
40+
this._Menu.addDataItem("/C++" , "../page-cpp-doc/page.html");
41+
this._Menu.addDataItem("/C++/문서" , "../page-cpp-doc/page.html");
42+
this._Menu.addDataItem("/C++/코드 조각" , "../page-cpp-code-snippet/page.html");
4143
}
4244

4345
initializeMenu() {
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# C++ 코드 조각
2+
3+
4+
## coroutine
5+
6+
### 설명
7+
8+
* https://cppreference.com/w/cpp/coroutine.html
9+
10+
11+
### 예제
12+
13+
* 코드
14+
15+
```C++
16+
17+
#include <coroutine>
18+
#include <iostream>
19+
#include <optional>
20+
21+
template<std::movable T>
22+
class Generator
23+
{
24+
public:
25+
struct promise_type
26+
{
27+
Generator<T> get_return_object()
28+
{
29+
return Generator{Handle::from_promise(*this)};
30+
}
31+
static std::suspend_always initial_suspend() noexcept
32+
{
33+
return {};
34+
}
35+
static std::suspend_always final_suspend() noexcept
36+
{
37+
return {};
38+
}
39+
std::suspend_always yield_value(T value) noexcept
40+
{
41+
current_value = std::move(value);
42+
return {};
43+
}
44+
// Disallow co_await in generator coroutines.
45+
void await_transform() = delete;
46+
[[noreturn]]
47+
static void unhandled_exception() { throw; }
48+
49+
std::optional<T> current_value;
50+
};
51+
52+
using Handle = std::coroutine_handle<promise_type>;
53+
54+
explicit Generator(const Handle coroutine) :
55+
m_coroutine{coroutine}
56+
{}
57+
58+
Generator() = default;
59+
~Generator()
60+
{
61+
if (m_coroutine)
62+
m_coroutine.destroy();
63+
}
64+
65+
Generator(const Generator&) = delete;
66+
Generator& operator=(const Generator&) = delete;
67+
68+
Generator(Generator&& other) noexcept :
69+
m_coroutine{other.m_coroutine}
70+
{
71+
other.m_coroutine = {};
72+
}
73+
Generator& operator=(Generator&& other) noexcept
74+
{
75+
if (this != &other)
76+
{
77+
if (m_coroutine)
78+
m_coroutine.destroy();
79+
m_coroutine = other.m_coroutine;
80+
other.m_coroutine = {};
81+
}
82+
return *this;
83+
}
84+
85+
// Range-based for loop support.
86+
class Iter
87+
{
88+
public:
89+
void operator++()
90+
{
91+
m_coroutine.resume();
92+
}
93+
const T& operator*() const
94+
{
95+
return *m_coroutine.promise().current_value;
96+
}
97+
bool operator==(std::default_sentinel_t) const
98+
{
99+
return !m_coroutine || m_coroutine.done();
100+
}
101+
102+
explicit Iter(const Handle coroutine) :
103+
m_coroutine{coroutine}
104+
{}
105+
106+
private:
107+
Handle m_coroutine;
108+
};
109+
110+
Iter begin()
111+
{
112+
if (m_coroutine)
113+
m_coroutine.resume();
114+
return Iter{m_coroutine};
115+
}
116+
117+
std::default_sentinel_t end() { return {}; }
118+
119+
private:
120+
Handle m_coroutine;
121+
};
122+
123+
template<std::integral T>
124+
Generator<T> range(T first, const T last)
125+
{
126+
while (first < last)
127+
co_yield first++;
128+
}
129+
130+
int main()
131+
{
132+
for (const char i : range(65, 91))
133+
std::cout << i << ' ';
134+
std::cout << '\n';
135+
}
136+
137+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# C++ 코드 조각
2+
3+
4+
## std::format
5+
6+
### 설명
7+
8+
* https://cppreference.com/w/cpp/utility/format/formatter.html
9+
10+
11+
### 예제
12+
13+
* 코드
14+
15+
```C++
16+
17+
#include <algorithm>
18+
#include <format>
19+
#include <iomanip>
20+
#include <iostream>
21+
#include <sstream>
22+
#include <string_view>
23+
24+
struct QuotableString : std::string_view
25+
{};
26+
27+
template<>
28+
struct std::formatter<QuotableString, char>
29+
{
30+
bool quoted = false;
31+
32+
template<class ParseContext>
33+
constexpr ParseContext::iterator parse(ParseContext& ctx)
34+
{
35+
auto it = ctx.begin();
36+
if (it == ctx.end())
37+
return it;
38+
39+
if (*it == '#')
40+
{
41+
quoted = true;
42+
++it;
43+
}
44+
if (it != ctx.end() && *it != '}')
45+
throw std::format_error("Invalid format args for QuotableString.");
46+
47+
return it;
48+
}
49+
50+
template<class FmtContext>
51+
FmtContext::iterator format(QuotableString s, FmtContext& ctx) const
52+
{
53+
std::ostringstream out;
54+
if (quoted)
55+
out << std::quoted(s);
56+
else
57+
out << s;
58+
59+
return std::ranges::copy(std::move(out).str(), ctx.out()).out;
60+
}
61+
};
62+
63+
int main()
64+
{
65+
QuotableString a("be"), a2(R"( " be " )");
66+
QuotableString b("a question");
67+
std::cout << std::format("To {0} or not to {0}, that is {1}.\n", a, b);
68+
std::cout << std::format("To {0:} or not to {0:}, that is {1:}.\n", a, b);
69+
std::cout << std::format("To {0:#} or not to {0:#}, that is {1:#}.\n", a2, b);
70+
}
71+
72+
```
73+
74+
75+
* 출력
76+
77+
```
78+
To be or not to be, that is a question.
79+
To be or not to be, that is a question.
80+
To " \" be \" " or not to " \" be \" ", that is "a question".
81+
```
82+
83+
84+
85+

web/page-cpp-code-snippet/page.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@charset "utf-8";
2+
3+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
5+
<meta charset="utf-8" />
6+
<title>code1009</title>
7+
<link rel="stylesheet" type="text/css" href="../core.css" />
8+
<link rel="stylesheet" type="text/css" href="../menu.css" />
9+
<link rel="stylesheet" type="text/css" href="./page.css" />
10+
<script type="text/javascript" src="../core.js"></script>
11+
<script type="text/javascript" src="../menu.js"></script>
12+
<script type="text/javascript" src="../mainmenu.js"></script>
13+
<script type="text/javascript" src="../markdown.js"></script>
14+
<script type="text/javascript" src="./submenu.js"></script>
15+
<script type="text/javascript" src="./page.js"></script>
16+
17+
</head>
18+
<body>
19+
20+
<div id="page-container">
21+
22+
<div id="page-information"></div>
23+
24+
<div id="page-resizer"></div>
25+
26+
<div id="page-contents">
27+
<div id="subMenu" class="menu"></div>
28+
<br />
29+
<div id="page-markdown-view">
30+
</div>
31+
</div>
32+
33+
</div>
34+
35+
<script>
36+
function getMarkDownFileURL() {
37+
const params = new URLSearchParams(window.location.search);
38+
const page = params.get("page");
39+
return page ? `./${page}.md` : "./page1.md";
40+
}
41+
42+
function initializeMarkdwonView() {
43+
const view = document.getElementById("page-markdown-view");
44+
if (!view) {
45+
return;
46+
}
47+
48+
const markdownFileURL = getMarkDownFileURL();
49+
50+
loadMarkDownScript(
51+
function () {
52+
renderMarkdwon("page-markdown-view", markdownFileURL);
53+
}
54+
);
55+
}
56+
57+
initializeMarkdwonView();
58+
</script>
59+
</body>
60+
</html>

web/page-cpp-code-snippet/page.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/////////////////////////////////////////////////////////////////////////////
2+
//===========================================================================
3+
"use strict";
4+
5+
6+
7+
8+
9+
/////////////////////////////////////////////////////////////////////////////
10+
//===========================================================================
11+
class Page {
12+
13+
#Context = null;
14+
15+
constructor() {
16+
this.#Context = null;
17+
}
18+
}
19+
20+
21+
22+
23+
24+
/////////////////////////////////////////////////////////////////////////////
25+
//===========================================================================
26+
var _Page = null;
27+
28+
29+
30+
31+
32+
/////////////////////////////////////////////////////////////////////////////
33+
//===========================================================================
34+
function initializePage() {
35+
_Page = new Page();
36+
}
37+
38+
39+
40+
41+
42+
/////////////////////////////////////////////////////////////////////////////
43+
//===========================================================================
44+
window.onload = function () {
45+
initializeCore();
46+
initializePage();
47+
48+
initializePageInformation();
49+
initializeMainMenu();
50+
51+
initializeSubMenu();
52+
}
53+
54+

web/page-cpp-code-snippet/page1.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# C++ 코드 조각
2+
3+
4+
## Hello World!
5+
6+
### 설명
7+
8+
* [https://cppreference.com/)
9+
10+
11+
### 예제
12+
13+
* 코드
14+
15+
```C++
16+
17+
#include <iostream>
18+
using namespace std;
19+
20+
int main() {
21+
cout << "Hello World!" << endl;
22+
return 0;
23+
}
24+
25+
```
26+
27+
28+
* 출력
29+
30+
```
31+
Hello World!
32+
33+
```
34+
35+
36+
37+

0 commit comments

Comments
 (0)