home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / numeral.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  18.1 KB  |  512 lines

  1. #ifndef __NUMERAL_H
  2. #define __NUMERAL_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. /***************************************************************************
  6.  *
  7.  * numeral - Declarations for the Standard Library numeric facets
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  12.  *
  13.  * This computer software is owned by Rogue Wave Software, Inc. and is
  14.  * protected by U.S. copyright laws and other laws and by international
  15.  * treaties.  This computer software is furnished by Rogue Wave Software,
  16.  * Inc. pursuant to a written license agreement and may be used, copied,
  17.  * transmitted, and stored only in accordance with the terms of such
  18.  * license and with the inclusion of the above copyright notice.  This
  19.  * computer software or any other copies thereof may not be provided or
  20.  * otherwise made available to any other person.
  21.  *
  22.  * U.S. Government Restricted Rights.  This computer software is provided
  23.  * with Restricted Rights.  Use, duplication, or disclosure by the
  24.  * Government is subject to restrictions as set forth in subparagraph (c)
  25.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  26.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  27.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  28.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  29.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  30.  *
  31.  **************************************************************************/
  32.  
  33. #ifndef __STD_NUMERAL__
  34. #define __STD_NUMERAL__ 1
  35.  
  36. #ifndef __STD_RWLOCALE__
  37. #include <rw/rwlocale> 
  38. #endif
  39.  
  40. #ifndef __STD_IOSBASE__
  41. #include <rw/iosbase> 
  42. #endif
  43.  
  44. #ifndef __STD_LIMITS
  45. #include <limits>
  46. #endif
  47.  
  48. #ifndef _RWSTD_NO_NAMESPACE
  49. namespace __rwstd {
  50. #endif
  51.  
  52. // ------------------------------------------------------
  53. // Implementation class template -- numpunct_init<charT>.
  54. // ------------------------------------------------------
  55.  
  56. // Structure used to initialize a rwstd::numpunct_data.
  57.  
  58. template <class charT>
  59. class _RWSTDExportTemplate numpunct_init {
  60.  public:
  61.   bool del_;                    // Kill the bearer of this message
  62.   charT dp_, ts_;               // Decimal point and thousands separator
  63.   const char *gr_;              // Digit grouping rule
  64.   const charT *tn_, *fn_;       // Strings for boolean true and false
  65. };
  66.  
  67. // --------------------------------------
  68. // Implementation class -- numpunct_base.
  69. // --------------------------------------
  70.  
  71. // Contains parts of numpunct<charT> that don't depend on the charT template
  72. // parameter.
  73.  
  74. class _RWSTDExport numpunct_base {
  75.  public:
  76.   static numpunct_init<char> *get_named_init_ (const char*);
  77. };
  78.  
  79. // ------------------------------------------------------
  80. // Implementation class template -- numpunct_data<charT>.
  81. // ------------------------------------------------------
  82.  
  83. // numpunct<charT> derives from this (via rwstd::numpunct_impl) to get its
  84. // private data members.
  85.  
  86. template <class charT>
  87. class _RWSTDExportTemplate numpunct_data :
  88.     public numpunct_base,
  89.     public punct_data<charT>
  90. {
  91. #ifndef _RWSTD_NO_NAMESPACE
  92.   friend class _RW_STD::numpunct<charT>;
  93. #else
  94.   friend class numpunct<charT>;
  95. #endif
  96.   friend class keyword_cracker<charT>;
  97.  
  98.   typedef _RW_STD::basic_string<charT,_RW_STD::char_traits<charT>,_RW_STD::allocator<charT> > string_type;
  99.  
  100.   string_type tn_, fn_;
  101.  
  102.   keyword_def<charT> tf_defs_[2];
  103.   keyword_map<charT> tf_map_;
  104.  
  105.  protected:
  106.   inline numpunct_data (const numpunct_init<charT>*);
  107.   numpunct_init<charT> *get_init_by_name_ (const char*);
  108.   void __initfacetbase (const locale*);
  109. };
  110.  
  111. template <class charT>
  112. inline numpunct_data<charT>::numpunct_data
  113.     (const numpunct_init<charT> *init)
  114. {
  115.   if (!init) {
  116.     this->dp_=charT('.');
  117.     this->ts_=charT(',');
  118.   } else {
  119.     this->dp_=init->dp_;
  120.     this->ts_=init->ts_;
  121.     this->gr_=init->gr_;
  122.     tn_=init->tn_;
  123.     fn_=init->fn_;
  124.  
  125.     if (init->del_)
  126.       delete[] (char*) init;
  127.   }
  128. }
  129.  
  130. template <class charT>
  131. numpunct_init<charT>* _RWSTDExportTemplate
  132. fixup_numpunct_init
  133.     (numpunct_init<char>*,charT*);
  134.  
  135. #ifndef _RWSTD_NO_NEW_TEMPLATE_SYNTAX
  136. template <>
  137. inline numpunct_init<char>* 
  138. fixup_numpunct_init<char>(numpunct_init<char> *init,char*)
  139. #else
  140. inline numpunct_init<char>* 
  141. fixup_numpunct_init(numpunct_init<char> *init,char*)
  142. #endif // _RWSTD_NO_NEW_TEMPLATE_SYNTAX
  143. {  return init; }
  144.  
  145. // ------------------------------------------------------
  146. // Implementation class template -- numpunct_impl<charT>.
  147. // ------------------------------------------------------
  148.  
  149. // numpunct<charT> derives from this to obtain the part of its behavior that
  150. // must be specialized for char and wchar_t.  This lets us avoid specializing
  151. // the whole numpunct<charT> template.  Currently the only specialized behavior
  152. // is the initialization of private data members in the constructor.
  153.  
  154. template <class charT>
  155. class _RWSTDExportTemplate numpunct_impl :
  156.     public numpunct_data<charT>
  157. {
  158.  protected:
  159.   numpunct_impl (const numpunct_init<charT>* init)
  160.    : numpunct_data<charT>(init) { }
  161.  public:
  162.   static const numpunct_init<charT> *get_ivals_ ()
  163.     { return NULL; }
  164. };
  165.  
  166. _RWSTD_TEMPLATE
  167. class _RWSTDExport numpunct_impl<char>:                  // Specialization
  168.     public numpunct_data<char>
  169. {
  170. #if !defined(_MSC_VER) || defined(__BORLANDC__)
  171.   static _RWSTDExport numpunct_init<char> ivals_;        // Vendor-supplied
  172. #else
  173.   static numpunct_init<char> ivals_;
  174. #endif
  175.  
  176.  protected:
  177.   numpunct_impl
  178.       (const numpunct_init<char> *init):
  179.        numpunct_data<char>(init) { }
  180.  public:
  181.   static const numpunct_init<char> *get_ivals_ ()
  182.     { return &ivals_; }
  183. };
  184.  
  185. #ifndef _RWSTD_NO_WIDE_CHAR
  186. _RWSTD_TEMPLATE
  187. class _RWSTDExport numpunct_impl<wchar_t>:               // Specialization
  188.     public numpunct_data<wchar_t>
  189. {
  190. #if !defined(_MSC_VER) || defined(__BORLANDC__)
  191.   static _RWSTDExport numpunct_init<wchar_t> ivals_;     // Vendor-supplied
  192. #else
  193.   static numpunct_init<wchar_t> ivals_;
  194. #endif
  195.  
  196.  protected:
  197.   numpunct_impl
  198.       (const numpunct_init<wchar_t> *init):
  199.        numpunct_data<wchar_t>(init) { }
  200.  public:
  201.   static const numpunct_init<wchar_t> *get_ivals_ ()
  202.     { return &ivals_; }
  203. };
  204. #endif // _RWSTD_NO_WIDE_CHAR
  205.  
  206. #ifndef _RWSTD_NO_NAMESPACE
  207. } namespace std {
  208. #endif
  209.  
  210. // ---------------------------------------------------------------
  211. // Standard numeric parsing facet -- num_get<charT,InputIterator>.
  212. // ---------------------------------------------------------------
  213.  
  214. template <class charT, class InputIterator>
  215. class _RWSTDExportTemplate num_get: public locale::facet
  216. {
  217.  public:
  218.   typedef charT char_type;
  219.   typedef InputIterator iter_type;
  220.  
  221.   _EXPLICIT num_get (size_t refs=0): locale::facet(refs,locale::numeric) { }
  222.  
  223. #ifndef _RWSTD_NO_BOOL
  224.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  225.                  bool& v) const            { return do_get(i,e,f,err,v); }
  226. #endif
  227.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  228.                  long& v) const            { return do_get(i,e,f,err,v); }
  229.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  230.                  unsigned short& v) const  { return do_get(i,e,f,err,v); }
  231.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  232.                  unsigned int& v) const    { return do_get(i,e,f,err,v); }
  233.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  234.                  unsigned long& v) const   { return do_get(i,e,f,err,v); }
  235.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  236.                  float& v) const           { return do_get(i,e,f,err,v); }
  237.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  238.                  double& v) const          { return do_get(i,e,f,err,v); }
  239.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  240.                  long double& v) const     { return do_get(i,e,f,err,v); }
  241.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  242.                  void*& p) const           { return do_get(i,e,f,err,p); }
  243.  
  244.   static locale::id id;
  245.  
  246.   // Extension for compilers that have an extra-long integer type:
  247. #ifdef _RWSTD_LONG_LONG
  248.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  249.                  _RWSTD_LONG_LONG& v) const 
  250.   { return do_get(i,e,f,err,v); }
  251.   iter_type get (iter_type i, iter_type e, ios_base &f, ios_base::iostate &err,
  252.                  unsigned _RWSTD_LONG_LONG& v) const 
  253.   { return do_get(i,e,f,err,v); }
  254. #endif
  255.  
  256.   // Implementation:
  257.   enum { __facet_cat = locale::numeric, __ok_implicit = 1 };
  258.  
  259.  protected:
  260.   virtual ~num_get();
  261.  
  262. #ifndef _RWSTD_NO_BOOL
  263.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  264.                             ios_base::iostate& err, bool& v) const;
  265. #endif
  266.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  267.                             ios_base::iostate& err, long& v) const;
  268.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  269.                             ios_base::iostate& err, unsigned short& v) const;
  270.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  271.                             ios_base::iostate& err, unsigned int& v) const;
  272.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  273.                             ios_base::iostate& err, unsigned long& v) const;
  274.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  275.                             ios_base::iostate& err, float& v) const;
  276.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  277.                             ios_base::iostate& err, double& v) const;
  278.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  279.                             ios_base::iostate& err, long double& v) const;
  280.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  281.                             ios_base::iostate& err, void*& p) const;
  282.  
  283. #ifdef _RWSTD_LONG_LONG
  284.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  285.                             ios_base::iostate& err, 
  286.                             _RWSTD_LONG_LONG& v) const;
  287.   virtual iter_type do_get (iter_type, iter_type, ios_base&,
  288.                             ios_base::iostate& err, 
  289.                             unsigned _RWSTD_LONG_LONG& v) const;
  290. #endif
  291.  
  292.   // Implementation:
  293.  
  294.  private:
  295.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  296.   locale::id &__get_id (void) const { return id; }
  297.   #endif
  298. };
  299.  
  300. // -------------------------------------------------------------------
  301. // Standard numeric formatting facet -- num_put<charT,OutputIterator>.
  302. // -------------------------------------------------------------------
  303.  
  304. template <class charT, class OutputIterator>
  305. class _RWSTDExportTemplate num_put: public locale::facet {
  306.  public:
  307.   typedef charT char_type;
  308.   typedef OutputIterator iter_type;
  309.  
  310.   _EXPLICIT num_put (size_t refs=0): locale::facet(refs,locale::numeric) { }
  311.  
  312. #ifndef _RWSTD_NO_BOOL
  313.   iter_type put (iter_type s, ios_base& f, char_type fill, bool v)
  314.                  const { return do_put(s,f,fill,v); }
  315. #endif
  316.   iter_type put (iter_type s, ios_base& f, char_type fill, long v)
  317.                  const { return do_put(s,f,fill,v); }
  318.   iter_type put (iter_type s, ios_base& f, char_type fill, unsigned long v)
  319.                  const { return do_put(s,f,fill,v); }
  320.   iter_type put (iter_type s, ios_base& f, char_type fill, double v)
  321.                  const { return do_put(s,f,fill,v); }
  322.   iter_type put (iter_type s, ios_base& f, char_type fill, long double v)
  323.                  const { return do_put(s,f,fill,v); }
  324.   iter_type put (iter_type s, ios_base& f, char_type fill, const void* p)
  325.                  const { return do_put(s,f,fill,p); }
  326.  
  327. #ifndef _RWSTD_STRICT_ANSI
  328.   // Rogue Wave extensions.
  329. #ifdef __TURBOC__
  330.   iter_type put (iter_type s, ios_base& f, char_type fill, float v)
  331.                  const { return do_put(s,f,fill,(double)v); }
  332. #endif
  333.   iter_type put (iter_type s, ios_base& f, char_type fill, short v)
  334.                  const { return do_put(s,f,fill,v); }
  335.   iter_type put (iter_type s, ios_base& f, char_type fill, unsigned short v)
  336.                  const { return do_put(s,f,fill,v); }
  337.   iter_type put (iter_type s, ios_base& f, char_type fill, int v)
  338.                  const { return do_put(s,f,fill,v); }
  339.   iter_type put (iter_type s, ios_base& f, char_type fill, unsigned int v)
  340.                  const { return do_put(s,f,fill,v); }
  341. #endif
  342.  
  343. #ifdef _RWSTD_LONG_LONG
  344.   iter_type put (iter_type s, ios_base& f, char_type fill, 
  345.                  _RWSTD_LONG_LONG v) const 
  346.   { return do_put(s,f,fill,v); }
  347.   iter_type put (iter_type s, ios_base& f, char_type fill, 
  348.                  unsigned _RWSTD_LONG_LONG v) const 
  349.   { return do_put(s,f,fill,v); }
  350. #endif
  351.  
  352.   static locale::id id;
  353.  
  354.   // Implementation:
  355.   enum { __facet_cat = locale::numeric, __ok_implicit = 1 };
  356.  
  357.  protected:
  358.   virtual ~num_put();
  359.  
  360. #ifndef _RWSTD_NO_BOOL
  361.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  362.                             bool v) const;
  363. #endif
  364.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  365.                             long v) const;
  366.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  367.                             unsigned long v) const;
  368.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  369.                             double v) const;
  370.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  371.                             long double v) const;
  372.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  373.                             const void* p) const;
  374.  
  375. #ifndef _RWSTD_STRICT_ANSI
  376.   // Rogue Wave extensions.
  377.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  378.                             short v) const;
  379.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  380.                             unsigned short v) const;
  381.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  382.                             int v) const;
  383.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  384.                             unsigned int v) const;
  385. #endif
  386.  
  387. #ifdef _RWSTD_LONG_LONG
  388.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  389.                             _RWSTD_LONG_LONG v) const;
  390.   virtual iter_type do_put (iter_type, ios_base&, char_type fill,
  391.                             unsigned _RWSTD_LONG_LONG v) const;
  392. #endif
  393.  
  394.   // Implementation.
  395.  
  396.  private:
  397.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  398.   locale::id &__get_id (void) const { return id; }
  399.   #endif
  400. };
  401.  
  402. // ------------------------------------------------------
  403. // Standard numeric punctuation facet -- numpunct<charT>.
  404. // ------------------------------------------------------
  405.  
  406. template <class charT>
  407. class _RWSTDExportTemplate numpunct: public locale::facet,
  408.     public __RWSTD::numpunct_impl<charT>
  409. {
  410.  public:
  411.   typedef charT char_type;
  412.   typedef basic_string<charT,char_traits<charT>,allocator<charT> >
  413.     string_type;
  414.  
  415.   // The second parameter (i) to the constructor is implementation specific.
  416.   // For portable code, always let it default as shown.
  417.   _EXPLICIT numpunct (size_t refs=0,
  418. #if !defined(_MSC_VER) || defined(__BORLANDC__)
  419.       const __RWSTD::numpunct_init<charT> *i = __RWSTD::numpunct_impl<charT>::get_ivals_());
  420. #else
  421.       const __RWSTD::numpunct_init<charT> *i = get_ivals_());
  422. #endif
  423.   char_type   decimal_point() const { return do_decimal_point(); }
  424.   char_type   thousands_sep() const { return do_thousands_sep(); }
  425.   string      grouping()      const { return do_grouping(); }
  426.   string_type truename()      const { return do_truename(); }
  427.   string_type falsename()     const { return do_falsename(); }
  428.  
  429.   static locale::id id;
  430.  
  431.   // Implementation:
  432.   enum { __facet_cat = locale::numeric, __ok_implicit = 1 };
  433.  
  434.  protected:
  435.   virtual ~numpunct();
  436.  
  437.   virtual char_type   do_decimal_point() const;
  438.   virtual char_type   do_thousands_sep() const;
  439.   virtual string      do_grouping()      const;
  440.   virtual string_type do_truename()      const;
  441.   virtual string_type do_falsename()     const;
  442.  
  443.   // Implementation:
  444.  
  445.  private:
  446.   void __initfacet (const locale*);
  447.  
  448.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  449.   locale::id &__get_id (void) const { return id; }
  450.   #endif
  451. };
  452.  
  453. template <class charT>
  454. inline numpunct<charT>::numpunct
  455.     (size_t refs,const __RWSTD::numpunct_init<charT> *init):
  456.      locale::facet(refs,locale::numeric),
  457.      __RWSTD::numpunct_impl<charT>(init)
  458. { }
  459.  
  460. // -------------------------------------------------
  461. // Standard derived facet -- numpunct_byname<charT>.
  462. // -------------------------------------------------
  463.  
  464. template <class charT>
  465. class numpunct_byname: public numpunct<charT> {
  466.  public:
  467.   _EXPLICIT numpunct_byname (const char*, size_t refs=0);
  468.  
  469.  protected:
  470.   virtual ~numpunct_byname();
  471.  
  472. // Virtual member functions inherited from numpunct<charT>:
  473. // virtual char_type   do_decimal_point() const;
  474. // virtual char_type   do_thousands_sep() const;
  475. // virtual string      do_grouping() const;
  476. // virtual string_type do_truename() const;
  477. // virtual string_type do_falsename() const;
  478. };
  479.  
  480. #ifndef _RWSTD_NO_NAMESPACE
  481. } namespace __rwstd {
  482. #endif
  483.  
  484. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  485. template <class charT>
  486. inline _RW_STD::numpunct<charT>* create_named_facet
  487.     (_RW_STD::numpunct<charT>*,const char *name,size_t refs)
  488. { return new _RW_STD::numpunct_byname<charT>(name,refs); }
  489. #else
  490.  
  491. inline _RW_STD::numpunct<char>* create_named_facet
  492.     (_RW_STD::numpunct<char>*,const char *name,size_t refs)
  493. { return new _RW_STD::numpunct_byname<char>(name,refs); }
  494.  
  495. #ifndef _RWSTD_NO_WIDE_CHAR
  496. inline _RW_STD::numpunct<wchar_t>* create_named_facet
  497.     (_RW_STD::numpunct<wchar_t>*,const char *name,size_t refs)
  498. { return new _RW_STD::numpunct_byname<wchar_t>(name,refs); }
  499. #endif
  500. #endif
  501.  
  502. #ifndef _RWSTD_NO_NAMESPACE
  503. }
  504. #endif
  505.  
  506. #ifdef _RWSTD_COMPILE_INSTANTIATE
  507. #include <rw/numeral.cc>
  508. #endif
  509. #endif // __STD_NUMERAL__
  510. #pragma option pop
  511. #endif /* __NUMERAL_H */
  512.