what does this code trying to show?: #include<iostream> #define ENABLEDYNAMICLOCATION…
- User
what does this code trying to show?: #include #define ENABLE_DYNAMIC_LOCATION #include"align_memory_field.hpp" #include"static_string_formatter.hpp" #include"cli_print_def.hpp" #include #include
using namespace static_string_formatter;
struct X { char name[30]; int number; int salary; };
int main() { cli_print_color_def color_def{}; char data[sizeof(X)]; static_align_field<char[30], int, int> f{data}; for (auto& x : f) { std::cin >> x; } color_def.color(print_color::green); println(f.cast()->name, f.cast()->number, f.cast()->salary); std::ofstream outf("xiaoming.dat"); if (!outf.is_open()) { std::cerr << "open failed"; return -1; } outf.write(data, sizeof(X)); outf.close(); color_def.color(print_color::blue); memset(data, 0, sizeof(X)); std::ifstream inf("xiaoming.dat"); if (!inf.is_open()) { std::cerr << "open failed"; return -2; } inf.read(data, sizeof(X)); inf.close(); println(f.cast()->name, f.cast()->number, f.cast()->salary); }
- Assistant
- User
I can't provide you the whole implementation, but can you tell me more about its iterator through this operator reload:? //align data by specified alignment #ifdef ENABLE_DYNAMIC_LOCATION //the other parts of the class use static assert or concept to handle type exception in compile time //while this part, due to codes in runtime, provide dynamic exception handling, by disabling this part //you will completely disable dynamic exception handling, improve on performance, but index must be handle //carefully, still, although in runtime, this part still provides type check to prevent usual type mismatching template<std::size_t pack_size, class Self_destruct_determin_t, class ...Args> class align_memory_field::auto operator[](unsigned int idx)const { if (idx > Max_types_index) throw std::out_of_range("error: index overflow!");
using offset_array_set = typename meta_count<0, member_counts>::array; using meta_array_set = decltype(Member_offset_vector::offset_array(offset_array_set{})); using field_list = exp_if<Is_self_destructible, exp_list<Args...>, exp_list<Self_destruct_determin_t, Args...>>; using type_check_stream = meta_stream_o<member_counts, meta_replace_o, meta_istream<field_list>>; using type_check_looper = align_memory_helper::align_memory_iterator<while_constexpr<type_check_stream>>; return type_check_looper{base_ptr, idx, meta_array_set::array()[idx]}; } - Assistant
- User
I can not provide you the whole implementation, but can you provide more information from these snippet on how the first example work? template class align_memory_iterator:://define template for two-argument-like operations //type check, but allow std::i/ostream to pass it to apply output operation template<class F, class T> void transform(F&& f, T && value) { std::size_t recursion_count{ 0 }; auto invoke_func = [&recursion_count, &f, this](T && value) { using current_type = stream_final_t<types_mso>; if (recursion_count == this->index) { if constexpr ( std::is_convertible_v<T, current_type> || is_safe_to_invoke_with<T, current_type> || is_allowed_type ) { std::invoke(f, read_from_align_offset<unsigned char, current_type>(this->base_ptr, this->offset), std::forward(value)); } else { throw std::invalid_argument("Type mismatch: Cannot do this operation to this location."); } } ++recursion_count; }; type_check_looper{}.for_each(invoke_func, value); } ... template std::istream& operator>>(std::istream& ins, align_memory_helper::align_memory_iterator& loc) { loc.transform([](auto& val, std::istream& is) { is >> val; }, ins); return ins; }
- Assistant
- User
I really can't offer you the whole snippet because the source is too long, but can you tell me what these component does: template<std::size_t pack_size, class Self_destruct_determin_t, class ...Args> class align_memory_field{ private: ... //if first type is a functor, the first type should be ignore in offset calculation using Member_offset_vector = exp_if<Is_self_destructible, offset_vector<pack_size, Args...>, //true offset_vector<pack_size, Self_destruct_determin_t, Args...>//false
; } ....in another hpp template<class ...> struct offset_pattern {}; template<std::size_t pack_size, class ...typs> struct offset_vector { template<std::size_t I> using iterator = align_offset_iterator< align_offset_ptr<pack_size, 0, 0>, offset_pattern<typs...>, I>;
template<std::size_t I> static constexpr std::size_t offset_v = iterator<I>::get::offset_v; template<std::size_t I> using value_type = typename iterator<I>::value_type; //get specified indices by any other struct like std::index_sequence or meta_traits::meta_array template<template<std::size_t ...> class indices_container, std::size_t...I> static constexpr auto offset_array(indices_container<I...>) { return indices_container<offset_v<I>...>{}; }};
- Assistant
- User
So you mean the whole calculation of offset is meta programming , does that mean that when the program actually start, the class align memory field already know exactly the offset of its member? this is unbelievable
- Assistant
- User
this is why I was confused before, because I didn't see any of the offset calculation in its implementation, while it works correctly...
- Assistant
- User
I literally know little about meta programming so I hope you'd further explain these: namespace calc_detail { struct make_align { template<class this_align, class T> using apply = std::conditional_t< (alignof(T) > this_align::value), Idx<alignof(T)>, this_align>; };
using align_calc_ostream = meta_object<Idx<0>, make_align>; template<class TL> using decl_align_t = meta_all_transfer< align_calc_ostream, meta_istream<TL> >::to::type;}
//tool for alignment calculate template<class...Args> constexpr std::size_t decl_align = calc_detail::decl_align_t<exp_list<Args...>>::value; using do_nothing_t = decltype([](unsigned char* p) {}); template<class ...Typs> using static_align_field = align_memory_field<decl_align<Typs...>, do_nothing_t, Typs...>;
- Assistant
- User
I am confused, sorry, I really lack of the knowledge of cpp meta programming, but wasn't it constant and unchanged, how could it manifest a stream with metaprogramming, a stream needs update, cache, its dynamic..
- Assistant
- User
no, you misunderstand I was asking about the decl_align part, your mention, and its name suggests that this components seems to be stream-like object, but it is templates, so I'm confused
- Assistant
- User
I look deeper into the lib and found some related, unfortunately, i understand nothing of these, but I think they are relative, right?: //define meta_stream object //meta stream contains an input stream (From) and an output stream (To) //argument with macro MMO, it requires an meta object //any meta object can be a MMO(To), but only a meta_ret_object can be a MMO(From) template<class MMO(To), class MMO(From)> struct meta_stream { using from = MMO(From); using to = MMO(To); using cache = typename MMO(From)::ret; using update = meta_stream< meta_invoke< invoke_object_if<(exp_size<typename MMO(From)::type> > 0)>, MMO(To), MMO(From) >, meta_invoke<MMO(From)> >; }; struct meta_stream_f { template<class mo_stream, class...> using apply = typename mo_stream::update; };
- Assistant
- User
I've found an example of the meta_stream from its github pages, can you explain: #include #include #include"meta_object_traits.hpp"
using namespace meta_typelist;
//the meta_stream try to prevent you from constructing extra templates //the following example showcase a meta_insert within a function without extra declaration template<std::size_t I, class T, class ...Args> auto insert_tuple(std::tuple<Args...> const& tp, T const& value) { //note that the return of stream transfer is a stream with current states using transformed_stream = transfer<I + 1, meta_ostream<exp_list<>>, meta_istream<exp_list<Args...>>>; //each i/o stream is a fully meta object and can be invoke independently //invoke an meta_ostream will add a type to the typelist it holds using os = meta_invoke<typename transformed_stream::to, T>; using final_list = typename meta_all_transfer<os, typename transformed_stream::from>::to::type; using new_tuple_type = final_list::template tostd::tuple;
new_tuple_type ntp{}; while_meta_stream<meta_displayer, meta_index_istream<final_list::length>>::for_each([tp, &ntp, value]<class Idx_t>{ constexpr auto pos = Idx_t::value; if constexpr (pos < I) { std::get<pos>(ntp) = std::get<pos>(tp); } else if constexpr (pos == I + 1) { std::get<pos>(ntp) = value; } else { std::get<pos>(ntp) = std::get<pos - 1>(tp); } }); return ntp; - Assistant