Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

c++ - C++0x: How can I access variadic tuple members by index at runtime? -


i have written following basic tuple template:

template <typename... t> class tuple;  template <uintptr_t n, typename... t> struct tupleindexer;  template <typename head, typename... tail> class tuple<head, tail...> : public tuple<tail...> {      private:     head element;      public:     template <uintptr_t n>     typename tupleindexer<n, head, tail...>::type& get() {         return tupleindexer<n, head, tail...>::get(*this);     }      uintptr_t getcount() const {         return sizeof...(tail) + 1;     }      private:     friend struct tupleindexer<0, head, tail...>;  };  template <> class tuple<> {      public:     uintptr_t getcount() const {         return 0;     }  };  template <typename head, typename... tail> struct tupleindexer<0, head, tail...> {      typedef head& type;      static type get(tuple<head, tail...>& tuple) {         return tuple.element;     }  };  template <uintptr_t n, typename head, typename... tail> struct tupleindexer<n, head, tail...> {      typedef typename tupleindexer<n - 1, tail...>::type type;      static type get(tuple<head, tail...>& tuple) {         return tupleindexer<n - 1, tail...>::get(*(tuple<tail...>*) &tuple);     }  }; 

it works fine, , can access elements in array-like fashion using tuple.get<index>() - can if know index @ compile-time. however, need access elements in tuple index @ runtime, , won't know @ compile-time index needs accessed. example:

int chosenindex = getuserinput(); void* chosenelement = tuple.get(chosenindex); cout << "the option chose was: " << ((myabstractbaseclass*) chosenelement)->getinfo() << endl; 

what's best way this?

edit:

hackish solution below:

okay, i've got idea. figured out 1 way of doing before posted question, hackish , produced warnings. since solution isn't forthcoming right away, maybe guys me improve hackish one. :-)

the tuple can't accessed array because elements not of same size. (hence array-style multiplication arrive @ correct offset in class structure not help.) however, managed work around creating static table contains list of offsets tuple. here's complete tuple , related templates:

#include <cstddef>  template <typename... t> class tuple;  template <uintptr_t n, typename... t> struct tupleindexer;  template <typename... t> struct tupleoffsets;  template <typename head, typename... tail> struct tupleoffsets<head, tail...> {      tupleoffsets() { init(offsets); }     static void init(uintptr_t* offsets);     uintptr_t const& operator[] (uintptr_t i) const { return offsets[i]; }      private:     uintptr_t offsets[sizeof...(tail) + 1];  };  template <typename head, typename... tail> void tupleoffsets<head, tail...>::init(uintptr_t* offsets) {      typedef tuple<head, tail...> type;      *offsets = offsetof(type, element);     tupleoffsets<tail...>::init(++offsets);  }  template <> struct tupleoffsets<> {      tupleoffsets() {}     static void init(uintptr_t* offsets) {}  };  template <typename head, typename... tail> class tuple<head, tail...> : public tuple<tail...> {      private:     head element;      public:     void* get(uintptr_t i) {         return (uint8_t*) + offsets[i];     }      template <uintptr_t n>     typename tupleindexer<n, head, tail...>::type& get() {         return tupleindexer<n, head, tail...>::get(*this);     }      uintptr_t getcount() const {         return sizeof...(tail) + 1;     }      private:     static const tupleoffsets<head, tail...> offsets;      friend struct tupleoffsets<head, tail...>;     friend struct tupleindexer<0, head, tail...>;  };  template <typename head, typename... tail> const tupleoffsets<head, tail...> tuple<head, tail...>::offsets;  template <> class tuple<> {      public:     uintptr_t getcount() const {         return 0;     }  };  template <typename head, typename... tail> struct tupleindexer<0, head, tail...> {      typedef head& type;      static type get(tuple<head, tail...>& tuple) {         return tuple.element;     }  };  template <uintptr_t n, typename head, typename... tail> struct tupleindexer<n, head, tail...> {      typedef typename tupleindexer<n - 1, tail...>::type type;      static type get(tuple<head, tail...>& tuple) {         return tupleindexer<n - 1, tail...>::get(*(tuple<tail...>*) &tuple);     }  }; 

in practice works. however, compiler gives me warning using offsetof on non-pod data type, , i'm not sure how portable solution is. know how might improve solution?

do this:

namespace detail {     template <std::size_t i, typename r, typename tuple, typename func>     r select(tuple&& ptuple, func pfunc)     {         return pfunc(get<i>(std::forward<tuple>(ptuple)));     }      template <std::size_t i, typename r, typename tuple, typename func>     r select_element(tuple&& ptuple, std::size_t pindex, func pfunc)     {         if (pindex == i)             return select<i, r>(std::forward<tuple>(ptuple), pfunc);         else             return select<i + 1, r>(std::forward<tuple>(ptuple), pindex, pfunc);     } }  template <typename tuple, typename func> r select(tuple&& ptuple, std::size_t pindex, func pfunc) {     typedef typename std::remove_reference<tuple>::type tuple_type;      // assumes possible calls func return same type     typedef typename std::tuple_element<0, tuple_type>::type dummy_type;     typedef typename std::result_of<func, dummy_type>::type result_type;      if (pindex >= std::tuple_size<tuple_type>::value)         throw std::out_of_range("select out of range");      return detail::select<0, result_type>(                                     std::forward<tuple>(ptuple), pindex, pfunc); } 

this lets call functor run-time selected element, checking each index incrementally. returns whatever function call returns, assumes invocations result in same type. (though right now, it'll "work" long invocations happen implicitly convertible same type invocation of first element. can assert match if want to, that's outside scope of question.)

i'd surprised if compiler didn't unroll it, don't know certain. in case, it's simple , works (well, untested, assume does) , that's far more important.

so whatever wanted run-time selected element, operate on this. can make call templated:

struct print_element {     // t determined @ compile time each possible element type,     // overload gets selected determined @ run-time     template <typename t>     void operator()(const t& px) const     {         std::cout << px << std::endl;     } }; 

if want value type, can make simple functor:

namespace detail {     template <typename r>     struct get_element     {         template <typename t>         r operator()(t&& pvalue) const         {             return std::forward<t>(pvalue);         }     }; }  template <typename r, typename tuple> r get(tuple&& ptuple, std::size_t pindex) {    return select(std::forward<tuple>(ptuple), pindex, get_element<r>()); } 

you can use this:

auto x = get<boost::any>(mytuple, i); 

to void*'s (yuck), need 1 last simple utility (too bad don't polymorphic lambda's):

class get_address { public:     template <typename t>     get_address(t& pvalue) :     mresult(&pvalue)     {}      void* get() const     {         return mresult;     }      operator void*() const     {         return get();     }  private:     void* mresult; }; 

allowing:

void* addr = get<get_address>(mytuple, i); 

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -