Skip to content

Commit

Permalink
Added mt::std::unique_ptr and mt::std::make_unique. Reduced use o…
Browse files Browse the repository at this point in the history
…f raw pointers related to multi_thread_allocator.
  • Loading branch information
Mankarse committed Apr 5, 2018
1 parent aa3445a commit fae96a8
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 110 deletions.
3 changes: 3 additions & 0 deletions ide/msvc2017/HourglassII/HourglassII/HourglassII.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@
<ClInclude Include="..\..\..\..\src\mt\std\map">
<FileType>CppHeader</FileType>
</ClInclude>
<ClInclude Include="..\..\..\..\src\mt\std\memory">
<FileType>CppHeader</FileType>
</ClInclude>
<ClInclude Include="..\..\..\..\src\mt\std\set">
<FileType>CppHeader</FileType>
</ClInclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
<MinimalRebuild>false</MinimalRebuild>
<ConformanceMode>true</ConformanceMode>
<AdditionalOptions>/bigobj</AdditionalOptions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -315,6 +316,9 @@
<ClInclude Include="..\..\..\..\src\mt\std\map">
<FileType>CppHeader</FileType>
</ClInclude>
<ClInclude Include="..\..\..\..\src\mt\std\memory">
<FileType>CppHeader</FileType>
</ClInclude>
<ClInclude Include="..\..\..\..\src\mt\std\set">
<FileType>CppHeader</FileType>
</ClInclude>
Expand Down
37 changes: 18 additions & 19 deletions src/BoxGlitzAdder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ImageGlitz.h"
#include "multi_thread_allocator.h"
#include "RectangleGlitz.h"
#include "mt/std/memory"
namespace hg {
class BoxGlitzAdder final {
public:
Expand All @@ -26,14 +27,12 @@ class BoxGlitzAdder final {
int size,
TimeDirection timeDirection) const
{
Glitz sameDirectionGlitz(
new (multi_thread_tag{}) ImageGlitz(
Glitz sameDirectionGlitz(mt::std::make_unique<ImageGlitz>(
500, mt::std::string("global.box"),
position.x, position.y,
size, size));

Glitz oppositeDirectionGlitz(
new (multi_thread_tag{}) ImageGlitz(
Glitz oppositeDirectionGlitz(mt::std::make_unique<ImageGlitz>(
500, mt::std::string("global.box_r"),
position.x, position.y,
size, size));
Expand All @@ -53,23 +52,23 @@ class BoxGlitzAdder final {
{
persistentGlitz->push_back(
GlitzPersister(
new (multi_thread_tag{}) StaticGlitzPersister(
mt::std::make_unique<StaticGlitzPersister>(
Glitz(
new (multi_thread_tag{}) RectangleGlitz(
1500,
x,
y,
size,
size,
timeDirection == TimeDirection::FORWARDS ? 0xFF000000u : 0x00FFFF00u)),
mt::std::make_unique<RectangleGlitz>(
1500,
x,
y,
size,
size,
timeDirection == TimeDirection::FORWARDS ? 0xFF000000u : 0x00FFFF00u)),
Glitz(
new (multi_thread_tag{}) RectangleGlitz(
1500,
x,
y,
size,
size,
timeDirection == TimeDirection::REVERSE ? 0xFF000000u : 0x00FFFF00u)),
mt::std::make_unique<RectangleGlitz>(
1500,
x,
y,
size,
size,
timeDirection == TimeDirection::REVERSE ? 0xFF000000u : 0x00FFFF00u)),
60,
timeDirection)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/DirectLuaTriggerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ GlitzPersister toGlitzPersister(lua_State *L) {
}
TimeDirection timeDirection = readField<TimeDirection>(L, "timeDirection");
return GlitzPersister(
new (multi_thread_tag{}) StaticGlitzPersister(
mt::std::make_unique<StaticGlitzPersister>(
std::move(forwardsGlitz), std::move(reverseGlitz), lifetime, timeDirection));
}
else if (type == "audio") {
mt::std::string key = readField<mt::std::string>(L, "key");
int duration = readField<int>(L, "duration");
TimeDirection timeDirection = readField<TimeDirection>(L, "timeDirection");
return GlitzPersister(
new (multi_thread_tag{}) AudioGlitzPersister(std::move(key), duration, timeDirection));
mt::std::make_unique<AudioGlitzPersister>(std::move(key), duration, timeDirection));
}
std::stringstream ss;
ss << "Unknown Glitz Persister Type: " << type;
Expand Down
10 changes: 6 additions & 4 deletions src/Glitz.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
#include "clone_ptr.h"
#include "memory_source_clone.h"
#include "multi_thread_memory_source.h"
#include "multi_thread_allocator.h"
#include "multi_thread_deleter.h"
#include "GlitzImplementation.h"
#include "mt/std/memory"
#include <cassert>
namespace hg {
class Glitz final : boost::totally_ordered<Glitz> {
public:
//TODO: Don't use raw pointer here?
explicit Glitz(GlitzImplementation *impl)
: impl(impl)
explicit Glitz(mt::std::unique_ptr<GlitzImplementation> impl)
: impl(impl.release())
{
assert(impl);
assert(this->impl);
}

void display(LayeredCanvas &canvas) const {
Expand Down
13 changes: 7 additions & 6 deletions src/GlitzPersister.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "GlitzPersister.h"
#include "Frame.h"
#include "mt/std/memory"
#include <boost/polymorphic_cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
Expand All @@ -16,7 +17,7 @@ ObjectAndTime<GlitzPersister, Frame *> StaticGlitzPersister::runStep(Frame *fram
{
return ObjectAndTime<GlitzPersister, Frame*>(
GlitzPersister(
new (multi_thread_tag{}) StaticGlitzPersister(
mt::std::make_unique<StaticGlitzPersister>(
forwardsGlitz, reverseGlitz,
framesLeft - 1, timeDirection)),
framesLeft ? nextFrame(frame, timeDirection) : nullptr);
Expand All @@ -43,6 +44,7 @@ AudioGlitzPersister::AudioGlitzPersister(
{}

AudioGlitzPersister::AudioGlitzPersister(
AudioGlitzPersister_access,
mt::std::string key,
unsigned duration,
unsigned currentFrame,
Expand All @@ -55,15 +57,13 @@ AudioGlitzPersister::AudioGlitzPersister(

Glitz AudioGlitzPersister::getForwardsGlitz() const {
mt::std::string suffix = timeDirection == TimeDirection::FORWARDS ? "" : "_r";
return Glitz(
new (multi_thread_tag{}) AudioGlitz(
return Glitz(mt::std::make_unique<AudioGlitz>(
key+suffix,
timeDirection == TimeDirection::FORWARDS ? currentFrame : duration-currentFrame));
}
Glitz AudioGlitzPersister::getReverseGlitz() const {
mt::std::string suffix = timeDirection == TimeDirection::REVERSE ? "" : "_r";
return Glitz(
new (multi_thread_tag{}) AudioGlitz(
return Glitz(mt::std::make_unique<AudioGlitz>(
key+suffix,
timeDirection == TimeDirection::REVERSE ? currentFrame : duration-currentFrame));
}
Expand All @@ -72,7 +72,8 @@ ObjectAndTime<GlitzPersister, Frame *> AudioGlitzPersister::runStep(Frame *frame
{
return ObjectAndTime<GlitzPersister, Frame*>(
GlitzPersister(
new (multi_thread_tag{}) AudioGlitzPersister(
mt::std::make_unique<AudioGlitzPersister>(
AudioGlitzPersister_access{},
key,
duration,
currentFrame + 1,
Expand Down
24 changes: 17 additions & 7 deletions src/GlitzPersister.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "TimeDirection.h"
#include <boost/operators.hpp>
#include "mt/std/string"
#include "mt/std/memory"
#include <tuple>
#include "ObjectAndTime.h"
#include "Glitz.h"
Expand All @@ -29,10 +30,10 @@ struct GlitzPersisterImpl : private boost::totally_ordered<GlitzPersisterImpl>
class GlitzPersister final : boost::totally_ordered<GlitzPersister>
{
public:
GlitzPersister(GlitzPersisterImpl *impl) :
impl(impl)
explicit GlitzPersister(mt::std::unique_ptr<GlitzPersisterImpl> impl) :
impl(impl.release())
{
assert(impl);
assert(this->impl);
}

ObjectAndTime<GlitzPersister, Frame *> runStep(Frame *frame) const {
Expand Down Expand Up @@ -63,7 +64,7 @@ class GlitzPersister final : boost::totally_ordered<GlitzPersister>
class GlitzPersisterConstPtr final : boost::totally_ordered<GlitzPersisterConstPtr>
{
public:
GlitzPersisterConstPtr(GlitzPersister const &glitzPersister) : glitzPersister(&glitzPersister) {}
explicit GlitzPersisterConstPtr(GlitzPersister const &glitzPersister) : glitzPersister(&glitzPersister) {}
typedef GlitzPersister base_type;
GlitzPersister const &get() const { return *glitzPersister; }

Expand All @@ -85,7 +86,7 @@ struct ConstPtr_of<GlitzPersister> {
class StaticGlitzPersister final : public GlitzPersisterImpl
{
public:
StaticGlitzPersister(
explicit StaticGlitzPersister(
Glitz const &forwardsGlitz, Glitz const &reverseGlitz,
unsigned lifetime, TimeDirection timeDirection);
ObjectAndTime<GlitzPersister, Frame *> runStep(Frame *frame) const override;
Expand Down Expand Up @@ -119,8 +120,13 @@ class StaticGlitzPersister final : public GlitzPersisterImpl

class AudioGlitzPersister final : public GlitzPersisterImpl
{
struct AudioGlitzPersister_access final{
friend class AudioGlitzPersister;
private:
AudioGlitzPersister_access(){}
};
public:
AudioGlitzPersister(
explicit AudioGlitzPersister(
mt::std::string key,
unsigned duration,
TimeDirection timeDirection);
Expand All @@ -141,12 +147,16 @@ class AudioGlitzPersister final : public GlitzPersisterImpl
}
bool operator==(GlitzPersisterImpl const &o) const override;
bool operator<(GlitzPersisterImpl const &o) const override;
private:

//Mostly private. Only accessible via AudioGlitzPersister_access key, which can only be created by
//AudioGlitzPersister.
AudioGlitzPersister(
AudioGlitzPersister_access,
mt::std::string key,
unsigned duration,
unsigned currentFrame,
TimeDirection timeDirection);
private:
mt::std::string key;
unsigned duration;
unsigned currentFrame;
Expand Down
Loading

0 comments on commit fae96a8

Please sign in to comment.