Skip to content

Commit

Permalink
singleton design pattern example
Browse files Browse the repository at this point in the history
  • Loading branch information
FullZing committed Jul 2, 2018
1 parent ee98b9d commit 22257fd
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
6 changes: 6 additions & 0 deletions singleton/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.9)
project(singleton)

set(CMAKE_CXX_STANDARD 11)

add_executable(singleton main.cpp ComputeSingleton.h ComputeSingleton.cpp CPUCompute.h GPUCompute.h CPUCompute.cpp GPUCompute.cpp)
8 changes: 8 additions & 0 deletions singleton/CPUCompute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "CPUCompute.h"

CPUCompute::CPUCompute() {
}

std::shared_ptr<CPUCompute> CPUCompute::get_shared_ptr() {
return std::static_pointer_cast<CPUCompute>(shared_from_this());
}
11 changes: 11 additions & 0 deletions singleton/CPUCompute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "ComputeSingleton.h"

class CPUCompute : public ComputeSingleton {
public:
CPUCompute();
std::shared_ptr<CPUCompute> get_shared_ptr();
private:
//memory or other operations
};
28 changes: 28 additions & 0 deletions singleton/ComputeSingleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cstdlib>
#include "ComputeSingleton.h"

std::shared_ptr<ComputeSingleton> ComputeSingleton::instance_;

std::map<std::string, std::shared_ptr<ComputeSingleton>> ComputeSingleton::registry_;

void ComputeSingleton::Register(const std::string &name, std::shared_ptr<ComputeSingleton> compute) {
if (registry_.find(name) == registry_.end()) {
registry_.emplace(std::make_pair(name, compute));
}
}

std::shared_ptr<ComputeSingleton> ComputeSingleton::Instance() {
if (instance_ == nullptr) {
// std::string compute_library_name = std::getenv("ComputeLibrary");
std::string compute_library_name = "CPU";
instance_ = Lookup(compute_library_name);
}
return instance_;
}

std::shared_ptr<ComputeSingleton> ComputeSingleton::Lookup(const std::string &name) {
auto search = registry_.find(name);
if (search != registry_.end()) {
return search->second;
}
}
16 changes: 16 additions & 0 deletions singleton/ComputeSingleton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <map>
#include <memory>
#include <string>

class ComputeSingleton : public std::enable_shared_from_this<ComputeSingleton> {
public:
static void Register(const std::string& name, std::shared_ptr<ComputeSingleton>);
static std::shared_ptr<ComputeSingleton> Instance();
protected:
static std::shared_ptr<ComputeSingleton> Lookup(const std::string& name);
private:
static std::shared_ptr<ComputeSingleton> instance_;
static std::map<std::string, std::shared_ptr<ComputeSingleton>> registry_;
};
9 changes: 9 additions & 0 deletions singleton/GPUCompute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "GPUCompute.h"

GPUCompute::GPUCompute() {
}

std::shared_ptr<GPUCompute> GPUCompute::get_shared_ptr() {
return std::static_pointer_cast<GPUCompute>(shared_from_this());
}

11 changes: 11 additions & 0 deletions singleton/GPUCompute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "ComputeSingleton.h"

class GPUCompute : public ComputeSingleton {
public:
GPUCompute();
std::shared_ptr<GPUCompute> get_shared_ptr();
private:
//memory or other operations
};
17 changes: 17 additions & 0 deletions singleton/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <cstdlib>
#include <iostream>
#include "CPUCompute.h"

int main() {

// std::setenv();
std::shared_ptr<CPUCompute> cpu_compute(new CPUCompute());
ComputeSingleton::Register("CPU" ,cpu_compute->get_shared_ptr());

std::shared_ptr<CPUCompute> gpu_compute(new CPUCompute());
ComputeSingleton::Register("GPU" ,cpu_compute->get_shared_ptr());

auto compute_library = ComputeSingleton::Instance();

return 0;
}

0 comments on commit 22257fd

Please sign in to comment.