Commits

Jeremy Sandell  committed 77bfeab

removed some vtables, added some const goodness, forward declarations. still needs a lot of love.

  • Participants
  • Parent commits c2fb099
  • Branches refactoring

Comments (0)

Files changed (11)

File include/hl7.hpp

 
 #include <hl7/message.hpp>
 #include <hl7/segment.hpp>
-
 #include <hl7/util.hpp>
 #include <hl7/except.hpp>
 #endif

File include/hl7/hl7object.hpp

 #ifndef _HL7_OBJECT_H_
 #define _HL7_OBJECT_H_
 
-#include <hl7/common.hpp>
+#include <string>
+
+//#include <hl7/common.hpp>
 #include <hl7/util.hpp>
 #include <hl7/object.hpp>
 
-namespace hl7 {
-    /**
-     * \class hl7object
-     * \brief Container/manager for segment types in HL7 message. 
-     *
-     */
-    class hl7object : public baseobject {
-        public:
-            hl7object(const std::string &data,
-                      const std::string &rsep = "\r",
-                      const std::string &fsep = "|",
-                      const std::string &ssep = "^") : odata(data),
-                                                       recordsep(rsep),
-                                                       fieldsep(fsep),
-                                                       subfieldsep(ssep) {};
+namespace hl7
+{
 
-            virtual ~hl7object() {};
+/**
+ * \class hl7object
+ * \brief Container/manager for segment types in HL7 message. 
+ *
+ */
 
-            virtual std::string rs() const { return recordsep; };
-            virtual std::string fs() const { return fieldsep; }; 
-            virtual std::string sfs() const { return subfieldsep; };
+class hl7object
+{
+public:
+    hl7object(const std::string &data,
+              const std::string &rsep = "\r",
+              const std::string &fsep = "|",
+              const std::string &ssep = "^");
 
-        protected:
-            std::string odata;
+    ~hl7object();
 
-            std::string recordsep;
-            std::string fieldsep;
-            std::string subfieldsep;
-    };
+    inline std::string rs() const { return recordsep; };
+    inline std::string fs() const { return fieldsep; }; 
+    std::string sfs() const { return subfieldsep; };
 
+
+private:
+    std::string odata;
+    std::string recordsep;
+    std::string fieldsep;
+    std::string subfieldsep;
 };
 
+}; // end ns hl7
 #endif

File include/hl7/hl7segment.hpp

 #ifndef _HL7_BASESEGMENT_H_
 #define _HL7_BASESEGMENT_H_
 
-#include <hl7/common.hpp>
-#include <hl7/hl7object.hpp>
-#include <hl7/segment.hpp>
+//#include <hl7/common.hpp>
+//#include <hl7/segment.hpp>
 
-namespace hl7 {
-    /**
-     * \class hl7segment
-     * \brief Container/manager for segment types in HL7 message. 
-     *
-     */
-    class hl7segment : public hl7object {
+#include <string>
+#include <vector>
+#include <iostream>
 
-        public:
-            hl7segment(const std::string &data,
-                       const std::string &rsep = "\r",
-                       const std::string &fsep = "|",
-                       const std::string &ssep = "^");
 
-            ~hl7segment() {};
+namespace hl7
+{
 
-            std::vector<segment>::iterator inline begin(void) { return s.begin(); };
-            std::vector<segment>::iterator inline end(void) { return s.end(); };
+class segment;
 
-            void push(const std::vector<std::string> &fields);
-            virtual void push(const hl7segment &fields);
-            virtual int count(void);
+/**
+ * \class hl7segment
+ * \brief Container/manager for segment types in HL7 message. 
+ *
+ */
 
-            std::string tostring() { return std::string("HL7 Segment: " + odata); };
-            segment & operator[](const int idx);
-            friend std::ostream & operator<<(std::ostream &out, hl7segment &s);
+class hl7segment
+{
 
+public:
+    hl7segment(const std::string &data,
+               const std::string &rsep = "\r",
+               const std::string &fsep = "|",
+               const std::string &ssep = "^");
 
-        private:
-            hl7segment();
-            hl7segment(const hl7object &o);
-            std::vector<segment> s;
-    };
+    ~hl7segment();
 
-    std::ostream & operator<<(std::ostream &out, const hl7segment &s);
+    inline std::vector<segment>::iterator begin() { return s.begin(); };
+    inline std::vector<segment>::iterator end() { return s.end(); };
+    inline std::string tostring() const { return std::string("HL7 Segment: " + odata); };
+
+    inline std::string rs() const { return recordsep; };
+    inline std::string fs() const { return fieldsep; }; 
+    inline std::string sfs() const { return subfieldsep; };
+
+    void push(const std::vector<std::string> &fields);
+    void push(const hl7segment &fields);
+    int count() const;
+
+    segment & operator[](const int idx);
+    friend std::ostream & operator<<(std::ostream &out, hl7segment &s);
+
+
+private:
+    hl7segment();
+
+    std::vector<segment> s;
+    std::string odata;
+    std::string recordsep;
+    std::string fieldsep;
+    std::string subfieldsep;
+};
+
+std::ostream & operator<<(std::ostream &out, const hl7segment &s);
+
 };
 
 #endif

File include/hl7/message.hpp

 #ifndef _HL7_MESSAGE_H_
 #define _HL7_MESSAGE_H_
 
-#include <hl7/common.hpp>
-#include <hl7/hl7segment.hpp>
-#include <hl7/hl7object.hpp>
-#include <hl7/segment.hpp>
 
-namespace hl7 {
-    class message : public hl7object {
+#include <vector>
+#include <string>
+#include <map>
 
-        public:
-            /**
-             * message
-             */
-            message(const std::string &data,
-                    const std::string &rs = "\r",
-                    const std::string &fs = "|",
-                    const std::string &sfs = "^");
-            /**
-             * push
-             */
-            void push(const std::string &name, hl7segment &seg);
 
-            /**
-             * push
-             */
-            void push(const std::string &name, std::vector<std::string> &fields);
+namespace hl7
+{
 
-            /**
-             * parse
-             */
-            void parse(void);
+class hl7segment;
+class segment;
 
-            /**
-             * contains
-             */
-            bool contains(const std::string &name);
+class message
+{
 
-            /**
-             * operator[]
-             */
-            hl7segment & operator[](const std::string &name);
+public:
+    /**
+     * message
+     */
 
-            bool rm_sof(const std::string &n = "\013");
-            bool rm_eof(const std::string &n = "\015\034\015");
+    message(const std::string &data,
+            const std::string &rs = "\r",
+            const std::string &fs = "|",
+            const std::string &sfs = "^");
 
-            std::string tostring() { return std::string("HL7 Message object"); };
-            operator std::string() { return std::string("HL7 Message object"); };
-            friend std::ostream & operator<<(std::ostream &out, message &m);
-            std::vector<std::string> & segments(std::vector<std::string> &f);
-            virtual ~message() {};
+    ~message();
 
-        private:
-            std::string msg;
+    /**
+     * push
+     */
+    void push(const std::string &name,
+              hl7segment &seg);
 
-            typedef std::map< std::string, hl7segment>::iterator msg_iter;
-            std::map<std::string, hl7segment> Message; // data["OBX"][index]
-            message(const message & m);
-    };
+    /**
+     * push
+     */
+    void push(const std::string &name,
+              const std::vector<std::string> &fields);
 
-    std::ostream & operator<<(std::ostream &out, message &m);
+    /**
+     * parse
+     */
+    void parse();
+
+    /**
+     * contains
+     */
+    bool contains(const std::string &name);
+
+    /**
+     * operator[]
+     */
+    hl7segment & operator[](const std::string &name);
+    const hl7segment & operator[](const std::string &name) const;
+
+
+    bool rm_sof(const std::string &n = "\013");
+    bool rm_eof(const std::string &n = "\015\034\015");
+
+    inline std::string tostring()
+    {
+        return std::string("HL7 Message object");
+    }
+
+
+    operator std::string()
+    {
+        return std::string("HL7 Message object");
+    }
+
+    friend std::ostream & operator<<(std::ostream &out, message &m);
+    std::vector<std::string> & segments(std::vector<std::string> &f);
+
+private:
+    std::string msg;
+    std::string recordsep;
+    std::string fieldsep;
+    std::string subfieldsep;
+
+
+    typedef std::map<std::string, hl7segment>::iterator msg_iter;
+    std::map<std::string, hl7segment> Message; // data["OBX"][index]
+    message(const message & m);
+};
+
+std::ostream & operator<<(std::ostream &out,
+                          message &m);
+
 };
 #endif

File include/hl7/segment.hpp

 #ifndef _HL7_SEGMENT_H_
 #define _HL7_SEGMENT_H_
 
-#include <hl7/common.hpp>
 #include <hl7/util.hpp>
-#include <hl7/hl7object.hpp>
+#include <vector>
+#include <string>
 
-namespace hl7 {
-    class segment : public hl7object {
-        public:
-            segment(const std::vector<std::string> &data,
-                    const std::string &rsep = "\r",
-                    const std::string &fsep = "|",
-                    const std::string &ssep = "^");
+namespace hl7
+{
 
+class segment
+{
+public:
+    segment(const std::vector<std::string> &data,
+            const std::string &rsep = "\r",
+            const std::string &fsep = "|",
+            const std::string &ssep = "^");
 
-            ~segment() {};
+    ~segment();
 
-            std::string operator[](const int index);
-            inline int count() { return this->data.size(); };
-            inline int size() { return sizeof(data); };
-            inline operator std::string() { return this->tostring(); };
-            inline operator char *() { return const_cast<char *>(this->tostring().c_str()); };
-            inline operator const char *() { return this->tostring().c_str(); };
+    std::string operator[](const int index);
 
-            std::string tostring(void);
+    inline int count() const { return this->data.size(); };
+    inline int size() const { return sizeof(data); };
 
-            friend std::ostream & operator<<(std::ostream &out, segment &s);
-            friend std::istream & operator>>(std::istream &in, segment &s);
+    inline operator std::string() { return this->tostring(); };
+    inline operator char *() { return const_cast<char *>(this->tostring().c_str()); };
+    inline operator const char *() { return this->tostring().c_str(); };
 
-            std::string field(const int index);
-            std::string name(void);
-            std::vector<std::string> fields(void);
+    std::string tostring();
 
-            std::vector<std::string>::iterator begin(void);
-            std::vector<std::string>::iterator end(void);
+    friend std::ostream & operator<<(std::ostream &out,
+                                     const segment &s);
 
-        private:
-            std::vector<std::string> data;
-            segment();
-    };
-    std::ostream & operator<<(std::ostream &out, segment &s);
+    friend std::istream & operator>>(std::istream &in,
+                                     segment &s);
+
+    std::string field(const int index);
+
+
+    inline std::string rs() const { return recordsep; };
+    inline std::string fs() const { return fieldsep; }; 
+    inline std::string sfs() const { return subfieldsep; };
+
+    std::string name();
+    std::vector<std::string> fields();
+
+    std::vector<std::string>::iterator begin();
+    std::vector<std::string>::iterator end();
+
+    std::vector<std::string>::const_iterator begin() const;
+    std::vector<std::string>::const_iterator end() const;
+
+private:
+    segment();
+    std::vector<std::string> data;
+    std::string recordsep;
+    std::string fieldsep;
+    std::string subfieldsep;
+};
+
+std::ostream & operator<<(std::ostream &out,
+                          const segment &s);
+
 };
 #endif

File include/hl7/util.hpp

 #ifndef _HL7_UTIL_H_
 #define _HL7_UTIL_H_
 
-#include <hl7/common.hpp>
+#include <string>
+#include <vector>
 
-namespace hl7 {
-    namespace util {
-        /*Relies on pcrepp*/
-        std::vector<std::string> split(const std::string str,
-                                       const std::string delim);
+namespace hl7 { namespace util {
 
-        /* This one does not. */
-        std::vector<std::string> & split(const std::string &in,
-                                         const char &delim,
-                                         std::vector<std::string> &sv);
+/*Relies on pcrepp*/
+std::vector<std::string> split(const std::string str,
+                               const std::string delim);
 
-        /* Nor does this one. */
-        std::vector<std::string> & split(const std::string &in,
-                                         const std::string &delim,
-                                         std::vector<std::string>& sv);
+/* This one does not. */
+std::vector<std::string> & split(const std::string &in,
+                                 const char &delim,
+                                 std::vector<std::string> &sv);
 
-        inline std::string tolower(const std::string& input_string);
-        void rtrim(std::string& s, char c);
+/* Nor does this one. */
+std::vector<std::string> & split(const std::string &in,
+                                 const std::string &delim,
+                                 std::vector<std::string>& sv);
+
+inline std::string tolower(const std::string& input_string);
+void rtrim(std::string& s, char c);
 
 #ifdef UNSTABLE
-        template<typename T> std::string join(typename std::vector<T> &v,
-                                              std::string &d);
+template<typename T> std::string join(typename std::vector<T> &v,
+                                      std::string &d);
 #else
-        std::string join(const std::vector<std::string> &v,
-                         const std::string &d);
+std::string join(const std::vector<std::string> &v,
+                 const std::string &d);
 #endif
 
-    };
-};
+}; // end namespace util
+
+}; //end namespace hl7
 #endif

File src/hl7segment.cpp

 
 #include <hl7/hl7segment.hpp>
 #include <hl7/except.hpp>
+#include <hl7/segment.hpp>
 
-namespace hl7 {
-    hl7segment::hl7segment(const std::string &data,
-                           const std::string &rsep,
-                           const std::string &fsep,
-                           const std::string &ssep) : hl7object(data,rsep,fsep,ssep)
+namespace hl7
+{
+hl7segment::hl7segment(const std::string &data,
+                       const std::string &rsep,
+                       const std::string &fsep,
+                       const std::string &ssep) : odata(data),
+                                                  recordsep(rsep),
+                                                  fieldsep(fsep),
+                                                  subfieldsep(ssep)
 
-    {}
+{
+}
 
-    void hl7segment::push(const std::vector<std::string> &fields)
-    {
-        segment seg(fields);
-        s.push_back(seg);
-    }
+hl7segment::~hl7segment()
+{
+}
 
-    void hl7segment::push(const hl7segment &fields)
-    {
-        for(std::vector<segment>::const_iterator iter = fields.s.begin(); iter != fields.s.end(); iter++)
-            s.push_back((*iter));
-    }
+void hl7segment::push(const std::vector<std::string> &fields)
+{
+    segment seg(fields);
+    s.push_back(seg);
+}
 
+void hl7segment::push(const hl7segment &fields)
+{
+    for(std::vector<segment>::const_iterator iter = fields.s.begin(); iter != fields.s.end(); ++iter)
+        s.push_back((*iter));
+}
 
-    segment & hl7segment::operator[](const int idx)
-    {
-        /* Complain if idx is invalid. */
-        if (idx < 0 || idx > int(s.size()))
-            throw Exception("Index error");
 
-        return s[idx];
-    }
+segment & hl7segment::operator[](const int idx)
+{
+    /* Complain if idx is invalid. */
+    if (idx < 0 || idx > int(s.size()))
+        throw Exception("Index error");
 
-    int hl7segment::count(void)
-    {
-        return s.size();
-    }
+    return s[idx];
+}
 
-    std::ostream & operator<<(std::ostream &out, hl7segment &seg)
-    {
-        for(std::vector<segment>::iterator iter = seg.begin(); iter != seg.end(); iter++)
-            out << (*iter) << seg.rs();
+int hl7segment::count() const
+{
+    return s.size();
+}
 
-        return out;
-    }
-};
+std::ostream & operator<<(std::ostream &out, hl7segment &seg)
+{
+    for(std::vector<segment>::iterator iter = seg.begin(); iter != seg.end(); ++iter)
+        out << (*iter) << seg.rs();
+
+    return out;
+}
+
+}; // end ns hl7

File src/message.cpp

  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <hl7/common.hpp>
+#include <hl7/hl7segment.hpp>
 #include <hl7/message.hpp>
+#include <hl7/segment.hpp>
 #include <hl7/except.hpp>
 #include <hl7/util.hpp>
 
-namespace hl7 {
-    /**
-     * message
-     */
-    message::message(const std::string &data,
-                     const std::string &rsep,
-                     const std::string &fsep,
-                     const std::string &ssep) : hl7object(data,rsep,fsep,ssep),
-                                                msg(data)
-    {
-    }
+#include <pcre++.h>
 
+namespace hl7
+{
 
-    /* TODO: We should be able to ascertain the name from the
-     * fields..
+/**
+ * message
+ */
+message::message(const std::string &data,
+                 const std::string &rsep,
+                 const std::string &fsep,
+                 const std::string &ssep) : msg(data),
+                                            recordsep(rsep),
+                                            fieldsep(fsep),
+                                            subfieldsep(ssep)
+{
+}
+
+message::~message()
+{
+}
+
+/* TODO: We should be able to ascertain the name from the
+ * fields..
+ */
+
+void message::push(const std::string &name,
+                   const std::vector<std::string> &fields)
+{
+    msg_iter keyvalue = Message.find(name);
+
+    /* If it doesn't exist already, convert to 
+     * hl7segment and add it.
      */
 
-    void message::push(const std::string &name, std::vector<std::string> &fields)
+    if(keyvalue == Message.end())
     {
-        msg_iter keyvalue = Message.find(name);
+        hl7segment seg(name, recordsep, fieldsep, subfieldsep);
+        seg.push(fields);
+        Message.insert(make_pair(name, seg));
+    }
+    else  
+        /* Already have a segment of this type, append to our list.*/
+        keyvalue->second.push(fields);
 
-        /* If it doesn't exist already, convert to 
-         * hl7segment and add it.
+}
+
+void message::push(const std::string &name, hl7segment &seg)
+{
+
+    msg_iter keyvalue = Message.find(name);
+    /* Add new if it doesn't exist already. */
+
+    if(keyvalue == Message.end())
+        Message.insert(make_pair(name, seg));
+    else 
+        /* Already have a segment of this type, append to our list.*/
+        keyvalue->second.push(seg);
+}
+
+void message::parse(void)
+{
+    using namespace pcrepp;
+    Pcre re("^([A-Z0-9]{3})(.)(.)(.)(.)(.)(.)");
+
+    std::vector<std::string> lines;
+    std::vector<std::string> fields;
+    std::string name;
+
+    if(re.search(msg) == true)
+    {
+        /* If it doesnt have seven chars, assume it's
+         * invalid;i.e., transmission error, corrupt file, 
+         * etc.
          */
 
-        if(keyvalue == Message.end())
+        if(re.matches() == 7)
         {
-            hl7segment seg(name, recordsep, fieldsep, subfieldsep);
-            seg.push(fields);
-            Message.insert(make_pair(name, seg));
-        }
-        else  
-            /* Already have a segment of this type, append to our list.*/
-            keyvalue->second.push(fields);
-
-    }
-
-    void message::push(const std::string &name, hl7segment &seg)
-    {
-        msg_iter keyvalue = Message.find(name);
-        /* Add new if it doesn't exist already. */
-
-        if(keyvalue == Message.end())
-            Message.insert(make_pair(name, seg));
-        else 
-            /* Already have a segment of this type, append to our list.*/
-            keyvalue->second.push(seg);
-    }
-
-    void message::parse(void)
-    {
-        using namespace pcrepp;
-        Pcre re("^([A-Z0-9]{3})(.)(.)(.)(.)(.)(.)");
-
-        std::vector<std::string> lines;
-        std::vector<std::string> fields;
-        std::string name;
-
-        if(re.search(msg) == true)
-        {
-            /* If it doesnt have seven chars, assume it's
-             * invalid;i.e., transmission error, corrupt file, 
-             * etc.
+            /* Make sure the field separator matches
+             * the rest of the delimiters. 
              */
 
-            if(re.matches() == 7)
+            if(re.get_match(6) != re.get_match(1))
+                throw Exception("Field separator doesn't match delimiters.");
+
+            if(fieldsep.empty())
+                fieldsep = re.get_match(1);
+
+            if(subfieldsep.empty())
+                subfieldsep = re.get_match(2);
+
+            /* Split into lines using record separator. */
+            lines = util::split(msg, recordsep, lines);
+
+            for(std::vector<std::string>::iterator line = lines.begin();
+                line != lines.end(); line++)
             {
-                /* Make sure the field separator matches
-                 * the rest of the delimiters. 
-                 */
+                name = (*line).substr(0, 3);
+                if(name.length() != 3)
+                    continue;
 
-                if(re.get_match(6) != re.get_match(1))
-                    throw Exception("Field separator doesn't match delimiters.");
-
-                if(fieldsep.empty())
-                    fieldsep = re.get_match(1);
-
-                if(subfieldsep.empty())
-                    subfieldsep = re.get_match(2);
-
-                /* Split into lines using record separator. */
-                lines = util::split(msg, recordsep, lines);
-
-                for(std::vector<std::string>::iterator line = lines.begin();
-                    line != lines.end(); line++)
-                {
-                    name = (*line).substr(0, 3);
-                    if(name.length() != 3)
-                        continue;
-
-                    fields = util::split((*line), fieldsep, fields);
-                    this->push(name, fields);
-                    fields.clear();
-                }
+                fields = util::split((*line), fieldsep, fields);
+                this->push(name, fields);
+                fields.clear();
             }
-            else
-                throw Exception("Corrupted MSH segment. (second pass)");
         }
         else
-            throw Exception("Corrupted MSH segment.(first pass)");
+            throw Exception("Corrupted MSH segment. (second pass)");
     }
+    else
+        throw Exception("Corrupted MSH segment.(first pass)");
+}
 
-    bool message::contains(const std::string &name)
+bool message::contains(const std::string &name)
+{
+    return (Message.find(name) != Message.end());
+}
+
+std::vector<std::string> & message::segments(std::vector<std::string> &f)
+{
+    f.clear();
+
+    for(msg_iter iter = Message.begin(); iter != Message.end(); iter++)
+        f.push_back(iter->first);
+
+    return f;
+}
+
+hl7segment & message::operator[](const std::string &name)
+{
+    std::map<std::string, hl7segment>::iterator iter = Message.find(name);
+
+    if (iter != Message.end())
     {
-        return (Message.find(name) != Message.end());
+        return iter->second;
     }
+    else
+    {
+        throw Exception("No such field.");
+    }
+}
 
-    std::vector<std::string> & message::segments(std::vector<std::string> &f)
+const hl7segment & message::operator[](const std::string &name) const
+{
+    std::map<std::string, hl7segment>::const_iterator iter = Message.find(name);
+
+    if (iter != Message.end())
     {
-        f.clear();
+        return iter->second;
+    }
+    else
+    {
+        throw Exception("No such field.");
+    }
+}
 
-        for(msg_iter iter = Message.begin(); iter != Message.end(); iter++)
-            f.push_back(iter->first);
 
-        return f;
+bool message::rm_sof(const std::string &n)
+{
+    std::string::size_type srch = msg.find(n);
+
+    if(srch != std::string::npos)
+    {
+        msg.erase(srch, 1);
+        return true;
     }
+    return false;
+}
 
-    hl7segment & message::operator[](const std::string &name)
+bool message::rm_eof(const std::string &n)
+{
+    std::string::size_type srch = msg.find(n);
+
+    if(srch != std::string::npos)
     {
-        msg_iter iter = Message.find(name);
+        msg.erase(srch, 1);
+        return true;
+    }
+    return false;
+}
 
-        if (iter != Message.end())
-            return iter->second;
-        else
-            throw Exception("No such field.");
-    }
-
-    bool message::rm_sof(const std::string &n)
-    {
-        std::string::size_type srch = msg.find(n);
-
-        if(srch != std::string::npos)
-        {
-            msg.erase(srch, 1);
-            return true;
-        }
-        return false;
-    }
-
-    bool message::rm_eof(const std::string &n)
-    {
-        std::string::size_type srch = msg.find(n);
-
-        if(srch != std::string::npos)
-        {
-            msg.erase(srch, 1);
-            return true;
-        }
-        return false;
-    }
 };

File src/segment.cpp

 #include <hl7/util.hpp>
 
 namespace hl7 {
-    segment::segment(const std::vector<std::string> &data_,
-                     const std::string &rsep,
-                     const std::string &fsep,
-                     const std::string &ssep) : hl7object("",rsep,fsep,ssep),
-                                                data(data_)
-    {}
+segment::segment(const std::vector<std::string> &data_,
+                 const std::string &rsep,
+                 const std::string &fsep,
+                 const std::string &ssep) : data(data_),
+                                            recordsep(rsep),
+                                            fieldsep(fsep),
+                                            subfieldsep(ssep)
 
+{
+}
 
-    std::string segment::field(int index)
-    {
-        /* Complain if index is invalid. */
-        if (index < 0 || index > int(data.size()))
-            throw Exception("Index error");
+segment::~segment()
+{
+}
 
-            return data[index];
-    }
+std::string segment::field(int index)
+{
+    /* Complain if index is invalid. */
+    if (index < 0 || index > int(data.size()))
+        throw Exception("Index error");
 
-    std::string segment::operator[](const int index)
-    {
-        if(int(data.size()) >= index)
-            return data[index];
-        else
-            throw Exception("Index error");
-    }
+        return data[index];
+}
 
-    std::vector<std::string>::iterator segment::begin(void)
-    {
-        std::vector<std::string>::iterator iter = data.begin();
-        return iter;
-    }
+std::string segment::operator[](const int index)
+{
+    if(int(data.size()) >= index)
+        return data[index];
+    else
+        throw Exception("Index error");
+}
 
-    std::vector<std::string>::iterator segment::end(void)
-    {
-        std::vector<std::string>::iterator iter = data.end();
-        return iter;
-    }
-    std::string segment::tostring(void)
-    {
+std::vector<std::string>::iterator segment::begin()
+{
+    std::vector<std::string>::iterator iter = data.begin();
+    return iter;
+}
+
+std::vector<std::string>::iterator segment::end()
+{
+    std::vector<std::string>::iterator iter = data.end();
+    return iter;
+}
+
+std::vector<std::string>::const_iterator segment::begin() const
+{
+    std::vector<std::string>::const_iterator iter = data.begin();
+    return iter;
+}
+
+std::vector<std::string>::const_iterator segment::end() const
+{
+    std::vector<std::string>::const_iterator iter = data.end();
+    return iter;
+}
+
+
+
+std::string segment::tostring()
+{
 #if 0
         /*TODO: make call to join work. */
         return util::join(data, this->fs());
 #endif
 
-        std::string combined;
+    std::string combined;
 
-        for(std::vector<std::string>::iterator iter = data.begin(); iter != data.end(); iter++)
-        {
-            combined.append((*iter));
+    for(std::vector<std::string>::iterator iter = data.begin(); iter != data.end(); ++iter)
+    {
+        combined.append((*iter));
 
-            if(iter != data.end()-1)
-                combined.append(fieldsep);
-        }
-        return combined;
+        if(iter != data.end()-1)
+            combined.append(fieldsep);
     }
+    return combined;
+}
 
-    std::ostream & operator<<(std::ostream &out, segment &s)
+std::ostream & operator<<(std::ostream &out,
+                          const segment &s)
+{
+    for(std::vector<std::string>::const_iterator iter = s.begin(); iter != s.end(); ++iter)
     {
-        for(std::vector<std::string>::iterator iter = s.begin(); iter != s.end(); iter++)
-        {
-            out << (*iter);
+        out << (*iter);
 
-            if(iter != s.end()-1)
-                out << s.fs();
-        }
-        return out;
+        if(iter != s.end()-1)
+            out << s.fs();
     }
+    return out;
+}
 
-    std::istream & operator>>(std::istream &in, segment &s)
-    {
-        std::string tmp;
-        in >> tmp;
-        s.data.push_back(tmp);
-        return in;
-    }
+std::istream & operator>>(std::istream &in, segment &s)
+{
+    std::string tmp;
+    in >> tmp;
+    s.data.push_back(tmp);
+    return in;
+}
 };

File tests/hl7.cpp

 #include <hl7.hpp>
-#include <hl7/common.hpp>
+#include <hl7/hl7segment.hpp>
 #include <hl7/except.hpp>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
 
 #include <cassert>
 
 
                 /* Print them out. */
                 for(vector<string>::iterator iter = flds.begin(); iter != flds.end(); iter++)
-                    cout << "'"<<(*iter)<<endl;
+                    cout << "'" << (*iter) << "'" << endl;
 
 
                 /* Print out the entire PID segment. This is, in effect, 
                  * printing out message["PID"].hl7segment[0].segment.
                  */
+
                 cout << m["PID"] <<endl;
 
                 /* Since one can have multiple entries of a segment type, */

File tests/util.cpp

 #include <hl7/util.hpp>
 #include <hl7/except.hpp>
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
 
 int
 main(int argc, char **argv)