Skip to content

Commit

Permalink
Merge branch 'types'
Browse files Browse the repository at this point in the history
  • Loading branch information
photm5 committed Oct 29, 2014
2 parents 60c185e + 4f5fa57 commit 967983f
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 517 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O1" )
# Include headers from inc
include_directories ( ${CMAKE_SOURCE_DIR}/inc )

# Add the two subdirectories
add_subdirectory ( ${CMAKE_SOURCE_DIR}/src )
# Add the test subdirectory
add_subdirectory ( ${CMAKE_SOURCE_DIR}/tests )

file ( GLOB_RECURSE EVENTSYSTEM_HEADERS ${CMAKE_SOURCE_DIR}/inc *.(h|hpp) )
Expand Down
92 changes: 0 additions & 92 deletions inc/engine/types/Dict.h

This file was deleted.

110 changes: 110 additions & 0 deletions inc/engine/types/Dynamic_union.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* eventsystem, A generic C++ eventsystem library utilizing templates
Copyright (C) 2014 firecoders
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#ifndef ENGINE_TYPES_DYNAMIC_UNION_GUARD
#define ENGINE_TYPES_DYNAMIC_UNION_GUARD

#include <memory>
#include <string>
#include <functional>

namespace engine
{
namespace types
{
template < typename T >
struct Type_description {};

class Dynamic_union
{
public:
template < typename T >
Dynamic_union ( T value );

std::string get_type () const;
bool operator== ( const Dynamic_union& other ) const;
bool operator< ( const Dynamic_union& other ) const;

template < typename T >
T get () const;

private:
typedef std::function < bool ( const Dynamic_union&, const Dynamic_union& ) > Compare_function;

std::string type;
std::shared_ptr < void > value;
Compare_function operator_equals;
Compare_function operator_less;

void check_type ( std::string type ) const;
};

template < typename T >
Dynamic_union::Dynamic_union ( T val ) :
type ( Type_description < T >::type_string ),
value ( std::make_shared < T > ( val ) ),
operator_equals ( Compare_function ( Type_description < T >::operator_equals ) ),
operator_less ( Compare_function ( Type_description < T >::operator_less ) )
{}

std::string Dynamic_union::get_type () const
{
return type;
}

bool Dynamic_union::operator== ( const Dynamic_union& other ) const
{
if ( get_type () != other.get_type () )
{
return false;
}
return operator_equals ( *this, other );
}

bool Dynamic_union::operator< ( const Dynamic_union& other ) const
{
if ( get_type () != other.get_type () )
{
return get_type () < other.get_type ();
}
return operator_less ( *this, other );
}

template < typename T >
T Dynamic_union::get () const
{
check_type ( Type_description < T >::type_string );
return * ( ( T* ) value.get () );
}

void Dynamic_union::check_type ( std::string type ) const
{
if ( get_type () != type )
{
throw std::logic_error ( "Cannot retrieve " + type +
" from a Dynamic_union whose type is " + get_type () + "." );
}
}

} /* namespace types */
} /* namespace engine */

#endif // ENGINE_TYPES_DYNAMIC_UNION_GUARD
29 changes: 9 additions & 20 deletions inc/engine/types/Index_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ namespace engine
public:
Index_node ();

// get_descendant is not const, because it creates and attaches
// a new Index_node if it doesn't already exist
Index_node < Wrapped, Key >& get_descendant ( std::vector < Key > keys );
template < typename Iterator >
Index_node < Wrapped, Key >& get_descendant ( Iterator keys_begin, Iterator keys_end );
Index_node < Wrapped, Key >& operator[] ( Key key );

std::shared_ptr < Wrapped > get_wrapped () const;
void set_wrapped ( std::shared_ptr < Wrapped > wrapped );
Expand All @@ -60,26 +56,19 @@ namespace engine
{}

template < typename Wrapped, typename Key >
Index_node < Wrapped, Key >& Index_node < Wrapped, Key >::get_descendant ( std::vector < Key > keys )
Index_node < Wrapped, Key >& Index_node < Wrapped, Key >::operator[] ( Key key )
{
return get_descendant ( keys.begin (), keys.end () );
}

template < typename Wrapped, typename Key >
template < typename Iterator >
Index_node < Wrapped, Key >& Index_node < Wrapped, Key >::get_descendant ( Iterator keys_begin, Iterator keys_end )
{
if ( keys_begin == keys_end )
auto iterator = children.find ( key );
if ( iterator == children.end () )
{
return *this;
auto node = std::make_shared < Index_node < Wrapped, Key > > ();
children.insert ( { key, node } );
return * node;
}
auto iterator = children.find ( *keys_begin );
if ( iterator == children.end () )
else
{
children.insert ( { *keys_begin, std::make_shared < Index_node < Wrapped, Key > > () } );
return * iterator->second;
}
auto other = children.at ( *keys_begin );
return other->get_descendant ( ++keys_begin, keys_end );
}

template < typename Wrapped, typename Key >
Expand Down
91 changes: 91 additions & 0 deletions inc/engine/types/Type_descriptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* eventsystem, A generic C++ eventsystem library utilizing templates
Copyright (C) 2014 firecoders
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#ifndef ENGINE_TYPES_TYPE_DESCRIPTIONS_GUARD
#define ENGINE_TYPES_TYPE_DESCRIPTIONS_GUARD

#include <string>
#include <memory>

namespace engine
{
namespace types
{
template <>
struct Type_description < bool >
{
static constexpr const char* type_string = "bool";
static bool operator_equals ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < bool > () == rhs.get < bool > ();
}
static bool operator_less ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < bool > () < rhs.get < bool > ();
}
};

template <>
struct Type_description < int >
{
static constexpr const char* type_string = "int";
static bool operator_equals ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < int > () == rhs.get < int > ();
}
static bool operator_less ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < int > () < rhs.get < int > ();
}
};

template <>
struct Type_description < double >
{
static constexpr const char* type_string = "double";
static bool operator_equals ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < double > () == rhs.get < double > ();
}
static bool operator_less ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < double > () < rhs.get < double > ();
}
};

template <>
struct Type_description < std::string >
{
static constexpr const char* type_string = "std::string";
static bool operator_equals ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < std::string > () == rhs.get < std::string > ();
}
static bool operator_less ( const Dynamic_union& lhs, const Dynamic_union& rhs )
{
return lhs.get < std::string > () < rhs.get < std::string > ();
}
};

} /* namespace types */
} /* namespace engine */

#endif // ENGINE_TYPES_TYPE_DESCRIPTIONS_GUARD
7 changes: 0 additions & 7 deletions src/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 967983f

Please sign in to comment.