you said), and they're not hard to avoid.- Hide quoted text -
I have a global deque variable here called compressedfiles.
http://webEbenezer.net/misc/File.cc
In a function called CalculateMarshallingSize, I compress
input files in order to figure out their sizes. I'm not
ready to marshall the compressed data yet, though, so I copy
the compressed file into the global. Then when I'm ready to
marshall the data I pop it off the deque. Technically it
wouldn't be hard to avoid the global by doing the
compression once in CalculateMarshallingSize and again in
Send. That doesn't seem the right answer to me though.
So I don't know of a way to avoid the global in this case.
For my money, the following would be a step in the right direction:
typedef std::vector<char> compressed_file;
typedef std::deque<compressed_file> compressed_file_deque;
compressed_file_deque& compressed_files() {
static compressed_file_deque deq;
return deq;
}
You would also have to change existing, direct accesses of the variable,
to call the function instead. Then you can change the function
definition at will without altering the code that uses the deque,
piggy-backing levels of abstraction or adding hooks for aspect-oriented
design.
For example, in the case I mentioned above, in which parallel instances
of the module become necessary, you can have compressed_files() return
thread-local deques, so that parallel calls do not interfere with each
other. Alternatively, if the threads need to share the deque, you can
insert a lock in the function to synchronize deque access.
Once you change the rest of the module to use function-call syntax, you
won't have to touch it again, even if you decide to go back to a
module-local variable:
class compressed_file_deque_accessor
{
compressed_file_deque m_deq;
public:
compressed_file_deque& operator()() {
return m_deq;
}
} compressed_files;
This is one type of module-local variable I'm OK with, and should have
mentioned earlier. It still gives you a place to insert hooks, e.g.
access counts, log messages, or thread locks, but allows flexible,
function-like syntax in client code.- Hide quoted text -
- Show quoted text -
Thanks. I guess it might be a step in the right direction
and I may use it.