*
*/
#include <iostream>
-#include <sstream>
//#include "zypp/base/Logger.h"
+#include "zypp/base/String.h"
#include "zypp/TranslatedText.h"
using std::endl;
namespace zypp
{ /////////////////////////////////////////////////////////////////
- const TranslatedText TranslatedText::notext;
-
- struct TranslatedText::Private
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : TranslatedText::Impl
+ //
+ /** TranslatedText implementation. */
+ struct TranslatedText::Impl
{
- std::map<LanguageCode, std::string> translations;
- };
+ Impl()
+ {}
- std::string TranslatedText::text( const LanguageCode &lang ) const
- {
- return d->translations[lang];
- }
-
- LanguageCode TranslatedText::detectLanguage() const
- {
- return LanguageCode();
- }
+ Impl(const std::string &text, const LanguageCode &lang)
+ { setText(text, lang); }
- void TranslatedText::setText( const std::string &text, const LanguageCode &lang )
- {
- d->translations[lang] = text;
- }
+ Impl(const std::list<std::string> &text, const LanguageCode &lang)
+ { setText(text, lang); }
- void TranslatedText::setText( const std::list<std::string> &text, const LanguageCode &lang )
- {
- std::stringstream strs(d->translations[lang]);
- //d->translations[lang].clear();
- std::list<std::string>::const_iterator it;
- for (it = text.begin(); it != text.end(); ++it)
+ std::string text( const LanguageCode &lang = LanguageCode() ) const
+ { return translations[lang]; }
+
+ void setText( const std::string &text, const LanguageCode &lang)
+ { translations[lang] = text; }
+
+ void setText( const std::list<std::string> &text, const LanguageCode &lang)
+ { translations[lang] = str::join( text, "\n" ); }
+
+ /** \todo Do it by accessing the global ZYpp. */
+ LanguageCode detectLanguage() const
{
- strs << *it << std::endl;
- //d->translations[lang] = d->translations[lang] + *it;
- //d->translations[lang] = d->translations[lang] + std::endl;
+ return LanguageCode();
}
- }
- /*
- operator TranslatedText::string() const
- {
- return text();
- }
- */
+ private:
+ mutable std::map<LanguageCode, std::string> translations;
- TranslatedText::TranslatedText(const std::string &text, const LanguageCode &lang)
- {
- d = new Private();
- setText(text, lang);
- }
+ public:
+ /** Offer default Impl. */
+ static shared_ptr<Impl> nullimpl()
+ {
+ static shared_ptr<Impl> _nullimpl( new Impl );
+ return _nullimpl;
+ }
- TranslatedText::TranslatedText(const std::list<std::string> &text, const LanguageCode &lang)
- {
- d = new Private();
- setText(text, lang);
- }
+ private:
+ friend Impl * rwcowClone<Impl>( const Impl * rhs );
+ /** clone for RWCOW_pointer */
+ Impl * clone() const
+ { return new Impl( *this ); }
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : TranslatedText
+ //
+ ///////////////////////////////////////////////////////////////////
+
+ const TranslatedText TranslatedText::notext;
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : TranslatedText::TranslatedText
+ // METHOD TYPE : Ctor
+ //
TranslatedText::TranslatedText()
- {
- d = new Private;
- }
+ : _pimpl( Impl::nullimpl() )
+ {}
- TranslatedText::~TranslatedText()
- {
- delete d;
- }
-
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : TranslatedText::TranslatedText
+ // METHOD TYPE : Ctor
+ //
+ TranslatedText::TranslatedText( const std::string &text,
+ const LanguageCode &lang )
+ : _pimpl( new Impl(text, lang) )
+ {}
- void TranslatedText::operator=(const std::string &text)
- {
- setText(text);
- }
-
- void TranslatedText::operator=(const std::list<std::string> &text)
- {
- setText(text);
- }
+ ///////////////////////////////////////////////////////////////////
+ //
+ // METHOD NAME : TranslatedText::TranslatedText
+ // METHOD TYPE : Ctor
+ //
+ TranslatedText::TranslatedText( const std::list<std::string> &text,
+ const LanguageCode &lang )
+ : _pimpl( new Impl(text, lang) )
+ {}
///////////////////////////////////////////////////////////////////
//
- // METHOD NAME : TranslatedText::asString
- // METHOD TYPE : std::string
+ // METHOD NAME : TranslatedText::~TranslatedText
+ // METHOD TYPE : Dtor
//
- std::string TranslatedText::asString() const
- {
- return std::string();
- }
+ TranslatedText::~TranslatedText()
+ {}
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // Forward to implementation:
+ //
+ ///////////////////////////////////////////////////////////////////
+
+ std::string TranslatedText::text( const LanguageCode &lang ) const
+ { return _pimpl->text( lang ); }
+
+ void TranslatedText::setText( const std::string &text, const LanguageCode &lang )
+ { _pimpl->setText( text, lang ); }
+
+ void TranslatedText::setText( const std::list<std::string> &text, const LanguageCode &lang )
+ { _pimpl->setText( text, lang ); }
+
+ LanguageCode TranslatedText::detectLanguage() const
+ { return _pimpl->detectLanguage(); }
/////////////////////////////////////////////////////////////////
} // namespace zypp
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
-/** \file zypp/Date.h
+/** \file zypp/TranslatedText.h
*
*/
#ifndef ZYPP_TRANSLATEDTEXT_H
#define ZYPP_TRANSLATEDTEXT_H
-#include <list>
+#include <iosfwd>
#include <map>
+#include <list>
#include <string>
-#include "zypp/LanguageCode.h"
-using std::string;
+#include "zypp/base/PtrTypes.h"
+#include "zypp/LanguageCode.h"
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
+
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : TranslatedText
//
- /** Class that represent a text and multiple translations
+ /** Class that represent a text and multiple translations.
*/
class TranslatedText
{
friend std::ostream & operator<<( std::ostream & str, const TranslatedText & obj );
public:
- /** Default ctor: 0 */
- static const TranslatedText notext;
+ /** Implementation */
+ class Impl;
+
+ public:
+ /** Default ctor */
TranslatedText();
- ~TranslatedText();
+ /** Ctor \todo Make ctor it explicit */
+ //explicit
TranslatedText(const std::string &text, const LanguageCode &lang = LanguageCode());
+ /** Ctor. \todo Make ctor it explicit */
+ //explicit
TranslatedText(const std::list<std::string> &text, const LanguageCode &lang = LanguageCode());
- void operator=(const std::string &text);
- void operator=(const std::list<std::string> &text);
- std::string asString() const;
+ /** Dtor */
+ ~TranslatedText();
+
+ /** \todo Think about eliminating them. The default text is nothing
+ * special. It's strange to allow to alter it in a different manner
+ * than the other ones. IMO more confusing than convenient.
+ */
+ void operator=(const std::string &text)
+ { setText(text); }
+ void operator=(const std::list<std::string> &text)
+ { setText(text); }
+
+ /** */
+ static const TranslatedText notext;
+
+ public:
+
+ /** Synonym for \ref text */
+ std::string asString( const LanguageCode &lang = LanguageCode() ) const
+ { return text(lang); }
+
std::string text( const LanguageCode &lang = LanguageCode() ) const;
- //operator string() const;
+
void setText( const std::string &text, const LanguageCode &lang = LanguageCode());
void setText( const std::list<std::string> &text, const LanguageCode &lang = LanguageCode());
+
LanguageCode detectLanguage() const;
+
private:
- class Private;
- Private *d;
+ /** Pointer to implementation */
+ RWCOW_pointer<Impl> _pimpl;
};
///////////////////////////////////////////////////////////////////
- /** \relates Date Stream output */
+ /** \relates TranslatedText Stream output */
inline std::ostream & operator<<( std::ostream & str, const TranslatedText & obj )
{ return str << obj.asString(); }
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
-#endif // ZYPP_TRANSLATED_H
+#endif // ZYPP_TRANSLATEDTEXT_H
///////////////////////////////////////////////////////////////////
/** \name Join. */
//@{
- /** Join \a words_r into a single line
- */
- template <class _Input>
- std::string join( const _Input & words_r,
- const std::string & sep_r )
- {
- if ( words_r.empty() )
- return "";
-
- std::string ret( words_r[0] );
-
- for ( unsigned i = 1; i < words_r.size(); ++i ) {
- ret += sep_r + words_r[i];
- }
-
- return ret;
+ /** Join strings using separator \sep_r (defaults to BLANK). */
+ template <class _Iterator>
+ std::string join( _Iterator begin, _Iterator end,
+ const std::string & sep_r = " " )
+ {
+ std::string res;
+ for ( _Iterator iter = begin; iter != end; ++ iter )
+ {
+ if ( iter != begin )
+ res += sep_r;
+ res += *iter;
+ }
+ return res;
}
+
+ /** Join strings using separator \sep_r (defaults to BLANK). */
+ template <class _Container>
+ std::string join( const _Container & cont_r,
+ const std::string & sep_r = " " )
+ { return join( cont_r.begin(), cont_r.end() ); }
//@}