Skip to content

Commit

Permalink
TypeRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
simonask committed May 26, 2012
1 parent 358233d commit b88b419
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions type_registry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "type_registry.hpp"
#include "struct_type.hpp"
#include "basic.hpp"
#include <map>

struct TypeRegistry::Impl {
std::map<std::string, const StructTypeBase*> type_map;
};

TypeRegistry::Impl* TypeRegistry::impl() {
static Impl* i = new Impl;
return i;
}

void TypeRegistry::add(const StructTypeBase* type) {
impl()->type_map[type->name()] = type;
}

const StructTypeBase* TypeRegistry::get(const std::string& name) {
return find_or(impl()->type_map, name, nullptr);
}
27 changes: 27 additions & 0 deletions type_registry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#ifndef TYPE_REGISTRY_HPP_LPQGF8DT
#define TYPE_REGISTRY_HPP_LPQGF8DT

#include "object.hpp"
#include <string>

class TypeRegistry {
public:
template <typename T>
static void add();
static void add(const StructTypeBase* type);

static const StructTypeBase* get(const std::string& name);
private:
TypeRegistry();
struct Impl;
static Impl* impl();
};

template <typename T>
void TypeRegistry::add() {
add(get_type<T>());
}


#endif /* end of include guard: TYPE_REGISTRY_HPP_LPQGF8DT */

0 comments on commit b88b419

Please sign in to comment.