home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / src / locale.cpp < prev    next >
C/C++ Source or Header  |  2001-04-13  |  3KB  |  121 lines

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