Skip to content

Commit

Permalink
fix as_arrays for variable_access_const
Browse files Browse the repository at this point in the history
previously the code
config c;
variable_access_const("a[1]", c).as_array();
would give an segfault.
now we return a range containing one empty element.
  • Loading branch information
gfgtdf committed Jul 6, 2014
1 parent 3c01715 commit 68f8652
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/variable_info.cpp
Expand Up @@ -20,6 +20,8 @@
*/
#include "variable_info.hpp"
#include "game_config.hpp"
#include "config_assign.hpp"

using namespace variable_info_3_detail;

/// general helpers
Expand Down Expand Up @@ -293,26 +295,57 @@ namespace
/// range_based operations
namespace {


template<const variable_info_3_type vit, typename THandler>
class as_range_visitor_base
class as_range_visitor_base2
: public variable_info_visitor_const<vit, typename THandler::result_type>
{
public:
as_range_visitor_base(const THandler& handler) : handler_(handler) {}
typename as_range_visitor_base::result_type from_named(typename as_range_visitor_base::param_type state) const
as_range_visitor_base2(const THandler& handler) : handler_(handler) {}
typename as_range_visitor_base2::result_type from_named(typename as_range_visitor_base2::param_type state) const
{
return handler_(*state.child_, state.key_, 0, state.child_->child_count(state.key_));
}
typename as_range_visitor_base::result_type from_indexed(typename as_range_visitor_base::param_type state) const
typename as_range_visitor_base2::result_type from_indexed(typename as_range_visitor_base2::param_type state) const
{
//Ensure we have a config at the given explicit position.
get_child_at<vit>(*state.child_, state.key_, state.index_);
return handler_(*state.child_, state.key_, state.index_, state.index_ + 1);
return this->handler_(*state.child_, state.key_, state.index_, state.index_ + 1);
}
private:
protected:
const THandler& handler_;
};

template<const variable_info_3_type vit, typename THandler>
class as_range_visitor_base
: public as_range_visitor_base2<vit, THandler>
{
public:
as_range_visitor_base(const THandler& handler) : as_range_visitor_base2<vit, THandler>(handler) {}
//inherit all from as_range_visitor_base2
};

const config non_empty_const_cfg = config_of("_", config());

//we cannot partly specialise methods so we are using inheritance.
template<typename THandler>
class as_range_visitor_base<vit_const, THandler>
: public as_range_visitor_base2<vit_const, THandler>
{
public:
as_range_visitor_base(const THandler& handler) : as_range_visitor_base2<vit_const, THandler>(handler) {}

typename as_range_visitor_base::result_type from_indexed(typename as_range_visitor_base::param_type state) const
{
//calling get_child_at<vit>(*state.child_, state.key_, state.index_) like above would have no effect
if(int(state.child_->child_count(state.key_)) < state.index_)
{
return this->handler_(non_empty_const_cfg, "_", 0, 1);
}
return this->handler_(*state.child_, state.key_, state.index_, state.index_ + 1);
}
};

template<const variable_info_3_type vit>
class variable_as_array_h
{
Expand Down

0 comments on commit 68f8652

Please sign in to comment.