Content-Length: 811191 | pFad | http://github.com/gusenov/examples-cpp/commit/6228603c850431808b208ed841d175ea8cc4a536

01 Type erasure на примере RPG-игры. · gusenov/examples-cpp@6228603 · GitHub
Skip to content

Commit 6228603

Browse files
committed
Type erasure на примере RPG-игры.
1 parent 5edc9c2 commit 6228603

22 files changed

+424
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208

209209
- [Сэндвич](syncfusion/cpp_succinctly/PimplSample)
210210

211+
## [Type Erasure](type-erasure)
212+
213+
- [RPG game](type-erasure/rpg-game)
214+
211215

212216
# Стандартная библиотека
213217

type-erasure/rpg-game/.gitignore

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# ---------------------------------------------------------------------------
2+
# https://github.com/github/gitignore/blob/master/C++.gitignore
3+
4+
# Prerequisites
5+
*.d
6+
7+
# Compiled Object files
8+
*.slo
9+
*.lo
10+
*.o
11+
*.obj
12+
13+
# Precompiled Headers
14+
*.gch
15+
*.pch
16+
17+
# Compiled Dynamic libraries
18+
*.so
19+
*.dylib
20+
*.dll
21+
22+
# Fortran module files
23+
*.mod
24+
*.smod
25+
26+
# Compiled Static libraries
27+
*.lai
28+
*.la
29+
*.a
30+
*.lib
31+
32+
# Executables
33+
*.exe
34+
*.out
35+
*.app
36+
37+
# ---------------------------------------------------------------------------
38+
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore
39+
40+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
41+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
42+
43+
# User-specific stuff
44+
.idea/**/workspace.xml
45+
.idea/**/tasks.xml
46+
.idea/**/usage.statistics.xml
47+
.idea/**/dictionaries
48+
.idea/**/shelf
49+
50+
# AWS User-specific
51+
.idea/**/aws.xml
52+
53+
# Generated files
54+
.idea/**/contentModel.xml
55+
56+
# Sensitive or high-churn files
57+
.idea/**/dataSources/
58+
.idea/**/dataSources.ids
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
.idea/**/dbnavigator.xml
64+
65+
# Gradle
66+
.idea/**/gradle.xml
67+
.idea/**/libraries
68+
69+
# Gradle and Maven with auto-import
70+
# When using Gradle or Maven with auto-import, you should exclude module files,
71+
# since they will be recreated, and may cause churn. Uncomment if using
72+
# auto-import.
73+
# .idea/artifacts
74+
# .idea/compiler.xml
75+
# .idea/jarRepositories.xml
76+
# .idea/modules.xml
77+
# .idea/*.iml
78+
# .idea/modules
79+
# *.iml
80+
# *.ipr
81+
82+
# CMake
83+
cmake-build-*/
84+
85+
# Mongo Explorer plugin
86+
.idea/**/mongoSettings.xml
87+
88+
# File-based project format
89+
*.iws
90+
91+
# IntelliJ
92+
out/
93+
94+
# mpeltonen/sbt-idea plugin
95+
.idea_modules/
96+
97+
# JIRA plugin
98+
atlassian-ide-plugin.xml
99+
100+
# Cursive Clojure plugin
101+
.idea/replstate.xml
102+
103+
# SonarLint plugin
104+
.idea/sonarlint/
105+
106+
# Crashlytics plugin (for Android Studio and IntelliJ)
107+
com_crashlytics_export_strings.xml
108+
crashlytics.properties
109+
crashlytics-build.properties
110+
fabric.properties
111+
112+
# Editor-based Rest Client
113+
.idea/httpRequests
114+
115+
# Android studio 3.1+ serialized cache file
116+
.idea/caches/build_file_checksums.ser
117+
118+
# ---------------------------------------------------------------------------
119+
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
120+
121+
# General
122+
.DS_Store
123+
.AppleDouble
124+
.LSOverride
125+
126+
# Icon must end with two \r
127+
Icon
128+
129+
# Thumbnails
130+
._*
131+
132+
# Files that might appear in the root of a volume
133+
.DocumentRevisions-V100
134+
.fseventsd
135+
.Spotlight-V100
136+
.TemporaryItems
137+
.Trashes
138+
.VolumeIcon.icns
139+
.com.apple.timemachine.donotpresent
140+
141+
# Directories potentially created on remote AFP share
142+
.AppleDB
143+
.AppleDesktop
144+
Network Trash Folder
145+
Temporary Items
146+
.apdisk
147+
148+
# ---------------------------------------------------------------------------
149+
# https://github.com/github/gitignore/blob/master/CMake.gitignore
150+
151+
CMakeLists.txt.user
152+
CMakeCache.txt
153+
CMakeFiles
154+
CMakeScripts
155+
Testing
156+
Makefile
157+
cmake_install.cmake
158+
install_manifest.txt
159+
compile_commands.json
160+
CTestTestfile.cmake
161+
_deps
162+
163+
# ---------------------------------------------------------------------------

type-erasure/rpg-game/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/.idea/rpg-game.iml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
project(RPG_game)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(${PROJECT_NAME} main.cc Object.cc)

type-erasure/rpg-game/Object.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "Object.h"
2+
3+
#include "items/Weapon.h"
4+
#include "items/Armor.h"
5+
#include "items/Potion.h"
6+
#include "items/Scroll.h"
7+
#include "items/FireScroll.h"
8+
#include "items/PoisonPotion.h"
9+
10+
#include "ObjectConcept.h"
11+
12+
/**
13+
* just done the inheritance thing. How is this better?
14+
* It is better not because it affords you more functionality than the inheritance approach,
15+
* but because it does not tighly couple Weapons and Armors etc through a common base class.
16+
* It gives me the power of retaining type as templates do.
17+
*/
18+
template<typename T>
19+
struct ObjectModel
20+
: ObjectConcept
21+
{
22+
ObjectModel(T const& t)
23+
: object(t)
24+
{}
25+
26+
~ObjectModel() override = default;
27+
28+
bool has_attack_concept() const override
29+
{
30+
return object.can_attack();
31+
}
32+
33+
std::string name() const override
34+
{
35+
return typeid(object).name();
36+
}
37+
38+
private:
39+
T object;
40+
};
41+
42+
#pragma mark - Object
43+
44+
45+
template<typename T>
46+
Object::Object(T const& obj )
47+
: object_(new ObjectModel<T>(obj))
48+
{}
49+
50+
template Object::Object(Weapon const&);
51+
template Object::Object(Armor const&);
52+
template Object::Object(Potion const&);
53+
template Object::Object(Scroll const&);
54+
template Object::Object(FireScroll const&);
55+
template Object::Object(PoisonPotion const&);
56+
57+
58+
std::string Object::name() const
59+
{
60+
return object_->name();
61+
}
62+
63+
bool Object::has_attack_concept() const
64+
{
65+
return object_->has_attack_concept();
66+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/gusenov/examples-cpp/commit/6228603c850431808b208ed841d175ea8cc4a536

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy