made std::shared_ptr and std::functional optional

This commit is contained in:
Noel Berry
2022-02-11 15:20:07 -08:00
parent 0c809f8085
commit cedc57e322
17 changed files with 212 additions and 162 deletions

View File

@ -36,5 +36,4 @@
#include "blah/streams/bufferstream.h"
#include "blah/streams/filestream.h"
#include "blah/streams/memorystream.h"
#include "blah/streams/stream.h"
#include "blah/streams/endian.h"
#include "blah/streams/stream.h"

View File

@ -5,10 +5,10 @@
namespace Blah
{
// Application Event Functions
using AppEventFn = Func<void()>;
using AppEventFn = Func<void>;
// Application Logging Functions
using AppLogFn = Func<void(const char* message, Log::Category category)>;
using AppLogFn = Func<void, const char*, Log::Category>;
// Type of Renderer the Application is using
enum class RendererType

View File

@ -1,45 +1,37 @@
#pragma once
#include <cstdint> // for integer types
#include <cstddef> // for size_t type
#include <memory> // for std::shared_ptr
#include <functional> // for std::function
// In-Place constructor new, used by Vector/StackVector
#include <new>
// Asserts
#if defined(DEBUG) || defined(_DEBUG)
#include <cstdlib> // for abort
#define BLAH_ASSERT(condition, msg) \
do { if (!(condition)) { Blah::Log::error("%s\n\tin %s:%d", (msg), __FILE__, __LINE__); abort(); } } while(0)
# include <stdlib.h> // for abort
# define BLAH_ASSERT(condition, msg) do { if (!(condition)) { Blah::Log::error("%s\n\tin %s:%d", (msg), __FILE__, __LINE__); abort(); } } while(0)
#else
#define BLAH_ASSERT(condition, msg) \
do { if (!(condition)) { Blah::Log::error("%s\n\tin %s:%d", (msg), __FILE__, __LINE__); } } while(0)
#endif
// maximum length of a print/warn/error message
#ifndef BLAH_MESSAGE
#define BLAH_MESSAGE 1024
# define BLAH_ASSERT(condition, msg) do { if (!(condition)) { Blah::Log::error("%s\n\tin %s:%d", (msg), __FILE__, __LINE__); } } while(0)
#endif
// Numeric Types
#include <stdint.h> // for integer types
#include <stddef.h> // for size_t type
namespace Blah
{
using i8 = int8_t;
// Numeric Types
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u8 = uint8_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
}
template<typename T>
using Ref = std::shared_ptr<T>;
template<typename T>
using WeakRef = std::weak_ptr<T>;
template<typename T>
using Func = std::function<T>;
// Logging
namespace Blah
{
namespace Log
{
enum class Category
@ -49,8 +41,100 @@ namespace Blah
Error
};
constexpr int max_length = 1024;
void info(const char* message, ...);
void warn(const char* message, ...);
void error(const char* message, ...);
}
}
}
// Initializer list, required for Vector/StackVector
#include <initializer_list>
namespace Blah
{
template<typename T> using InitializerList = std::initializer_list<T>;
}
// Functional, for App Callbacks
#ifndef BLAH_NO_FUNCTIONAL
#include <functional>
namespace Blah
{
template<class Ret, class...Args> using Func = std::function<Ret(Args...)>;
}
#else
namespace Blah
{
template<class Ret, class...Args> using Func = Ret(*)(Args...);
}
#endif
// Ref Counter, for Graphics & Input Resources
#ifndef BLAH_NO_SHARED_PTR
#include <memory>
namespace Blah
{
template<typename T> using Ref = std::shared_ptr<T>;
}
#else
namespace Blah
{
template<typename T>
class Ref
{
template<class Y> friend class Ref;
private:
T* m_instance;
i32* m_counter;
Ref(T* instance, i32* counter) : m_instance(instance), m_counter(counter) {}
public:
Ref() : m_instance(nullptr), m_counter(nullptr) {}
template<class Y>
Ref(Y* instance) : Ref(static_cast<T*>(instance), new i32(1)) {}
Ref(const Ref& rhs) : Ref(rhs.m_instance, rhs.m_counter) { if (m_counter) (*m_counter)++; }
Ref(Ref&& rhs) : Ref(rhs.m_instance, rhs.m_counter) { rhs.m_instance = nullptr; rhs.m_counter = nullptr; }
Ref& operator=(const Ref& rhs)
{
if (this != &rhs)
{
reset();
m_instance = rhs.m_instance; m_counter = rhs.m_counter;
if (m_counter) (*m_counter)++;
}
return *this;
}
Ref& operator=(Ref&& rhs)
{
if (this != &rhs)
{
reset();
m_instance = rhs.m_instance; m_counter = rhs.m_counter;
rhs.m_instance = nullptr; rhs.m_counter = nullptr;
}
return *this;
}
~Ref() { reset(); }
void reset()
{
if (m_counter) (*m_counter)--;
if (m_counter && (*m_counter) <= 0) { delete m_instance; delete m_counter; }
m_instance = nullptr; m_counter = nullptr;
}
int use_count() const { return (m_counter ? (*m_counter) : 0); }
T* get() const { return m_instance; }
T* operator->() const { return m_instance; }
operator bool() const { return m_counter && (*m_counter) > 0; }
template<class Y> bool operator==(const Ref<Y>& rhs) const { return m_counter == rhs.m_counter; }
template<class Y> bool operator!=(const Ref<Y>& rhs) const { return m_counter != rhs.m_counter; }
template<class Y> operator Ref<Y>() { if (m_counter) (*m_counter)++; return Ref<Y>(static_cast<Y*>(m_instance), m_counter); }
};
}
#endif

