Pattern to share data between objects in C++ -
i have started migration of high energy physics algorithm written in fortran object oriented approach in c++. fortran code uses lot of global variables across lot of functions.
i have simplified global variables set of input variables, , set of invariants (variables calculated once @ beginning of algorithm , used functions).
also, have divided full algorithm 3 logical steps, represented 3 different classes. so, in simple way, have this:
double calculatefactor(double x, double y, double z) { invariantstypea inva(); invariantstypeb invb(); // need x, y , z inva.calculatevalues(); invb.calculatevalues(); step1 s1(); step2 s2(); step3 s3(); // need x, y, z, inva , invb return s1.eval() + s2.eval() + s3.eval(); } my problem is:
- for doing calculations
invariantstypex,stepxobjects need input parameters (and these not three). - the 3 objects
s1,s2,s3need data ofinva,invbobjects. - all classes use several other classes through composition job, , classes need input , invariants (by example,
s1has member objectthetaof classthetamatrixneedsx,z,invbconstructed). - i cannot rewrite algorithm reduce global values, because follows several high energy physics formulas, , formulas that.
is there pattern share input parameters , invariants objects used calculate result?
should use singletons? (but calculatefactor function evaluated around million of times)
or should pass required data arguments objects when created?(but if data passed everywhere in every member object of every class, creating mess)
thanks.
well, in c++ suitable solution, given constraints , conditions, represented pointers. many developers told use boost::shared_ptr. not necessary, although provides better performance when considering portability , robustness system faults.
it not necessary bind boost. true not compiled , standardization processes lead c++ boost directly integrated standard library, if not want use external library can.
so let's go , try solve problem using c++ , provides actually.
you'll have main method , there, told before, initialize invariants elements... have constants , can every possible type. no need make them constant if want, however, in main instantiate invariant elements , point them components requiring usage. first in separate file called "common_components.hpp" consider following (i assume need types invariant variables):
typedef struct { type1 invariant_var1; type2 invariant_var2; ... typen invariant_varn; } invarianttype; // contains variables need, type, instantiating generate set of global variables. typedef invarianttype* invariantptr; // point set of invariants in "main.cpp" file you'll have:
#include "common_components.hpp" // functions declaration int main(int, char**); mytype1 calculatevalues1(invariantptr); /* functions have imput param pointer globals */ mytype2 calculatevalues2(invariantptr); /* functions have imput param pointer globals */ ... mytype3 calculatevaluesn(invariantptr); /* functions have imput param pointer globals */ // main implementation int main(int argc, char** argv) { invarianttype invariants = { value1, value2, ... valuen }; // instantiating invariants need. invariantptr global = &invariants; // have variable global being pointer global. // here have call functions calculatevalue1(global); calculatevalue2(global); ... calculatevaluen(global); } if have functions returning or using global variable use pointer struct modifying methods' interface. doing changes flooded using thoss variables.
Comments
Post a Comment