Re: [Boost-users] Dynamically creating new units
Hi, Oh, sorry, I forgot to specify that I was talking about the Boost.Units library. Brandon On 2009-12-25, Brandon Olivares wrote:
Hi,
I posted before about having a problem with having so many units that it takes forever to install. I was thinking about trying to perhaps dynamically create only the ones I need, because I can't know that at compile-time.
Mostly it would be scaled units. For instance I might have the unit meter, but might want to create a scaled unit, say like kilometer or millimeter.
Also though I wouldn't mind being able to create some of the astronomical units when necessary, such as parsec or light year. But in all cases, the base units already exist.
What would be the best way to do this?
Thanks, Brandon
-- www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana Follow creatorspiritus.com on Twitter: http://twitter.com/creatorspiritus
-- Brandon www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana
On Fri, Dec 25, 2009 at 10:49 AM, Brandon Olivares
Hi,
Oh, sorry, I forgot to specify that I was talking about the Boost.Units library.
Brandon
On 2009-12-25, Brandon Olivares wrote:
Hi,
I posted before about having a problem with having so many units that it takes forever to install. I was thinking about trying to perhaps dynamically create only the ones I need, because I can't know that at compile-time.
Mostly it would be scaled units. For instance I might have the unit meter, but might want to create a scaled unit, say like kilometer or millimeter.
Also though I wouldn't mind being able to create some of the astronomical units when necessary, such as parsec or light year. But in all cases, the base units already exist.
What would be the best way to do this?
First of all, please do not top post, here is a good reason why:
A: Yes.
Q: Are you sure?
A: Because it reverses the logical flow of conversation.
Q: Why is top posting annoying in email?
If you are not convinced that it is wrong by the simple example above, now try to mix top and bottom posting:
A: Yes.
Q: Are you sure?
Q: Why is top posting annoying in email? A: Because it reverses the logical flow of conversation.
I don't know about you, but that's simply crazy!
The correct way:
Q: Why is top posting annoying in email? A: Because it reverses the logical flow of conversation. Q: Are you sure? A: Yes.
As for dynamically creating types, you can do that all you wish, but you have no type information then (unless you use variant or any or something, but that has needless overhead, well, maybe not variant). That is the point of Boost.Units, to enforce that you are doing things correctly, at compile time, it optimizes completely out at run-time.
On 2009-12-25, OvermindDL1 wrote:
As for dynamically creating types, you can do that all you wish, but you have no type information then (unless you use variant or any or something, but that has needless overhead, well, maybe not variant). That is the point of Boost.Units, to enforce that you are doing things correctly, at compile time, it optimizes completely out at run-time.
OK thanks, perhaps you are right. I'm just really trying to figure out a way to make compilation a bit faster. My concern is that since these are in header files, well any source file that needs units will require them all to be processed again, and that is really inconvenient. Thanks, Brandon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Brandon www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana
On Fri, Dec 25, 2009 at 11:33 AM, Brandon Olivares
On 2009-12-25, OvermindDL1 wrote:
As for dynamically creating types, you can do that all you wish, but you have no type information then (unless you use variant or any or something, but that has needless overhead, well, maybe not variant). That is the point of Boost.Units, to enforce that you are doing things correctly, at compile time, it optimizes completely out at run-time.
OK thanks, perhaps you are right. I'm just really trying to figure out a way to make compilation a bit faster. My concern is that since these are in header files, well any source file that needs units will require them all to be processed again, and that is really inconvenient.
Yep, that is the major hazard of C++. If you learn how to structure things differently and use forward declarations and less class member functions (use external functions instead, ala C), that can speed up compilation tremendously.
On 2009-12-25, OvermindDL1 wrote:
Yep, that is the major hazard of C++. If you learn how to structure things differently and use forward declarations and less class member functions (use external functions instead, ala C), that can speed up compilation tremendously.
Well yeah, that's the problem. The units are all typedefs and constants so, AFAIK, you can't use forward declarations. I'm hoping to perhaps do something similar to the runtime_unit.cpp example. I can declare all of the base units, so to speak, and then hopefully scale them as necessary. I mostly want to be able to apply any of the SI prefixes to the metric units. Again though if I declare them explicitly, it takes forever to compile. Thanks, Brandon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Brandon www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana
On Fri, Dec 25, 2009 at 6:30 PM, Brandon Olivares
On 2009-12-25, OvermindDL1 wrote:
Yep, that is the major hazard of C++. If you learn how to structure things differently and use forward declarations and less class member functions (use external functions instead, ala C), that can speed up compilation tremendously.
Ah, but you can, depends on how you can abstract your system. Think
of how C works with void pointers and such and you can start to see
patterns for your classes. Think of this.
Say you have a class like this in your header:
class myStuff
{
private:
someSuperHeavyTemplate<int> mySpecialInt;
/* other member vars */
public:
void operateOnMySpecialInt(int i);
int getSpecialInt(void);
/* other member functions */
}
And the implementation is in a cpp file, but now everything that uses
this has to include all the nasty and slow instancing for the
someSuperHeavyTemplate<int> type. Now think of this in a C way...
class myStuff;
void operateOnMySpecialInt(myStuff &m, int i);
int getSpecialInt(myStuff &m);
/* other functions that operate on myStuff */
And the implementation contains the implementation of myStuff and the
function definitions. Only that cpp file includes the heavy template
instancing, and all other accesses are fast. You can only do this on
classes/struct that you normally use as pointers in the first place,
but if you can then you can see how you can vastly simplify access to
only what is needed in the public interface, rather then *everything*
that is in the interface.
On Fri, Dec 25, 2009 at 6:30 PM, Brandon Olivares
Well yeah, that's the problem. The units are all typedefs and constants so, AFAIK, you can't use forward declarations.
True, but how much of your code *needs* all of them, only include
those in the cpp by cpp basis, rather then in all headers everywhere.
On Fri, Dec 25, 2009 at 6:30 PM, Brandon Olivares
I'm hoping to perhaps do something similar to the runtime_unit.cpp example. I can declare all of the base units, so to speak, and then hopefully scale them as necessary.
I mostly want to be able to apply any of the SI prefixes to the metric units. Again though if I declare them explicitly, it takes forever to compile.
Still though, upgrade to GCC4.4+ and you will see a dramatic bettering of compile speed, generally 30% at minimum with heavy template code.
Ah, but you can, depends on how you can abstract your system. Think of how C works with void pointers and such and you can start to see patterns for your classes. Think of this.
<snip>
And the implementation is in a cpp file, but now everything that uses this has to include all the nasty and slow instancing for the someSuperHeavyTemplate<int> type. Now think of this in a C way... class myStuff;
<snip>
And the implementation contains the implementation of myStuff and the function definitions. Only that cpp file includes the heavy template instancing, and all other accesses are fast. You can only do this on classes/struct that you normally use as pointers in the first place, but if you can then you can see how you can vastly simplify access to only what is needed in the public interface, rather then *everything* that is in the interface.
I think this falls under the PIMPL idiom, which is described by Herb Sutter at: http://www.gotw.ca/publications/mill04.htm http://www.gotw.ca/publications/mill05.htm with more generically useful articles at: http://www.gotw.ca/publications/index.htm -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
On Fri, Dec 25, 2009 at 7:25 PM, Ray Burkholder
I think this falls under the PIMPL idiom, which is described by Herb Sutter at:
http://www.gotw.ca/publications/mill04.htm http://www.gotw.ca/publications/mill05.htm
with more generically useful articles at:
Not generally. I am not really a proponent of Pimpl. The C style
gives you complete abstraction, not even any overhead of the Pimpl,
and anything is visible only through functions. As much as many
idiots like to say, the C++ OO style is *not* OO. In C++, lets take
this class:
class anotherClass;
class myStuff
{
private;
int i;
float f;
std::string s;
public:
void doOperation(anotherClass *c);
}
Now, that can let you do things like:
anotherClass *a = fromSomewhere();
myStuff *m = fromSomeplace();
m->doOperation(a);
Now this is no where near real OO as it was originally described. Of
all programming languages, CLOS gets the closest to real OO, and in
pseudo-C++ that would be more like:
class anotherClass;
class myStuff
{
int i;
float f;
std::string s;
}
void
On Fri, Dec 25, 2009 at 6:25 PM, Ray Burkholder
I think this falls under the PIMPL idiom, which is described by Herb Sutter at:
http://www.gotw.ca/publications/mill04.htm http://www.gotw.ca/publications/mill05.htm
Quite frankly, I don't see much point in using the PIMPL idiom. I'd much rather use: struct widget; shared_ptr<widget> make_widget(); void frobnicate( widget & ); Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Fri, Dec 25, 2009 at 8:43 PM, Emil Dotchevski
On Fri, Dec 25, 2009 at 6:25 PM, Ray Burkholder
wrote: I think this falls under the PIMPL idiom, which is described by Herb Sutter at:
http://www.gotw.ca/publications/mill04.htm http://www.gotw.ca/publications/mill05.htm
Quite frankly, I don't see much point in using the PIMPL idiom. I'd much rather use:
struct widget; shared_ptr<widget> make_widget(); void frobnicate( widget & );
Exactly! Ditto. Just what my last post said. It is a very "C" style rather then C++, but its compiling speed enhancements are very nice.
On 2009-12-25, OvermindDL1 wrote:
On Fri, Dec 25, 2009 at 11:33 AM, Brandon Olivares
wrote: As for dynamically creating types, you can do that all you wish, but you have no type information then (unless you use variant or any or something, but that has needless overhead, well, maybe not variant). That is the point of Boost.Units, to enforce that you are doing things correctly, at compile time, it optimizes completely out at run-time. OK thanks, perhaps you are right. I'm just really trying to figure out a way to make compilation a bit faster. My concern is that since these are in header files, well any source file that needs units will require
On 2009-12-25, OvermindDL1 wrote: them all to be processed again, and that is really inconvenient.
Yep, that is the major hazard of C++. If you learn how to structure things differently and use forward declarations and less class member functions (use external functions instead, ala C), that can speed up compilation tremendously.
OK, well perhaps there's another method. The primary bulk of the units I was defining were velocities. I just realized though that you can do l/t though, where l is some length and t is some time, and it doesn't have to define an existing unit. But if I do that, I'm not sure what type to actually define the variable as. sometype v = l/t; What is the type in this case? So for instance if l is light_year and t is hour, just as an example, I don't want to have to define light_year_per_hour as I was before. It's obvious that this is allowed: quantityastronomical::light_year_unit l(3.0 * astronomical::light_years); quantitymetric::hour_unit t(8.0 * metric::hours); quantity> v(l/t); I think figuring that out will pretty much solve my issue. I appreciate the discussion on pimpl though as I had never heard of it before, and it was interesting reading. Thanks, Brandon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Brandon www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana
On Fri, Dec 25, 2009 at 10:49 PM, Brandon Olivares
On 2009-12-25, OvermindDL1 wrote:
On Fri, Dec 25, 2009 at 11:33 AM, Brandon Olivares
wrote: As for dynamically creating types, you can do that all you wish, but you have no type information then (unless you use variant or any or something, but that has needless overhead, well, maybe not variant). That is the point of Boost.Units, to enforce that you are doing things correctly, at compile time, it optimizes completely out at run-time. OK thanks, perhaps you are right. I'm just really trying to figure out a way to make compilation a bit faster. My concern is that since these are in header files, well any source file that needs units will require
On 2009-12-25, OvermindDL1 wrote: them all to be processed again, and that is really inconvenient.
Yep, that is the major hazard of C++. If you learn how to structure things differently and use forward declarations and less class member functions (use external functions instead, ala C), that can speed up compilation tremendously.
OK, well perhaps there's another method. The primary bulk of the units I was defining were velocities. I just realized though that you can do l/t though, where l is some length and t is some time, and it doesn't have to define an existing unit.
But if I do that, I'm not sure what type to actually define the variable as.
sometype v = l/t;
What is the type in this case?
So for instance if l is light_year and t is hour, just as an example, I don't want to have to define light_year_per_hour as I was before. It's obvious that this is allowed:
quantityastronomical::light_year_unit l(3.0 * astronomical::light_years); quantitymetric::hour_unit t(8.0 * metric::hours); quantity> v(l/t);
You could just auto it. :) BOOST_AUTO(v, l/t); Then you do not need to bother figuring out what type it is, although it is your light_year_per_hour, although maybe a little more efficient representation. If you just want the type of an expression like l/t if you want to typedef it or something, then you can use BOOST_TYPEOF. It is all in the documentation. :)
On 2009-12-26, OvermindDL1 wrote:
You could just auto it. :) BOOST_AUTO(v, l/t); Then you do not need to bother figuring out what type it is, although it is your light_year_per_hour, although maybe a little more efficient representation.
If you just want the type of an expression like l/t if you want to typedef it or something, then you can use BOOST_TYPEOF. It is all in the documentation. :) _______________________________________________
Oh that's perfect; that has solved my problem! Thank you very much, and it only took 5 seconds to compile. Brandon
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Brandon www.creatorspiritus.com Blog and forum discussing living a Christ-centered life Follow me on Twitter: http://twitter.com/devbanana
On Sat, Dec 26, 2009 at 12:16 AM, Brandon Olivares
On 2009-12-26, OvermindDL1 wrote:
You could just auto it. :) BOOST_AUTO(v, l/t); Then you do not need to bother figuring out what type it is, although it is your light_year_per_hour, although maybe a little more efficient representation.
If you just want the type of an expression like l/t if you want to typedef it or something, then you can use BOOST_TYPEOF. It is all in the documentation. :) _______________________________________________
Oh that's perfect; that has solved my problem! Thank you very much, and it only took 5 seconds to compile.
Yeah, I guess I should have thought of that earlier. I use BOOST_AUTO and BOOST_TYPEOF rather often actually since it can cut down on template instancing pretty dramatically in some libraries.
participants (4)
-
Brandon Olivares
-
Emil Dotchevski
-
OvermindDL1
-
Ray Burkholder