View File

@ -1,7 +1,5 @@
#pragma once
#include <blah/common.h>
#include <new>
#include <initializer_list>
namespace Blah
{
@ -19,7 +17,7 @@ namespace Blah
static constexpr size_t capacity = Capacity;
StackVector();
StackVector(const std::initializer_list<T>& init);
StackVector(const InitializerList<T>& list);
StackVector(const StackVector& src);
StackVector(StackVector&& src) noexcept;
~StackVector();
@ -62,7 +60,7 @@ namespace Blah
}
template<class T, size_t Capacity>
inline StackVector<T, Capacity>::StackVector(const std::initializer_list<T>& init)
inline StackVector<T, Capacity>::StackVector(const InitializerList<T>& init)
{
m_count = 0;
for (auto& it : init)

View File

@ -1,14 +1,12 @@
#pragma once
#include <blah/common.h>
#include <stdarg.h>
#include <cstdio>
#include <blah/containers/vector.h>
#include <functional>
#include <stdarg.h>
#include <stdio.h>
namespace Blah
{
template<int T>
class StrOf;
template<int T> class StrOf;
using String = StrOf<64>;
using FilePath = StrOf<260>;

View File

@ -1,8 +1,6 @@
#pragma once
#include <blah/common.h>
#include <initializer_list>
#include <new>
#include <cstring>
#include <string.h>
namespace Blah
{
@ -19,9 +17,9 @@ namespace Blah
Vector();
Vector(int capacity);
Vector(const InitializerList<T>& list);
Vector(const Vector& src);
Vector(Vector&& src) noexcept;
Vector(std::initializer_list<T> list);
~Vector();
Vector& operator=(const Vector& src);
@ -98,9 +96,8 @@ namespace Blah
src.m_count = 0;
}
template<class T>
inline Vector<T>::Vector(std::initializer_list<T> list)
inline Vector<T>::Vector(const InitializerList<T>& list)
{
m_buffer = nullptr;
m_count = m_capacity = 0;

View File

@ -123,7 +123,7 @@ namespace Blah
void set_sampler(const TextureSampler& sampler);
// Draws the batch to the given target
void render(const TargetRef& target = nullptr);
void render(const TargetRef& target = TargetRef());
// Draws the batch to the given target, with the provided matrix
void render(const TargetRef& target, const Mat4x4f& matrix);

View File

@ -44,7 +44,7 @@ namespace Blah
int stride = 0;
VertexFormat() = default;
VertexFormat(std::initializer_list<VertexAttribute> attributes, int stride = 0);
VertexFormat(const StackVector<VertexAttribute, 16>& attributes, int stride = 0);
};
// Supported Vertex Index formats

View File

@ -1,5 +1,4 @@
#pragma once
#include <blah/common.h>
#include <blah/numerics/spatial.h>
#include <blah/containers/str.h>

View File

@ -14,15 +14,15 @@ namespace Blah
// uptime, in seconds
extern double seconds;
// delta time from last frame
extern float delta;
// previous frame uptime, in ticks
extern u64 previous_ticks;
// previous frame uptime, in seconds
extern double previous_seconds;
// delta time from last frame
extern float delta;
// time the application should pause for
extern float pause_timer;