Merge pull request #3648 from SpecLad:get_policy-ts
This commit is contained in:
commit
c1252a1089
@ -150,13 +150,27 @@ SMALL_POLICY(bool);
|
|||||||
|
|
||||||
#undef SMALL_POLICY
|
#undef SMALL_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;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
typename choose_policy<T>::type SinglePolicy<T>::policy;
|
||||||
|
|
||||||
/// This function will return a different policy for each type.
|
/// This function will return a different policy for each type.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
base_any_policy* get_policy()
|
inline base_any_policy* SinglePolicy<T>::get_policy() { return &policy; }
|
||||||
{
|
|
||||||
static typename choose_policy<T>::type policy;
|
|
||||||
return &policy;
|
|
||||||
}
|
|
||||||
} // namespace anyimpl
|
} // namespace anyimpl
|
||||||
|
|
||||||
struct any
|
struct any
|
||||||
@ -170,26 +184,26 @@ public:
|
|||||||
/// Initializing constructor.
|
/// Initializing constructor.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
any(const T& x)
|
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);
|
assign(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Empty constructor.
|
/// Empty constructor.
|
||||||
any()
|
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.
|
/// Special initializing constructor for string literals.
|
||||||
any(const char* x)
|
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);
|
assign(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy constructor.
|
/// Copy constructor.
|
||||||
any(const any& x)
|
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);
|
assign(x);
|
||||||
}
|
}
|
||||||
@ -214,7 +228,7 @@ public:
|
|||||||
any& assign(const T& x)
|
any& assign(const T& x)
|
||||||
{
|
{
|
||||||
reset();
|
reset();
|
||||||
policy = anyimpl::get_policy<T>();
|
policy = anyimpl::SinglePolicy<T>::get_policy();
|
||||||
policy->copy_from_value(&x, &object);
|
policy->copy_from_value(&x, &object);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -269,7 +283,7 @@ public:
|
|||||||
void reset()
|
void reset()
|
||||||
{
|
{
|
||||||
policy->static_delete(&object);
|
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.
|
/// Returns true if the two types are the same.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user