Skip to content

Commit

Permalink
Merge pull request #77 from ikbuibui/views
Browse files Browse the repository at this point in the history
Enable views on existing resources
  • Loading branch information
psychocoderHPC authored Jun 7, 2024
2 parents e7bc6e0 + 08d3a23 commit 9f77dd6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 10 deletions.
56 changes: 56 additions & 0 deletions redGrapes/resource/fieldresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "redGrapes/TaskFreeCtx.hpp"
#include "redGrapes/resource/access/field.hpp"
#include "redGrapes/resource/resource.hpp"
#include "redGrapes/util/traits.hpp"

namespace redGrapes
{
Expand Down Expand Up @@ -101,6 +102,19 @@ namespace redGrapes
{
}

AreaGuard(
Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res,
std::shared_ptr<Container> const& obj)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(res, obj)
{
}

template<typename... Args>
AreaGuard(Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res, Args&&... args)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(res, std::forward<Args>(args)...)
{
}

AreaGuard(AreaGuard const& other, Index begin, Index end)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(other)
, m_area(other.make_area(begin, end))
Expand Down Expand Up @@ -183,6 +197,19 @@ namespace redGrapes
ReadGuard(ResourceId id, Args&&... args) : AreaGuard<Container, TTask>(id, std::forward<Args>(args)...)
{
}

ReadGuard(
Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res,
std::shared_ptr<Container> const& obj)
: AreaGuard<Container, TTask>(res, obj)
{
}

template<typename... Args>
ReadGuard(Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res, Args&&... args)
: AreaGuard<Container, TTask>(res, std::forward<Args>(args)...)
{
}
};

template<typename Container, typename TTask>
Expand Down Expand Up @@ -239,6 +266,19 @@ namespace redGrapes
WriteGuard(ResourceId id, Args&&... args) : ReadGuard<Container, TTask>(id, std::forward<Args>(args)...)
{
}

WriteGuard(
Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res,
std::shared_ptr<Container> const& obj)
: ReadGuard<Container, TTask>(res, obj)
{
}

template<typename... Args>
WriteGuard(Resource<TTask, access::FieldAccess<trait::Field<Container>::dim>> const& res, Args&&... args)
: ReadGuard<Container, TTask>(res, std::forward<Args>(args)...)
{
}
};

} // namespace fieldresource
Expand All @@ -256,12 +296,28 @@ namespace redGrapes
}

template<typename... Args>
requires(
!(traits::is_specialization_of<std::decay_t<traits::first_type_t<Args...>>, FieldResource>::value
|| std::is_same_v<std::decay_t<traits::first_type_t<Args...>>, Container*>) )

FieldResource(Args&&... args)
: fieldresource::WriteGuard<Container, TTask>(
TaskFreeCtx::create_resource_uid(),
std::forward<Args>(args)...)
{
}

template<typename U>
FieldResource(FieldResource<U, TTask> const& res, Container* c)
: fieldresource::WriteGuard<Container, TTask>(res, std::shared_ptr<Container>(c))
{
}

template<typename U, typename... Args>
FieldResource(FieldResource<U, TTask> const& res, Args&&... args)
: fieldresource::WriteGuard<Container, TTask>(res, std::forward<Args>(args)...)
{
}
};

}; // namespace redGrapes
43 changes: 43 additions & 0 deletions redGrapes/resource/ioresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include "redGrapes/TaskFreeCtx.hpp"
#include "redGrapes/resource/access/io.hpp"
#include "redGrapes/resource/resource.hpp"
#include "redGrapes/util/traits.hpp"

#include <memory>
#include <type_traits>
#include <utility>

namespace redGrapes
{
Expand Down Expand Up @@ -59,6 +64,17 @@ namespace redGrapes
: SharedResourceObject<T, TTask, access::IOAccess>(id, std::forward<Args>(args)...)
{
}

ReadGuard(Resource<TTask, access::IOAccess> const& res, std::shared_ptr<T> const& obj)
: SharedResourceObject<T, TTask, access::IOAccess>(res, obj)
{
}

template<typename... Args>
ReadGuard(Resource<TTask, access::IOAccess> const& res, Args&&... args)
: SharedResourceObject<T, TTask, access::IOAccess>(res, std::forward<Args>(args)...)
{
}
};

