[proto] baby steps learning with proto, proxy object
Hi, This can be an exercise for definining the scope of Boost.Proto for little things. I have a histogram class that internally looks like this class histogram{ ... bool operator()(double const& x){return impl.increment(x);} bool operator()(double const& x, double const& weight){return impl.accumulate(x, weight);} }; histogram h( .. bin spec .. ); double x = ... ; so to add a count to the bin of x, I call h(x); to add a count with weigh w to bin x, I call h(x,w); so far so good. Now suppose I want to "improve" the syntax. The desired syntax would be: h[x]++; h[x]+=w; I don't want to expose the internal representation of the bins, so I can't return a reference to something from h[x]. So I guess we can create a proxy of the expression h[x] , "subindex of historgram with double". The proxy can be used with the syntax to call either accumulate or increment depending on the application of += or + + to the proxy. The naive implementation is this: class histogram{ ... private: struct proxy_histogram_subscript{ histogram& self; double const& x; proxy_histogram_subscript(histogram& self, double const& x) : self(self), x(x){} proxy_histogram_subscript const& operator++() const{self.increment(x); return *this;} bool operator++(int) const{return self.increment(x);} bool operator+=(double const& weight) const{return self.accumulate(x, weight);} }; public: proxy_histogram_subscript operator[](double const& x){ return proxy_histogram_subscript(*this, x); } }; which allows the desired syntax. The question is, can Boost.Proto do better? What I don't understand is how proto can make such a mini language *inside* the class. Hopefully without adding to much lines of code. I can see that one could create a histogram terminal, but that will be public (e.g. from the main program) and that will allow to create all sorts of expression involving brackets of anything. Thank you, Alfredo
On 10/7/2010 2:52 PM, alfC wrote: <snip>
So I guess we can create a proxy of the expression h[x] , "subindex of historgram with double". The proxy can be used with the syntax to call either accumulate or increment depending on the application of += or + + to the proxy.
The naive implementation is this: class histogram{ ... private: struct proxy_histogram_subscript{ histogram& self; double const& x; proxy_histogram_subscript(histogram& self, double const& x) : self(self), x(x){} proxy_histogram_subscript const& operator++() const{self.increment(x); return *this;} bool operator++(int) const{return self.increment(x);} bool operator+=(double const& weight) const{return self.accumulate(x, weight);} }; public: proxy_histogram_subscript operator[](double const& x){ return proxy_histogram_subscript(*this, x); } };
which allows the desired syntax. The question is, can Boost.Proto do better? <snip>
Not really, I don't think. What you have is short, simple, and efficient. I wouldn't mess with it further. -- Eric Niebler BoostPro Computing http://www.boostpro.com
On Fri, Oct 8, 2010 at 12:31 PM, Eric Niebler
On 10/7/2010 2:52 PM, alfC wrote: <snip>
So I guess we can create a proxy of the expression h[x] , "subindex of historgram with double". The proxy can be used with the syntax to call either accumulate or increment depending on the application of += or + + to the proxy.
The naive implementation is this: class histogram{ ... private: struct proxy_histogram_subscript{ histogram& self; double const& x; proxy_histogram_subscript(histogram& self, double const& x) : self(self), x(x){} proxy_histogram_subscript const& operator++() const{self.increment(x); return *this;} bool operator++(int) const{return self.increment(x);} bool operator+=(double const& weight) const{return self.accumulate(x, weight);} }; public: proxy_histogram_subscript operator[](double const& x){ return proxy_histogram_subscript(*this, x); } };
which allows the desired syntax. The question is, can Boost.Proto do better? <snip>
Not really, I don't think. What you have is short, simple, and efficient. I wouldn't mess with it further.
Thank you, I was just checking whether it worth using proto for such a minimicrolanguage. At least for learning, but it seems not. Thank you for saving me a headache. Alfredo
-- Eric Niebler BoostPro Computing http://www.boostpro.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sie haben diese Nachricht erhalten, da Sie der Google Groups-Gruppe Boost Users beigetreten sind. Wenn Sie Nachrichten in dieser Gruppe posten möchten, senden Sie eine E-Mail an boostusers@googlegroups.com. Wenn Sie aus dieser Gruppe austreten möchten, senden Sie eine E-Mail an boostusers+unsubscribe@googlegroups.com. Besuchen Sie die Gruppe unter http://groups.google.com/group/boostusers?hl=de, um weitere Optionen zu erhalten.
participants (3)
-
alfC
-
Alfredo Correa
-
Eric Niebler