os/ossrv/stdcpp/src/locale.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/src/locale.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,139 @@
     1.4 +/*
     1.5 + * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     1.6 + * Copyright (c) 1999
     1.7 + * Silicon Graphics Computer Systems, Inc.
     1.8 + *
     1.9 + * Copyright (c) 1999 
    1.10 + * Boris Fomitchev
    1.11 + *
    1.12 + * This material is provided "as is", with absolutely no warranty expressed
    1.13 + * or implied. Any use is at your own risk.
    1.14 + *
    1.15 + * Permission to use or copy this software for any purpose is hereby granted 
    1.16 + * without fee, provided the above notices are retained on all copies.
    1.17 + * Permission to modify the code and to distribute modified code is granted,
    1.18 + * provided the above notices are retained, and a notice that the code was
    1.19 + * modified is included with the above copyright notice.
    1.20 + *
    1.21 + */ 
    1.22 +# include "stlport_prefix.h"
    1.23 +
    1.24 +#include <locale>
    1.25 +#include <stdexcept>
    1.26 +#include <stl/_algobase.h>
    1.27 +
    1.28 +#include "locale_nonclassic.h"
    1.29 +
    1.30 +#if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    1.31 +#include "libstdcppwsd.h"
    1.32 +# endif
    1.33 +
    1.34 +_STLP_BEGIN_NAMESPACE
    1.35 +
    1.36 +_Locale::_Locale(const _Locale_impl& L)
    1.37 +  : _Locale_impl(L), _Refcount_Base(1), facets_vec((void**)L.facets, (void**)L.facets+L.size())
    1.38 +{
    1.39 +  for (size_t i = 1; i < L.size(); ++i) {
    1.40 +    locale::facet* f = L.facets[i];
    1.41 +    if (f && f->_M_delete)
    1.42 +      f->_M_incr();
    1.43 +  }
    1.44 +  facets = (locale::facet**)&facets_vec[0];
    1.45 +  _M_size = facets_vec.size();
    1.46 +}
    1.47 +
    1.48 +_Locale::~_Locale() {
    1.49 +  size_t sz = facets_vec.size();
    1.50 +  for (size_t i = 1; i < sz ; ++i)
    1.51 +    this->remove(i);
    1.52 +}
    1.53 +
    1.54 +void _Locale::remove(size_t index) {
    1.55 +  if (index > 0 && index < facets_vec.size()) {
    1.56 +    locale::facet* old = (locale::facet*)facets_vec[index];
    1.57 +    if (old && old->_M_delete) {
    1.58 +      old->_M_decr(); 
    1.59 +      if (old->_M_ref_count == 0)
    1.60 +        delete old;
    1.61 +    }
    1.62 +    facets_vec[index] = 0;
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +locale::facet*
    1.67 +_Locale::insert(locale::facet* f, size_t index, bool do_incr) {
    1.68 +  if (f != 0 && index != 0) {
    1.69 +    if (index >= facets_vec.size()) {
    1.70 +      facets_vec.insert(facets_vec.end(),
    1.71 +                        index - facets_vec.size() + 1, (void*) 0);
    1.72 +      facets = (locale::facet**)&facets_vec[0];
    1.73 +      _M_size = facets_vec.size();
    1.74 +    }
    1.75 +    if (do_incr)
    1.76 +      f->_M_incr();
    1.77 +    
    1.78 +    remove(index);
    1.79 +    facets_vec[index] = (void*)f;
    1.80 +    return f;
    1.81 +  }
    1.82 +  else
    1.83 +    return 0;
    1.84 +}
    1.85 +
    1.86 +void _Locale::insert(_Locale_impl* from, const locale::id& n) {
    1.87 +  size_t index = n._M_index;
    1.88 +  this->remove(index);
    1.89 +  if (index > 0 && index < from->size())
    1.90 +    this->insert(from->facets[index], index, true);
    1.91 +}
    1.92 +
    1.93 +#if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    1.94 +void locale_mutex_lock_init()
    1.95 +{
    1.96 +   get_locale_Index_lock()._M_lock.iState = _ENeedsNormalInit;
    1.97 +   get_locale_Index_lock()._M_lock.iPtr = 0;
    1.98 +   get_locale_Index_lock()._M_lock.iReentry = 0;  	
    1.99 +}   
   1.100 +# else 
   1.101 +static _STLP_STATIC_MUTEX _Index_lock _STLP_MUTEX_INITIALIZER;
   1.102 +# endif
   1.103 +
   1.104 +
   1.105 +// Takes a reference to a locale::id, and returns its numeric index.
   1.106 +// If no numeric index has yet been assigned, assigns one.  The return
   1.107 +// value is always positive.
   1.108 +static size_t _Stl_loc_get_index(locale::id& id)
   1.109 +{
   1.110 +  if (id._M_index == 0) {
   1.111 +#if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
   1.112 +    _STLP_auto_lock sentry(get_locale_Index_lock());
   1.113 +    size_t new_index = get_locale_id_S_max()++;
   1.114 +# else
   1.115 +	_STLP_auto_lock sentry(_Index_lock);    
   1.116 +    size_t new_index = locale::id::_S_max++;
   1.117 +# endif
   1.118 +    id._M_index = new_index;
   1.119 +  }
   1.120 +  return id._M_index;
   1.121 +}
   1.122 +
   1.123 +_STLP_EXP_DECLSPEC void locale::_M_insert(facet* f, locale::id& n)
   1.124 +{
   1.125 +  if (f)
   1.126 +    ((_Locale*)_M_impl)->insert(f, _Stl_loc_get_index(n), false);
   1.127 +}
   1.128 +
   1.129 +
   1.130 +// Make a locale directly from a _Locale_impl object.  If the second argument
   1.131 +// is true, we clone the _Locale_impl.  If false, we just twiddle pointers.
   1.132 +_STLP_EXP_DECLSPEC locale::locale(_Locale_impl* impl, bool do_copy)
   1.133 +  : _M_impl(0)
   1.134 +{
   1.135 +  if (do_copy) {
   1.136 +    _M_impl = new _Locale(*impl);
   1.137 +    _M_impl->name = "*";
   1.138 +  } else
   1.139 +    _M_impl = _S_copy_impl(impl);
   1.140 +}
   1.141 +
   1.142 +_STLP_END_NAMESPACE