Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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 class StringBuilderImpl
26 {
27   public:
28     StringBuilderImpl() : m_counter(1)
29     {}
30
31     void append(const std::string& str)
32     {
33         if (!(m_ss << str)) {
34             m_ss.clear();
35             ThrowMsg(PlatformException, "Couldn't append string.");
36         }
37     }
38
39     void append(const StringBuilderImpl* impl)
40     {
41         if (!(m_ss << impl->m_ss)) {
42             m_ss.clear();
43             ThrowMsg(PlatformException,
44                      "Couldn't append string builder.");
45         }
46     }
47
48     void addRef()
49     {
50         ++m_counter;
51     }
52
53     void deref()
54     {
55         if (0 == --m_counter) {
56             delete this;
57         }
58     }
59
60     StringBuilderImpl* clone()
61     {
62         if (1 == m_counter) {
63             return this;
64         }
65
66         --m_counter;
67
68         StringBuilderImpl* result = new StringBuilderImpl();
69         result->m_ss << m_ss;
70         return result;
71     }
72
73     std::string toString() const
74     {
75         return m_ss.str();
76     }
77
78   private:
79     std::stringstream m_ss;
80     unsigned int m_counter;
81 };
82
83 StringBuilder::StringBuilder() : m_impl(new StringBuilderImpl())
84 {}
85
86 StringBuilder::~StringBuilder()
87 {
88     m_impl->deref();
89 }
90
91 StringBuilder::StringBuilder(const StringBuilder& other)
92 {
93     other.m_impl->addRef();
94     m_impl = other.m_impl;
95 }
96
97 StringBuilder & StringBuilder::operator=(const StringBuilder& other)
98 {
99     if (this == &other) {
100         return *this;
101     }
102
103     other.m_impl->addRef();
104
105     m_impl->deref();
106     m_impl = other.m_impl;
107
108     return *this;
109 }
110
111 StringBuilder& StringBuilder::append(const std::string& str)
112 {
113     m_impl = m_impl->clone();
114     m_impl->append(str);
115     return *this;
116 }
117
118 StringBuilder& StringBuilder::append(const StringBuilder& builder)
119 {
120     m_impl = m_impl->clone();
121     m_impl->append(builder.m_impl);
122     return *this;
123 }
124
125 std::string StringBuilder::toString() const
126 {
127     return m_impl->toString();
128 }
129 }
130 } // WrtDeviceApisCommon