Skip to content

Commit

Permalink
refactored type name mapping
Browse files Browse the repository at this point in the history
type names are now mapped using a static lookup table; all built-in types
information instances are now members of uniform_type_info_map to get rid
of as much heap allocations as possible
  • Loading branch information
Neverlord committed May 5, 2013
1 parent 31699f7 commit 488ca97
Show file tree
Hide file tree
Showing 19 changed files with 939 additions and 896 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ set(LIBCPPA_SRC
src/context_switching_actor.cpp
src/continuable_reader.cpp
src/continuable_writer.cpp
src/decorated_names_map.cpp
src/default_actor_addressing.cpp
src/default_actor_proxy.cpp
src/default_peer.cpp
Expand Down Expand Up @@ -154,6 +153,7 @@ set(LIBCPPA_SRC
src/to_uniform_name.cpp
src/unicast_network.cpp
src/uniform_type_info.cpp
src/uniform_type_info_map.cpp
src/weak_ptr_anchor.cpp
src/yield_interface.cpp)

Expand Down
7 changes: 3 additions & 4 deletions cppa.files
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ cppa/detail/behavior_impl.hpp
cppa/detail/behavior_stack.hpp
cppa/detail/boxed.hpp
cppa/detail/container_tuple_view.hpp
cppa/detail/decorated_names_map.hpp
cppa/detail/decorated_tuple.hpp
cppa/detail/default_uniform_type_info_impl.hpp
cppa/detail/demangle.hpp
Expand Down Expand Up @@ -122,6 +121,7 @@ cppa/option.hpp
cppa/partial_function.hpp
cppa/primitive_type.hpp
cppa/primitive_variant.hpp
cppa/prioritizing.hpp
cppa/process_information.hpp
cppa/qtsupport/actor_widget_mixin.hpp
cppa/receive.hpp
Expand All @@ -131,6 +131,7 @@ cppa/sb_actor.hpp
cppa/scheduled_actor.hpp
cppa/scheduler.hpp
cppa/self.hpp
cppa/send.hpp
cppa/serializer.hpp
cppa/singletons.hpp
cppa/spawn_options.hpp
Expand Down Expand Up @@ -227,7 +228,6 @@ src/channel.cpp
src/context_switching_actor.cpp
src/continuable_reader.cpp
src/continuable_writer.cpp
src/decorated_names_map.cpp
src/default_actor_addressing.cpp
src/default_actor_proxy.cpp
src/default_peer.cpp
Expand Down Expand Up @@ -283,6 +283,7 @@ src/thread_pool_scheduler.cpp
src/to_uniform_name.cpp
src/unicast_network.cpp
src/uniform_type_info.cpp
src/uniform_type_info_map.cpp
src/weak_ptr_anchor.cpp
src/yield_interface.cpp
unit_testing/ping_pong.cpp
Expand All @@ -306,5 +307,3 @@ unit_testing/test_sync_send.cpp
unit_testing/test_tuple.cpp
unit_testing/test_uniform_type.cpp
unit_testing/test_yield_interface.cpp
cppa/prioritizing.hpp
cppa/send.hpp
61 changes: 0 additions & 61 deletions cppa/detail/decorated_names_map.hpp

This file was deleted.

3 changes: 0 additions & 3 deletions cppa/detail/singleton_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class empty_tuple;
class group_manager;
class abstract_tuple;
class actor_registry;
class decorated_names_map;
class uniform_type_info_map;

class singleton_manager {
Expand Down Expand Up @@ -79,8 +78,6 @@ class singleton_manager {

static empty_tuple* get_empty_tuple();

static decorated_names_map* get_decorated_names_map();

static opencl::command_dispatcher* get_command_dispatcher();

private:
Expand Down
84 changes: 60 additions & 24 deletions cppa/detail/uniform_type_info_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,93 @@
#define CPPA_UNIFORM_TYPE_INFO_MAP_HPP

#include <set>
#include <map>
#include <string>
#include <utility> // std::pair
#include <utility>
#include <type_traits>

#include "cppa/cppa_fwd.hpp"

#include "cppa/util/duration.hpp"
#include "cppa/util/type_list.hpp"

#include "cppa/detail/singleton_mixin.hpp"
#include "cppa/detail/default_uniform_type_info_impl.hpp"

namespace cppa { class uniform_type_info; }

namespace cppa { namespace detail {

using mapped_type_list = util::type_list<
bool,
any_tuple,
atom_value,
actor_ptr,
channel_ptr,
group_ptr,
process_information_ptr,
message_header,
std::nullptr_t,
util::duration,
util::void_type,
double,
float,
long double,
std::string,
std::u16string,
std::u32string,
std::map<std::string,std::string>
>;

using zipped_type_list = util::tl_zip_with_index<mapped_type_list>::type;

// lookup table for built-in types
extern const char* mapped_type_names[][2];

template<typename T>
constexpr const char* mapped_name() {
return mapped_type_names[util::tl_index_of<zipped_type_list,T>::value][1];
}

const char* mapped_name_by_decorated_name(const char* decorated_type_name);

// lookup table for integer types
extern const char* mapped_int_names[][2];

template<typename T>
constexpr const char* mapped_int_name() {
return mapped_int_names[sizeof(T)][std::is_signed<T>::value ? 1 : 0];
}

class uniform_type_info_map_helper;

// note: this class is implemented in uniform_type_info.cpp
class uniform_type_info_map : public singleton_mixin<uniform_type_info_map> {
class uniform_type_info_map {

friend class uniform_type_info_map_helper;
friend class singleton_mixin<uniform_type_info_map>;

public:

typedef std::set<std::string> set_type;
typedef std::map<std::string, uniform_type_info*> uti_map_type;
typedef std::map<int, std::pair<set_type, set_type> > int_map_type;
typedef const uniform_type_info* pointer;

inline const int_map_type& int_names() const {
return m_ints;
}
virtual ~uniform_type_info_map();

const uniform_type_info* by_raw_name(const std::string& name) const;
virtual pointer by_uniform_name(const std::string& name) const = 0;

const uniform_type_info* by_uniform_name(const std::string& name) const;
virtual pointer by_rtti(const std::type_info& ti) const = 0;

std::vector<const uniform_type_info*> get_all() const;
virtual std::vector<pointer> get_all() const = 0;

// NOT thread safe!
bool insert(const std::set<std::string>& raw_names, uniform_type_info* uti);

private:

// maps raw typeid names to uniform type informations
uti_map_type m_by_rname;
virtual bool insert(uniform_type_info* uti) = 0;

// maps uniform names to uniform type informations
uti_map_type m_by_uname;
static uniform_type_info_map* create_singleton();

// maps sizeof(-integer_type-) to { signed-names-set, unsigned-names-set }
int_map_type m_ints;
inline void dispose() { delete this; }

uniform_type_info_map();
inline void destroy() { delete this; }

~uniform_type_info_map();
virtual void initialize() = 0;

};

Expand Down
4 changes: 0 additions & 4 deletions cppa/singletons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ inline detail::empty_tuple* get_empty_tuple() {
return detail::singleton_manager::get_empty_tuple();
}

inline detail::decorated_names_map* get_decorated_names_map() {
return detail::singleton_manager::get_decorated_names_map();
}

} // namespace cppa

#endif // CPPA_SINGLETONS_HPP
32 changes: 7 additions & 25 deletions cppa/uniform_type_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ class uniform_type_info {
*/
static std::vector<const uniform_type_info*> instances();

/**
* @brief Get the internal @p libcppa name for this type.
* @returns A string describing the @p libcppa internal type name.
*/
inline const std::string& name() const { return m_name; }

/**
* @brief Creates an object of this type.
*/
Expand All @@ -202,6 +196,12 @@ class uniform_type_info {
*/
object deserialize(deserializer* source) const;

/**
* @brief Get the internal @p libcppa name for this type.
* @returns A string describing the @p libcppa internal type name.
*/
virtual const char* name() const = 0;

/**
* @brief Determines if this uniform_type_info describes the same
* type than @p tinfo.
Expand Down Expand Up @@ -234,23 +234,9 @@ class uniform_type_info {
*/
virtual void deserialize(void* instance, deserializer* source) const = 0;

/**
* @brief Checks wheter <tt>src->seek_object()</tt>
* returns @p tname and throws an exception if not.
* @throws std::logic_error
*/
static void assert_type_name(deserializer* src, const std::string& tname);

protected:

/**
* @brief Checks wheter <tt>src->seek_object()</tt>
* returns {@link name()} and throws an exception if not.
* @throws std::logic_error
*/
void assert_type_name(deserializer* src) const;

uniform_type_info(const std::string& uniform_name);
uniform_type_info() = default;

/**
* @brief Casts @p instance to the native type and deletes it.
Expand All @@ -270,10 +256,6 @@ class uniform_type_info {
*/
virtual void* new_instance(const void* instance = nullptr) const = 0;

private:

std::string m_name;

};

/**
Expand Down
Loading

0 comments on commit 488ca97

Please sign in to comment.