stdcpp/src/locale.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
       
     3  * Copyright (c) 1999
       
     4  * Silicon Graphics Computer Systems, Inc.
       
     5  *
       
     6  * Copyright (c) 1999 
       
     7  * Boris Fomitchev
       
     8  *
       
     9  * This material is provided "as is", with absolutely no warranty expressed
       
    10  * or implied. Any use is at your own risk.
       
    11  *
       
    12  * Permission to use or copy this software for any purpose is hereby granted 
       
    13  * without fee, provided the above notices are retained on all copies.
       
    14  * Permission to modify the code and to distribute modified code is granted,
       
    15  * provided the above notices are retained, and a notice that the code was
       
    16  * modified is included with the above copyright notice.
       
    17  *
       
    18  */ 
       
    19 # include "stlport_prefix.h"
       
    20 
       
    21 #include <locale>
       
    22 #include <stdexcept>
       
    23 #include <stl/_algobase.h>
       
    24 
       
    25 #include "locale_nonclassic.h"
       
    26 
       
    27 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
       
    28 #include "libstdcppwsd.h"
       
    29 # endif
       
    30 
       
    31 _STLP_BEGIN_NAMESPACE
       
    32 
       
    33 _Locale::_Locale(const _Locale_impl& L)
       
    34   : _Locale_impl(L), _Refcount_Base(1), facets_vec((void**)L.facets, (void**)L.facets+L.size())
       
    35 {
       
    36   for (size_t i = 1; i < L.size(); ++i) {
       
    37     locale::facet* f = L.facets[i];
       
    38     if (f && f->_M_delete)
       
    39       f->_M_incr();
       
    40   }
       
    41   facets = (locale::facet**)&facets_vec[0];
       
    42   _M_size = facets_vec.size();
       
    43 }
       
    44 
       
    45 _Locale::~_Locale() {
       
    46   size_t sz = facets_vec.size();
       
    47   for (size_t i = 1; i < sz ; ++i)
       
    48     this->remove(i);
       
    49 }
       
    50 
       
    51 void _Locale::remove(size_t index) {
       
    52   if (index > 0 && index < facets_vec.size()) {
       
    53     locale::facet* old = (locale::facet*)facets_vec[index];
       
    54     if (old && old->_M_delete) {
       
    55       old->_M_decr(); 
       
    56       if (old->_M_ref_count == 0)
       
    57         delete old;
       
    58     }
       
    59     facets_vec[index] = 0;
       
    60   }
       
    61 }
       
    62 
       
    63 locale::facet*
       
    64 _Locale::insert(locale::facet* f, size_t index, bool do_incr) {
       
    65   if (f != 0 && index != 0) {
       
    66     if (index >= facets_vec.size()) {
       
    67       facets_vec.insert(facets_vec.end(),
       
    68                         index - facets_vec.size() + 1, (void*) 0);
       
    69       facets = (locale::facet**)&facets_vec[0];
       
    70       _M_size = facets_vec.size();
       
    71     }
       
    72     if (do_incr)
       
    73       f->_M_incr();
       
    74     
       
    75     remove(index);
       
    76     facets_vec[index] = (void*)f;
       
    77     return f;
       
    78   }
       
    79   else
       
    80     return 0;
       
    81 }
       
    82 
       
    83 void _Locale::insert(_Locale_impl* from, const locale::id& n) {
       
    84   size_t index = n._M_index;
       
    85   this->remove(index);
       
    86   if (index > 0 && index < from->size())
       
    87     this->insert(from->facets[index], index, true);
       
    88 }
       
    89 
       
    90 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
       
    91 void locale_mutex_lock_init()
       
    92 {
       
    93    get_locale_Index_lock()._M_lock.iState = _ENeedsNormalInit;
       
    94    get_locale_Index_lock()._M_lock.iPtr = 0;
       
    95    get_locale_Index_lock()._M_lock.iReentry = 0;  	
       
    96 }   
       
    97 # else 
       
    98 static _STLP_STATIC_MUTEX _Index_lock _STLP_MUTEX_INITIALIZER;
       
    99 # endif
       
   100 
       
   101 
       
   102 // Takes a reference to a locale::id, and returns its numeric index.
       
   103 // If no numeric index has yet been assigned, assigns one.  The return
       
   104 // value is always positive.
       
   105 static size_t _Stl_loc_get_index(locale::id& id)
       
   106 {
       
   107   if (id._M_index == 0) {
       
   108 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
       
   109     _STLP_auto_lock sentry(get_locale_Index_lock());
       
   110     size_t new_index = get_locale_id_S_max()++;
       
   111 # else
       
   112 	_STLP_auto_lock sentry(_Index_lock);    
       
   113     size_t new_index = locale::id::_S_max++;
       
   114 # endif
       
   115     id._M_index = new_index;
       
   116   }
       
   117   return id._M_index;
       
   118 }
       
   119 
       
   120 _STLP_EXP_DECLSPEC void locale::_M_insert(facet* f, locale::id& n)
       
   121 {
       
   122   if (f)
       
   123     ((_Locale*)_M_impl)->insert(f, _Stl_loc_get_index(n), false);
       
   124 }
       
   125 
       
   126 
       
   127 // Make a locale directly from a _Locale_impl object.  If the second argument
       
   128 // is true, we clone the _Locale_impl.  If false, we just twiddle pointers.
       
   129 _STLP_EXP_DECLSPEC locale::locale(_Locale_impl* impl, bool do_copy)
       
   130   : _M_impl(0)
       
   131 {
       
   132   if (do_copy) {
       
   133     _M_impl = new _Locale(*impl);
       
   134     _M_impl->name = "*";
       
   135   } else
       
   136     _M_impl = _S_copy_impl(impl);
       
   137 }
       
   138 
       
   139 _STLP_END_NAMESPACE