OSDN Git Service

基本ファイルを追加
[winaudioj/sfwasapi.git] / sfwasapi / code_converter.h
1 #pragma once
2
3
4 /** @file
5  *  @author S.F. (Satoshi Fujiwara)
6  *  @brief wchar_t <--> char \82Ì\95Ï\8a·\83\86\81[\83e\83B\83\8a\83e\83B
7  *  \88ø\90\94\82Æ\82µ\82Ä\95\8e\9a\97ñ\82ð\88ø\82«\93n\82·\8dÛ\81A\95Ï\8a·\82ª\95K\97v\82È\8fê\8d\87\81A\89º\8bL\82Ì\82æ\82¤\82É\83A\83_\83v\83^\93I\82É\8eg\97p\82·\82é\82±\82Æ\82ð\91z\92è\82µ\82Ä\82¢\82é\81B
8  *  std::wstring a(L"abcd");
9  *  std::wstring b((sf::code_converter<wcha_t,char>(a)));
10  *
11  *\81@\8cã\81AATL\83R\81[\83h\8d·\82µ\91Ö\82¦\97p\82Ìtypedef\82à\97p\88Ó\82µ\82Ä\82¢\82é\81B
12  *  \82±\82ê\82Í\81A\83A\83_\83v\83^\82Æ\82µ\82Ä\82Ì\97\98\97p\82Ì\82Ý\82É\8cÀ\92è\82³\82ê\82é\81B
13  *  sf::ct2a hoge_(L"abcef"); // \91z\92è\82µ\82Ä\82¢\82È\82¢\81i\93®\8dì\82·\82é\8fê\8d\87\82à\82 \82é\82µ\81A\83_\83\81\82È\8fê\8d\87\82à\82 \82é\81j
14  */
15
16 namespace sf 
17 {
18     /** \90\97\8c` */
19     template <typename SrcChar,typename DestChar>
20     struct code_converter
21     {
22     public:
23         explicit code_converter(SrcChar* p);
24         explicit code_converter(std::basic_string<SrcChar> & p);
25         operator DestChar*();
26     };
27
28     /** char -> wchar_t\82Ö\82Ì\95Ï\8a· */
29     template <>
30     struct code_converter<char,wchar_t>
31     {
32         explicit code_converter(const char* p);
33         explicit code_converter(const std::string& p);
34
35         operator wchar_t*() const {return m_dest.get();}
36     private:
37         boost::scoped_array<wchar_t> m_dest;
38     };
39
40     /** wchar_t -> char */
41     template <>
42     struct code_converter<wchar_t,char>
43     {
44         explicit code_converter(const wchar_t* p);
45         explicit code_converter(const std::wstring& p);
46         operator char* () const {return m_dest.get();}
47     private:
48         boost::scoped_array<char> m_dest;
49     };
50
51     /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
52     typedef sf::code_converter<char,wchar_t> ca2w;
53
54     /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
55     typedef sf::code_converter<wchar_t,char> cw2a;
56
57     #ifdef _UNICODE
58
59         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
60         typedef sf::code_converter<char,wchar_t> ca2t;
61         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
62         typedef sf::code_converter<wchar_t,char> ct2a;
63
64         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
65         inline const wchar_t* ct2w(const wchar_t* p) { return p;};
66         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
67         inline const wchar_t* cw2t(const wchar_t* p) { return p;};
68
69     #else
70
71         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
72         inline const char* ct2a(const char* p) { return p;};
73         /** ATL\83R\81[\83h\8d·\82µ\91Ö\82¦\82Ì\82½\82ß\82Ì\83G\83C\83\8a\83A\83X */
74         inline const char* ca2t(const char* p) { return p;};
75
76     #endif
77 }
78
79