Last active
August 29, 2015 14:00
-
-
Save arturl/11071021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <tchar.h> | |
#include <typeinfo> | |
#include <memory> | |
namespace std | |
{ | |
class sequential_execution_policy{ | |
}; | |
class parallel_execution_policy{ | |
}; | |
class vector_execution_policy{ | |
}; | |
class execution_policy | |
{ | |
struct policy_holder_base { | |
virtual void* get_policy() = 0; | |
}; | |
template<typename ExecutionPolicy> | |
struct policy_holder : public policy_holder_base{ | |
ExecutionPolicy data_; | |
policy_holder(const ExecutionPolicy& p) : data_(p) {} | |
virtual void* get_policy() { return &data_; } | |
}; | |
public: | |
template<class ExecutionPolicy> | |
execution_policy(const ExecutionPolicy &exec) : typeinfo_(typeid(exec)) | |
{ | |
target_ = std::make_shared<policy_holder<ExecutionPolicy>>(exec); | |
} | |
// obtains the typeid of the stored target | |
const type_info& target_type() const | |
{ | |
return typeinfo_; | |
} | |
// obtains a pointer to the stored target | |
template<class ExecutionPolicy> | |
ExecutionPolicy * target() | |
{ | |
return static_cast<ExecutionPolicy*>(target_->get_policy()); | |
} | |
private: | |
const std::type_info& typeinfo_; | |
std::shared_ptr<policy_holder_base> target_; | |
}; | |
} | |
namespace std | |
{ | |
sequential_execution_policy seq; | |
parallel_execution_policy par; | |
vector_execution_policy vec; | |
} | |
namespace ppl | |
{ | |
// A PPL-specific policy | |
struct par_with_chunk_size_execution_policy | |
{ | |
int chunk_size; | |
par_with_chunk_size_execution_policy(int size) : chunk_size(size) {} | |
}; | |
par_with_chunk_size_execution_policy par_with_fixed_chunk_size(512); | |
} | |
void some_api(std::execution_policy exec, int arg1, double arg2) | |
{ | |
if(exec.target_type() == typeid(std::sequential_execution_policy)) | |
{ | |
std::sequential_execution_policy *exec_ptr = exec.target<std::sequential_execution_policy>(); | |
printf("Received a sequential policy\n"); | |
} | |
else if(exec.target_type() == typeid(std::parallel_execution_policy)) | |
{ | |
std::parallel_execution_policy *exec_ptr = exec.target<std::parallel_execution_policy>(); | |
printf("Received a parallel policy\n"); | |
} | |
else if(exec.target_type() == typeid(std::vector_execution_policy)) | |
{ | |
std::vector_execution_policy *exec_ptr = exec.target<std::vector_execution_policy>(); | |
printf("Received a vector policy\n"); | |
} | |
else | |
{ | |
printf("Received some other policy\n"); | |
if(exec.target_type() == typeid(ppl::par_with_chunk_size_execution_policy)) | |
{ | |
ppl::par_with_chunk_size_execution_policy *exec_ptr = exec.target<ppl::par_with_chunk_size_execution_policy>(); | |
printf("Received a ppl par_with_fixed_chunk_size policy with chunk size %d\n", exec_ptr->chunk_size); | |
} | |
} | |
} | |
int main() | |
{ | |
std::execution_policy policy = std::par; | |
some_api(policy, 1, 3.14); | |
std::execution_policy policy2 = ppl::par_with_fixed_chunk_size; | |
some_api(policy2, 2, 2.718); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment