opp_string.h

00001 //==========================================================================
00002 //   OPP_STRING.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  opp_string class
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2008 Andras Varga
00013   Copyright (C) 2006-2008 OpenSim Ltd.
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __OPP_STRING_H
00020 #define __OPP_STRING_H
00021 
00022 #include <vector>
00023 #include <map>
00024 #include <ostream>
00025 #include "simkerneldefs.h"
00026 #include "simutil.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00041 class SIM_API opp_string
00042 {
00043   private:
00044     char *str;
00045 
00046   public:
00050     opp_string()  {str = 0;}
00051 
00055     opp_string(const char *s)  {str = opp_strdup(s);}
00056 
00060     opp_string(const char *s, int n)  {str = new char[n+1]; strncpy(str, s?s:"", n); str[n] = '\0';}
00061 
00065     opp_string(const std::string& s)  {str = opp_strdup(s.c_str());}
00066 
00070     opp_string(const opp_string& s)  {str = opp_strdup(s.str);}
00071 
00075     ~opp_string()  {delete [] str;}
00076 
00080     const char *c_str() const  {return str ? str : "";}
00081 
00085     bool empty() const  {return !str || !str[0];}
00086 
00092     char *buffer()  {return str;}
00093 
00097     char *reserve(unsigned size)  {delete[] str;str=new char[size];return str;}
00098 
00103     const char *operator=(const char *s)  {delete[] str;str=opp_strdup(s);return str;}
00104 
00108     opp_string& operator=(const opp_string& s)  {operator=(s.str); return *this;}
00109 
00113     opp_string& operator=(const std::string& s)  {operator=(s.c_str()); return *this;}
00114 
00118     bool operator<(const opp_string& s) const  {return opp_strcmp(str,s.str) < 0;}
00119 
00123     opp_string& operator+=(const char *s) {return operator=(std::string(str).append(s));}
00124 
00128     opp_string& operator+=(const opp_string& s) {operator+=(s.str); return *this;}
00129 
00133     opp_string& operator+=(const std::string& s) {operator+=(s.c_str()); return *this;}
00134 
00138     opp_string operator+(const char *s) {return opp_string((std::string(str)+s).c_str());}
00139 
00143     opp_string operator+(const opp_string& s) {return operator+(s.c_str());}
00144 
00148     opp_string operator+(const std::string& s) {return operator+(s.c_str());}
00149 
00150 };
00151 
00152 inline std::ostream& operator<<(std::ostream& out, const opp_string& s)
00153 {
00154     out << s.c_str(); return out;
00155 }
00156 
00157 
00165 class SIM_API opp_string_vector : public std::vector<opp_string>
00166 {
00167   public:
00168     opp_string_vector() {}
00169     opp_string_vector(const opp_string_vector& other) {*this = other;}
00170 };
00171 
00172 
00180 class SIM_API opp_string_map : public std::map<opp_string,opp_string>
00181 {
00182   public:
00183     opp_string_map() {}
00184     opp_string_map(const opp_string_map& other) {*this = other;}
00185 };
00186 
00187 NAMESPACE_END
00188 
00189 
00190 #endif
00191 
00192 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3