libcruft-util/introspection.hpp

207 lines
5.7 KiB
C++
Raw Normal View History

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_INTROSPECTION_HPP
#define __UTIL_INTROSPECTION_HPP
#include "variadic.hpp"
#include <cstddef>
#include <string>
#include <tuple>
namespace util {
template <
typename T
>
struct type_string {
// static const std::string value
};
template <> struct type_string<bool> { static constexpr const char value[] = "bool"; };
template <> struct type_string< int8_t> { static constexpr const char value[] = "int8"; };
template <> struct type_string< int16_t> { static constexpr const char value[] = "int16"; };
template <> struct type_string< int32_t> { static constexpr const char value[] = "int32"; };
template <> struct type_string< int64_t> { static constexpr const char value[] = "int64"; };
template <> struct type_string< uint8_t> { static constexpr const char value[] = "uint8"; };
template <> struct type_string<uint16_t> { static constexpr const char value[] = "uint16"; };
template <> struct type_string<uint32_t> { static constexpr const char value[] = "uint32"; };
template <> struct type_string<uint64_t> { static constexpr const char value[] = "uint64"; };
template <> struct type_string<float > { static constexpr const char value[] = "float32"; };
template <> struct type_string<double > { static constexpr const char value[] = "float64"; };
template <typename T>
auto
to_string (void)
{
return type_string<T>::value;
}
///////////////////////////////////////////////////////////////////////////
/// Lists valid values of an enumeration
///
/// E: enumeration type
template <
typename E
>
struct enum_values {
/// Specialisations must provide the following constexpr:
///
/// value_type: typename
/// value_count: size_t
/// values: static const std::array<value_type,value_count>
};
///////////////////////////////////////////////////////////////////////////
/// Describes a single member variable in a type availabe for introspection
///
/// K: target class
/// R: member type
/// M: pointer-to-member
template <
class K,
typename R,
R K::*M
>
struct field
{
typedef K klass;
typedef R type;
static const std::string name;
static const R& get (const K &k) { return k.*M; }
static R get (K &k) { return k.*M; }
};
///////////////////////////////////////////////////////////////////////////
/// Holds the fields of a type available for introspection
///
/// Specialise the following type struct with a 'fields' tuple of the
/// members that should be accessed like below:
///
/// struct foo { int a; int b; };
///
/// template <> struct type<foo>
/// {
/// typedef std::tuple<
/// field<foo,int,&foo::a>,
/// field<foo,int,&foo::b>
/// > fields;
/// };
///
/// template <> const std::string field<foo,int,&foo::a>::name = "a";
/// template <> const std::string field<foo,int,&foo::b>::name = "b";
template <class K>
struct type { };
///////////////////////////////////////////////////////////////////////////
/// traits class which converts an introspected type to a tuple
///
/// K: target class
template <typename K>
struct type_tuple;
template <
typename ...T
> struct type_tuple<
std::tuple<T...>
> {
typedef std::tuple<T...> type;
};
template <
typename K,
typename I = typename make_indices<
std::tuple_size<
typename type<K>::fields
>::value
>::type
> struct _type_tuple;
template <
typename K,
size_t ...I
> struct _type_tuple <
K,
indices<I...>
> {
typedef std::tuple<
typename std::tuple_element<
I,
typename type<K>::fields
>::type::type...
> type;
};
template <
typename K
> struct type_tuple {
typedef typename _type_tuple<K>::type type;
};
///////////////////////////////////////////////////////////////////////////
namespace detail {
template <
typename K,
typename I = typename make_indices<
std::tuple_size<
typename type<K>::fields
>::value
>::type
>
struct _as_tuple;
template <
typename K,
size_t ...I
>
struct _as_tuple <
K,
indices<I...>
>
{
static typename type_tuple<K>::type
make (const K &k)
{
return std::make_tuple (
std::tuple_element<I, typename type<K>::fields>::type::get (k)...
);
}
};
}
/// Convert an introspection capable class instance into a tuple instance
///
/// K: source class
template <typename K>
auto
as_tuple (const K &k)
{
return detail::_as_tuple<K>::make (k);
}
}
#endif