c++ - retrieving objects from boost::variant -
i tried ask question before, think way ask question not proper. tried again here: (still don't know subject appropriate)
first defined
typedef boost::variant<point, line, vertex> vec_variant; typedef std::vector<vec_variant> vec; the write function such depends on case returns point, line, vertex or combination of them.
vec my_func::select_t(const mesh::section& s, const char* model) const { vec new_vec; . . . . //loop on lines else if ( strcmp(model , "line") == 0 ) { for(section::lineiterator ite = s.beginline(); ite != s.endline(); ++ite ) { line ed = *ite; point p0 = ed.point(0); point p1 = ed.point(1); point p0_modified ( /* modification */ ); point p1_modified ( /* modification */ ); if( /* other conditions */ ) { new_vec.push_back(ed); new_vec.push_back(p0_modified); //note before pushing point new_vec.push_back(p1_modified); //first pushed line } else if ( /* other conditions */ ) { . . . vertex m = .......; new_vec.push_back(ed); new_vec.push_back(m); //note before pushing point //first pushed line } } } } return new_vec; } so @ end may have {ed, p0_modified, p0_modified, ed, m, ed, m, ed, p0_modified, p0_modified, ed, m, ....} {line,point,point,line,vertex,line,vertex,line,point,point,line,vertex, ...}
now call function in other part of code (different file)
first defined visitor: template<typename t> struct t_visitor : public boost::static_visitor<> { t_visitor(std::vector<t>& v) : zerovector(v) {} template<typename u> void operator () (const u&) {} void operator () (const t& value) { zerovector.push_back(value); } private: std::vector<t>& zerovector; }; i called above function here :
void func_2( /*......*/ ) { . //we can use above visitor store each type (point, line, vertex) in vactor<point or line or vertex) . //we not know new_vec @ end of loop. thing know after each line there . //would either 2 points or 1 vertex . const char *model = "edge"; . .//how find line ed , corresponded points? . create_line( point& p0_modified, point& p1_modified, line& ed); //two modified points , line . .//how find point m , corresponded line? . create_point( m(0), m(1), m(2), line& ed); //point m coordinates , line . . }
so data structure sequence of point, line, , vertex objects (i'm going assume surface in first sentence typo). know additional restriction line must followed either 2 points or 1 vertex. want use structure.
this possible, it's annoying. may recommend change data structure?
struct linewithpoints { line line; point p1, p2; }; struct linewithvertex { line line; vertex v; }; typedef boost::variant<linewithpoints, linewithvertex> vec_variant; typedef std::vector<vec_variant> vec; and change code produce sequence instead. working becomes trivial.
Comments
Post a Comment