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

+4
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

+163
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

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+1
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

+4
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

+8
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

+2
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

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

type-erasure/rpg-game/CMakeLists.txt

+6
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

+66
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+
}

type-erasure/rpg-game/Object.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
#include <memory>
3+
#include <typeinfo>
4+
5+
#pragma mark - 1
6+
7+
//*
8+
9+
#include <string>
10+
11+
struct ObjectConcept;
12+
13+
// Object is a simple "passthrough" object that becomes transparent
14+
class Object
15+
{
16+
public:
17+
template<typename T>
18+
Object(T const&);
19+
20+
std::string name() const;
21+
bool has_attack_concept() const;
22+
23+
private:
24+
std::shared_ptr<ObjectConcept> object_;
25+
};
26+
27+
//*/
28+
29+
#pragma mark - 2
30+
31+
/*
32+
33+
#include "ObjectConcept.h"
34+
35+
// Object is a simple "passthrough" object that becomes transparent
36+
class Object
37+
: public ObjectConcept
38+
{
39+
public:
40+
template<typename T>
41+
Object(T const&);
42+
43+
std::string name() const override;
44+
bool has_attack_concept() const override;
45+
46+
private:
47+
std::shared_ptr<ObjectConcept> object_;
48+
};
49+
50+
//*/

type-erasure/rpg-game/ObjectConcept.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <string>
3+
4+
// concept
5+
// 1. An abstract and general idea; an abstraction.
6+
// 2. (generic programming) A description of supported operations on a type, including their syntax and semantics.
7+
8+
struct ObjectConcept
9+
{
10+
virtual ~ObjectConcept() = default;
11+
virtual bool has_attack_concept() const = 0;
12+
virtual std::string name() const = 0;
13+
};

type-erasure/rpg-game/REFERENCES.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- [C++ type erasure](http://www.cplusplus.com/articles/oz18T05o/)
2+
- [Makefile Tutorial](https://courses.engr.illinois.edu/cs225/fa2018/resources/maketutorial/)
3+
- Wiki
4+
- [concept](https://en.wiktionary.org/wiki/concept)
5+
- ["All problems in computer science can be solved by another level of indirection" (David Wheeler)](https://en.wikipedia.org/wiki/David_Wheeler_%28computer_scientist%29#Quotes)
6+
- [Fundamental theorem of software engineering](https://en.wikipedia.org/wiki/Fundamental_theorem_of_software_engineering)

type-erasure/rpg-game/items/Armor.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct Armor
4+
{
5+
bool can_attack() const { return false; } // Cannot attack with armor...
6+
};
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct FireScroll
4+
{
5+
bool can_attack() const { return true; }
6+
};

type-erasure/rpg-game/items/Helmet.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct Helmet
4+
{
5+
bool can_attack() const { return false; } // Cannot attack with helmet...
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct PoisonPotion
4+
{
5+
bool can_attack() const { return true; }
6+
};

type-erasure/rpg-game/items/Potion.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct Potion
4+
{
5+
bool can_attack() const { return false; }
6+
};

0 commit comments

Comments
 (0)