Skip to content

Commit

Permalink
Fixed getPolicy() method to make it thread-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
akarsakov committed Mar 14, 2014
1 parent fd8f2c8 commit 8910508
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions modules/flann/include/opencv2/flann/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,27 @@ SMALL_POLICY(bool);

#undef SMALL_POLICY

/// This function will return a different policy for each type.
template<typename T>
base_any_policy* get_policy()
template <typename T>
class SinglePolicy
{
SinglePolicy();
SinglePolicy(const SinglePolicy& other);
SinglePolicy& operator=(const SinglePolicy& other);

public:
static base_any_policy* get_policy();

private:
static typename choose_policy<T>::type policy;
return &policy;
}
};

template <typename T>
typename choose_policy<T>::type SinglePolicy<T>::policy;

/// This function will return a different policy for each type.
template <typename T>
inline base_any_policy* SinglePolicy<T>::get_policy() { return &policy; }

} // namespace anyimpl

struct any
Expand All @@ -175,26 +189,26 @@ struct any
/// Initializing constructor.
template <typename T>
any(const T& x)
: policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
{
assign(x);
}

/// Empty constructor.
any()
: policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
{ }

/// Special initializing constructor for string literals.
any(const char* x)
: policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
{
assign(x);
}

/// Copy constructor.
any(const any& x)
: policy(anyimpl::get_policy<anyimpl::empty_any>()), object(NULL)
: policy(anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy()), object(NULL)
{
assign(x);
}
Expand All @@ -219,7 +233,7 @@ struct any
any& assign(const T& x)
{
reset();
policy = anyimpl::get_policy<T>();
policy = anyimpl::SinglePolicy<T>::get_policy();
policy->copy_from_value(&x, &object);
return *this;
}
Expand Down Expand Up @@ -274,7 +288,7 @@ struct any
void reset()
{
policy->static_delete(&object);
policy = anyimpl::get_policy<anyimpl::empty_any>();
policy = anyimpl::SinglePolicy<anyimpl::empty_any>::get_policy();
}

/// Returns true if the two types are the same.
Expand Down

0 comments on commit 8910508

Please sign in to comment.