template<typename T, typename TTask>
Expand Down Expand Up @@ -98,6 +114,17 @@ namespace redGrapes
WriteGuard(ResourceId id, Args&&... args) : ReadGuard<T, TTask>(id, std::forward<Args>(args)...)
{
}

WriteGuard(Resource<TTask, access::IOAccess> const& res, std::shared_ptr<T> const& obj)
: ReadGuard<T, TTask>(res, obj)
{
}

template<typename... Args>
WriteGuard(Resource<TTask, access::IOAccess> const& res, Args&&... args)
: ReadGuard<T, TTask>(res, std::forward<Args>(args)...)
{
}
};

} // namespace ioresource
Expand All @@ -111,11 +138,27 @@ namespace redGrapes
}

template<typename... Args>
requires(
!(traits::is_specialization_of<std::decay_t<traits::first_type_t<Args...>>, IOResource>::value
|| std::is_same_v<std::decay_t<traits::first_type_t<Args...>>, std::shared_ptr<T>>) )
IOResource(Args&&... args)
: ioresource::WriteGuard<T, TTask>(TaskFreeCtx::create_resource_uid(), std::forward<Args>(args)...)
{
}

template<typename U>
IOResource(IOResource<U, TTask> const& res, std::shared_ptr<T> const& obj)
: ioresource::WriteGuard<T, TTask>(res, obj)
{
}

template<typename U, typename... Args>
IOResource(IOResource<U, TTask> const& res, Args&&... args)
: ioresource::WriteGuard<T, TTask>(res, std::forward<Args>(args)...)
{
}


}; // struct IOResource

} // namespace redGrapes
28 changes: 18 additions & 10 deletions redGrapes/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ namespace redGrapes

std::shared_ptr<ResourceBase<TTask>> base;

Resource(std::shared_ptr<ResourceBase<TTask>> base) : base(base)
{
}

public:
Resource(ResourceId id)
: base{redGrapes::memory::alloc_shared_bind<ResourceBase<TTask>>(mapping::map_resource_to_worker(id), id)}
Expand Down Expand Up @@ -364,13 +360,9 @@ namespace redGrapes
// protected:
std::shared_ptr<T> obj;

SharedResourceObject(ResourceId id, std::shared_ptr<T> obj) : Resource<TTask, AccessPolicy>(id), obj(obj)
{
}

SharedResourceObject(ResourceId id, SharedResourceObject const& other)
SharedResourceObject(ResourceId id, std::shared_ptr<T> const& obj)
: Resource<TTask, AccessPolicy>(id)
, obj(other.obj)
, obj(obj)
{
}

Expand All @@ -380,6 +372,22 @@ namespace redGrapes
, obj{memory::alloc_shared_bind<T>(mapping::map_resource_to_worker(id), std::forward<Args>(args)...)}
{
}

SharedResourceObject(Resource<TTask, AccessPolicy> const& res, std::shared_ptr<T> const& obj)
: Resource<TTask, AccessPolicy>{res}
, obj{obj}
{
}

template<typename... Args>
SharedResourceObject(Resource<TTask, AccessPolicy> const& res, Args&&... args)
: Resource<TTask, AccessPolicy>{res}
, obj{memory::alloc_shared_bind<T>(
mapping::map_resource_to_worker(res.resource_id()),
std::forward<Args>(args)...)}
{
}

}; // struct SharedResourceObject

} // namespace redGrapes
Expand Down
28 changes: 28 additions & 0 deletions redGrapes/util/traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <type_traits>

namespace redGrapes::traits
{
// Primary template for is_specialization_of
template<typename, template<typename...> class>
struct is_specialization_of : std::false_type
{
};

// Specialization for types that are specializations of the template
template<typename... Args, template<typename...> class Template>
struct is_specialization_of<Template<Args...>, Template> : std::true_type
{
};

template<typename T = std::false_type, typename...>
struct first_type
{
using type = T;
};

// Convenience alias template
template<typename... Ts>
using first_type_t = typename first_type<Ts...>::type;

} // namespace redGrapes::traits

0 comments on commit 9f77dd6

Please sign in to comment.