tizen beta release
[framework/web/wrt-plugins-common.git] / src / Commons / StringBuilder.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19 #include <sstream>
20 #include "Exception.h"
21 #include "StringBuilder.h"
22
23 namespace WrtDeviceApis {
24 namespace Commons {
25
26 class StringBuilderImpl
27 {
28   public:
29     StringBuilderImpl() : m_counter(1)
30     {
31     }
32
33     void append(const std::string& str)
34     {
35         if (!(m_ss << str)) {
36             m_ss.clear();
37             ThrowMsg(PlatformException, "Couldn't append string.");
38         }
39     }
40
41     void append(const StringBuilderImpl* impl)
42     {
43         if (!(m_ss << impl->m_ss)) {
44             m_ss.clear();
45             ThrowMsg(PlatformException,
46                      "Couldn't append string builder.");
47         }
48     }
49
50     void addRef()
51     {
52         ++m_counter;
53     }
54
55     void deref()
56     {
57         if (0 == --m_counter) {
58             delete this;
59         }
60     }
61
62     StringBuilderImpl* clone()
63     {
64         if (1 == m_counter) {
65             return this;
66         }
67
68         --m_counter;
69
70         StringBuilderImpl* result = new StringBuilderImpl();
71         result->m_ss << m_ss;
72         return result;
73     }
74
75     std::string toString() const
76     {
77         return m_ss.str();
78     }
79
80   private:
81     std::stringstream m_ss;
82     unsigned int m_counter;
83 };
84
85 StringBuilder::StringBuilder() : m_impl(new StringBuilderImpl())
86 {
87 }
88
89 StringBuilder::~StringBuilder()
90 {
91     m_impl->deref();
92 }
93
94 StringBuilder::StringBuilder(const StringBuilder& other)
95 {
96     other.m_impl->addRef();
97     m_impl = other.m_impl;
98 }
99
100 StringBuilder & StringBuilder::operator=(const StringBuilder& other)
101 {
102     if (this == &other) { return *this; }
103
104     other.m_impl->addRef();
105
106     m_impl->deref();
107     m_impl = other.m_impl;
108
109     return *this;
110 }
111
112 StringBuilder& StringBuilder::append(const std::string& str)
113 {
114     m_impl = m_impl->clone();
115     m_impl->append(str);
116     return *this;
117 }
118
119 StringBuilder& StringBuilder::append(const StringBuilder& builder)
120 {
121     m_impl = m_impl->clone();
122     m_impl->append(builder.m_impl);
123     return *this;
124 }
125
126 std::string StringBuilder::toString() const
127 {
128     return m_impl->toString();
129 }
130
131 }
132 } // WrtDeviceApisCommon