阿攀的博客

海阔天空

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  5 随笔 :: 2 文章 :: 11 评论 :: 0 Trackbacks

      代码下载链接: /Files/Apan/CP_FixedString.rar
     
       近来,项目组中在很多地方频繁的使用固定长度的字符数组,由于各人的操作习惯不一样,可能要的结果一样,但过程不一致,有时,在书写过程中,可能会漏写致命的操作。基于这些原因,封装了一个固定长度字符数组的模板类容器,提供一些常用操作,如果需要更多的操作,可以使用STL里的算法。代码如下:
       注意:由于,模板的参数是个常量,所以CP_String<10>test1和CP_String<16>test2是两种不同类型的对象,这样的操作CP_String<10>test1(test2)、test1=test2等是非法的,可以这样使用CP_String<10>test1(test2.begin())、test1=test2.begin()
       希望对看过本篇文章的朋友有所帮助,如发现错误的地方,请指出,非常感谢!
      
      后记:写完本片文章的第二天,感觉这样的操作CP_String<10>test1(test2)、test1=test2等是非法的,感觉不是很爽,用起来比较麻烦,经过一番的尝试,终于搞定,很是高兴。
                  拷贝构造函数、赋值函数等类似函数只需这样修改就可以:

1 template<size_type M>
2     CP_String(const CP_String<M>& other)
3     {
4         stringCopy(other.begin());
5     }

 这是原来写法:

1 
2     CP_String(const CP_String<N>& other)
3     {
4         stringCopy(other.begin());
5     }


完整代码如下:

  1 #ifndef _HZ_FIXSTRING_HEAD
  2 #define _HZ_FIXSTRING_HEAD
  3 /**
  4  CopyRight:  (C)  2009 by caipan
  5  Contents:   Fixed buffer length strings
  6  Email:      caipan0206@gmail.com
  7 */
  8 
  9 #include <memory.h>
 10 
 11 template<size_t N>
 12 class CP_FixString
 13 {
 14 public:
 15     typedef TCHAR        value_type;
 16     typedef TCHAR*       pointer;
 17     typedef const TCHAR* const_pointer;
 18     typedef TCHAR*       iterator;
 19     typedef const TCHAR* const_iterator;
 20     typedef TCHAR&       reference;
 21     typedef const TCHAR& const_reference;
 22     typedef size_t       size_type;
 23     typedef ptrdiff_t    difference_type;
 24     typedef std::random_access_iterator_tag iterator_category;
 25 
 26 private:
 27     enum 
 28     {
 29         max_length = N, //表示字符串的最大长度,不包括_T('\0')
 30         max_size = N+1  //容纳字符串的缓冲区大小
 31     }; 
 32 
 33 public:
 34     CP_FixString()
 35     {
 36         m_nStrLength = 0;
 37         memset(m_buf, 0sizeof(m_buf));
 38     }
 39 
 40     CP_FixString(const TCHAR *pData)
 41     {
 42         stringCopy(pData);
 43     }
 44 
 45     template<size_type M>
 46     CP_FixString(const CP_FixString<M>& other)
 47     {
 48         stringCopy(other.begin());
 49     }
 50 
 51 public:
 52     template<size_type M>
 53     CP_FixString& operator=(const CP_FixString<M> &other)
 54     {
 55         stringCopy(other.begin());
 56         return *this;
 57     }
 58 
 59     CP_FixString& operator=(const TCHAR *pData)
 60     {
 61         stringCopy(pData);
 62         return *this;
 63     }
 64 
 65     CP_FixString& operator+=(const TCHAR *pData)
 66     {
 67         stringCat(pData);
 68         return *this;
 69     }
 70 
 71     template<size_type M>
 72     CP_FixString& operator+=(const CP_FixString<M>& other)
 73     {
 74         stringCat(other.begin());
 75         return *this;
 76     }
 77 
 78     bool operator==(const TCHAR *pData)
 79     {
 80         return !_tcscmp(m_buf, pData);
 81     }
 82 
 83     template<size_type M>
 84     bool operator==(const CP_FixString<M>& other)
 85     {
 86         return !_tcscmp(m_buf, other.begin());
 87     }
 88 
 89     reference operator[](size_type n)
 90     {
 91         n = n < max_length ? n : max_length;
 92 
 93         return m_buf[n];
 94     }
 95 
 96     const_reference operator[] (size_type n) const
 97     {
 98         n = n < max_length ? n : max_length;
 99 
100         return m_buf[n];
101     }
102 
103 public:
104     iterator begin()
105     {
106         return m_buf;
107     }
108 
109     iterator end()
110     {
111         return m_buf + m_nStrLength + 1;
112     }
113 
114     const_iterator begin() const
115     {
116         return m_buf;
117     }
118 
119     const_iterator end() const
120     {
121         return m_buf + m_nStrLength + 1;
122     }
123 
124     bool empty()
125     {
126         return m_buf[0== _T('\0');
127     }
128 
129     void clear()
130     {
131         m_nStrLength = 0;
132         memset(m_buf, 0sizeof(m_buf));
133     }
134 
135     size_type capacity() const
136     {
137         return max_length;
138     }
139 
140     size_type size() const
141     {
142         return m_nStrLength;
143     }
144 
145     size_type length() const
146     {
147         return m_nStrLength;
148     }
149 
150 private:
151     void stringCopy(const TCHAR *pData)
152     {
153         memset(m_buf, 0sizeof(m_buf)); 
154 
155         size_type nLen = _tcslen(pData);
156 
157         nLen = nLen < max_length ? nLen : max_length;
158 
159         _tcsncpy(m_buf, pData, nLen);
160    
161         m_nStrLength = _tcslen(m_buf);
162     }
163 
164     void stringCat(const TCHAR *pData)
165     {
166         size_type nDataLen = _tcslen(pData);
167         size_type nLen = length();
168         nLen = max_length - nLen;
169         nLen = nLen > nDataLen ? nDataLen : nLen;
170 
171         _tcsncat(m_buf, pData, nLen);
172         
173         m_nStrLength = _tcslen(m_buf);
174     }
175 
176 private:
177     TCHAR m_buf[max_size];
178     size_type m_nStrLength;
179 };
180 #endif
posted on 2009-05-02 18:48 阿攀 阅读(2075) 评论(4)  编辑 收藏 引用 所属分类: STL

评论

# re: 封装固定长度字符数组的模板容器类 2009-05-02 22:58 尹东斐
我觉得这个可以考虑重新定义一个
template <class T, int N>
class my_allocator
: public allocator<T>
{
//按照N分配空间
};

template <int N>
class my_string
: public basic_string<char, char_traits<char>, my_allocator<char, N> >
{};

这样子实现起来,不用考虑异常安全等问题,标准库会考虑这个,因为allocator的实现比起string来,简单多了。  回复  更多评论
  

# re: 封装固定长度字符数组的模板容器类 2009-05-04 08:51 Apan
看来这位仁兄是这方面的老手。谢谢你的建议!@尹东斐
  回复  更多评论
  

# re: 封装固定长度字符数组的模板容器类 2010-02-23 18:48 ccsdu2009
boost中就有array!  回复  更多评论
  

# re: 封装固定长度字符数组的模板容器类[未登录] 2010-02-24 15:30 Apan
boost中确实有这样的fixed array template,但个人感觉并不适合于fixed string。@ccsdu2009
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理