aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--contributing.md5
-rw-r--r--src/crepe/Asset.h41
-rw-r--r--src/crepe/api/Asset.h10
-rw-r--r--src/crepe/util/OptionalRef.h37
-rw-r--r--src/example/audio_internal.cpp12
5 files changed, 53 insertions, 52 deletions
diff --git a/contributing.md b/contributing.md
index 5b0c79d..e4f075f 100644
--- a/contributing.md
+++ b/contributing.md
@@ -15,7 +15,11 @@ that you can click on to open them.
`name/feature` (i.e. `loek/dll-so-poc` or `jaro/class2`)
- The master branch is considered stable, and should always contain a
working/compiling version of the project
+- Pull requests for new code include either automated tests for the new code or
+ an explanation as to why the code can not (reliably) be tested
+<!--
- TODO: tagging / versions
+-->
# Code style
@@ -790,6 +794,7 @@ that you can click on to open them.
}
```
</td></tr></table></details>
+- Do not implement new classes as singletons
## CMakeLists-specific
diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h
deleted file mode 100644
index 9051c5e..0000000
--- a/src/crepe/Asset.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include <fstream>
-#include <iostream>
-#include <string>
-
-namespace crepe {
-
-/**
- * \brief Asset location helper
- *
- * This class is used to locate and canonicalize paths to game asset files, and should *always*
- * be used when retrieving files from disk.
- */
-class Asset {
-public:
- /**
- * \param src Unique identifier to asset
- */
- Asset(const std::string & src);
-
-public:
- /**
- * \brief Get an input stream to the contents of this asset
- * \return Input stream with file contents
- */
- std::istream & get_stream();
- /**
- * \brief Get the canonical path to this asset
- * \return Canonical path to this asset
- */
- const std::string & get_canonical() const;
-
-private:
- //! Canonical path to asset
- const std::string src;
- //! File handle (stream)
- std::ifstream file;
-};
-
-} // namespace crepe
diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h
index 05dccba..685dd3a 100644
--- a/src/crepe/api/Asset.h
+++ b/src/crepe/api/Asset.h
@@ -1,7 +1,6 @@
#pragma once
#include <string>
-#include <unordered_map>
namespace crepe {
@@ -52,7 +51,16 @@ private:
namespace std {
+//! Hash helper struct
template<> struct hash<const crepe::Asset> {
+ /**
+ * \brief Hash operator for crepe::Asset
+ *
+ * This function hashes a crepe::Asset instance, allowing it to be used as a key in an \c
+ * std::unordered_map.
+ *
+ * \returns Hash value
+ */
size_t operator()(const crepe::Asset & asset) const noexcept;
};
diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h
index 1ad3a6d..8417a25 100644
--- a/src/crepe/util/OptionalRef.h
+++ b/src/crepe/util/OptionalRef.h
@@ -12,18 +12,51 @@ namespace crepe {
template <typename T>
class OptionalRef {
public:
+ //! Initialize empty (nonexistant) reference
OptionalRef() = default;
- OptionalRef(T &);
- OptionalRef<T> & operator=(T &);
+ //! Initialize reference with value
+ OptionalRef(T & ref);
+ /**
+ * \brief Assign new reference
+ *
+ * \param ref Reference to assign
+ *
+ * \return Reference to this (required for operator)
+ */
+ OptionalRef<T> & operator=(T & ref);
+ /**
+ * \brief Check if this reference is not empty
+ *
+ * \returns `true` if reference is set, or `false` if it is not
+ */
explicit operator bool() const noexcept;
+ /**
+ * \brief Assign new reference
+ *
+ * \param ref Reference to assign
+ */
void set(T &) noexcept;
+ /**
+ * \brief Retrieve this reference
+ *
+ * \returns Internal reference if it is set
+ *
+ * \throws std::runtime_error if this function is called while the reference it not set
+ */
T & get() const;
+ /**
+ * \brief Make this reference empty
+ */
void clear() noexcept;
+ //! Copy constructor
OptionalRef(const OptionalRef<T> &);
+ //! Move constructor
OptionalRef(OptionalRef<T> &&);
+ //! Copy assignment
OptionalRef<T> & operator=(const OptionalRef<T> &);
+ //! Move assignment
OptionalRef<T> & operator=(OptionalRef<T> &&);
private:
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
index e23d485..f3bf349 100644
--- a/src/example/audio_internal.cpp
+++ b/src/example/audio_internal.cpp
@@ -29,15 +29,11 @@ int main() {
SoundContext ctx{};
Sound sound{ctx};
// Load a background track (Ogg Vorbis)
- auto _bgm = sound.clone(Asset{"mwe/audio/bgm.ogg"});
- Sound & bgm = *dynamic_cast<Sound *>(_bgm.get());
+ auto bgm = Sound("mwe/audio/bgm.ogg");
// Load three short samples (WAV)
- auto _sfx1 = sound.clone(Asset{"mwe/audio/sfx1.wav"});
- Sound & sfx1 = *dynamic_cast<Sound *>(_sfx1.get());
- auto _sfx2 = sound.clone(Asset{"mwe/audio/sfx2.wav"});
- Sound & sfx2 = *dynamic_cast<Sound *>(_sfx2.get());
- auto _sfx3 = sound.clone(Asset{"mwe/audio/sfx3.wav"});
- Sound & sfx3 = *dynamic_cast<Sound *>(_sfx3.get());
+ auto sfx1 = Sound("mwe/audio/sfx1.wav");
+ auto sfx2 = Sound("mwe/audio/sfx2.wav");
+ auto sfx3 = Sound("mwe/audio/sfx3.wav");
// Start the background track
bgm.play();