StringStreamer.h

00001 
00002 #ifndef SCT_STRINGSTREAMER_HH
00003 #define SCT_STRINGSTREAMER_HH
00004 
00005 // The purpose of the StringStreamer object is to make it easier to
00006 // write mrs (message stream) messages containing mixtures of numbers
00007 // and text without the need for explicit creation of temporary
00008 // ostringstream objects to hold the MRS_TEXT while it is being built.
00009 
00010 // For two examples of how to use StringStreamer object,
00011 // see the foot of this file
00012 
00013 // Note that this file is called StringStreamer rather than
00014 // StringStreamerClass because the purpose of this file is to
00015 // introduce the Sct::StringStreamer const identifier, not to
00016 // define the StringStreamerClass which nobody will ever need
00017 // to use directly.
00018 
00019 // Author Chris Lester.
00020 
00021 #include <sstream>
00022 #include <string>
00023 
00024 namespace Sct {
00025 
00026   class StringStreamerClass {
00027   private:
00028     std::string _s;
00029   public:
00030     /*
00031     template<class T>
00032     const StringStreamerClass operator,(const T & i) const {
00033       std::ostringstream oss;
00034       oss << _s;
00035       oss << i;
00036       return StringStreamerClass(oss.str());
00037     };
00038     */
00039     template<class T>
00040     const StringStreamerClass operator<<(const T & i) const {
00041       std::ostringstream oss;
00042       oss << _s;
00043       oss << i;
00044       return StringStreamerClass(oss.str());
00045     };
00046     operator std::string() const {
00047       return _s;
00048     };
00049     StringStreamerClass() {};
00050     StringStreamerClass(const std::string & s) : _s(s) {
00051     };
00052   };
00053 
00054   const StringStreamerClass StringStreamer;
00055 
00056 };
00057 
00058 
00059 // Example of use:
00060 //
00061 // If you were to comment out the mini-program below,
00062 // compile it, and run it, it should produce the output:
00063 //
00064 // "s was set to [4-5 is the best!]"
00065 
00066 /*
00067 
00068 #include <iostream>
00069 
00070 int main() {
00071   const std::string s = (Sct::StringStreamer, 4, "-", 5, " is the best!");
00072   std::cout << "s was set to ["<<s<<"]"<<std::endl;
00073   return 0;
00074 };
00075 
00076 // A more useful example (for which it was created!) would be:
00077 
00078 int someNumber = XXX;
00079 mrs_stream << MRS_TEXT((Sct::StringStreamer, "This message including ",someNumber, " would have been very awkward to send without Sct::StringStreamer")) << ENDM;
00080 
00081 */
00082 
00083 #endif

Generated on Mon Feb 6 14:01:30 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6