string_conversions.h

Go to the documentation of this file.
00001 #ifndef STRING_CONVERSIONS_GROUP
00002 #define STRING_CONVERSIONS_GROUP
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : string_conversions                                                *
00007 *  Author : Various                                                           *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2007-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00019 
00026 using namespace System;
00027 using namespace System::Collections;
00028 
00029 #include <basis/istring.h>
00030 
00031 namespace hoople_api {
00032 
00034   public ref class string_conversions
00035   {
00036   public:
00038 
00040       static void convert(const String ^to_convert, istring &result) {
00041         result.reset();
00042         if (const_cast<String ^>(to_convert)->Length > 0) {
00043           // transcode the internal string down to utf8.
00044           cli::array<Byte> ^bytes = System::Text::Encoding::UTF8->GetBytes
00045               (const_cast<String ^>(to_convert));
00046           pin_ptr<const byte> value = &bytes[0];  // trap the memory for a sec.
00047           result = (const char *)value;
00048           value = nullptr;  // deallocate what we had pinned.
00049         }
00050       }
00051 
00053       static void convert(const istring &to_convert, String ^ %result) {
00054         result = nullptr;  // shake loose anything stored already.
00055         result = gcnew String(to_convert.observe());
00056       }
00057 
00058       // functional methods return the object of the type specified.
00059       // this is not as efficient as the procedural form above, but this
00060       // form lends itself to a functional programming style better.
00061 
00063       static istring to_istring(const String ^to_convert) {
00064         istring to_return;
00065         convert(to_convert, to_return);
00066         return to_return;
00067       }
00068 
00070       static String ^ to_mstring(const istring &to_convert) {
00071         String ^ to_return;
00072         convert(to_convert, to_return);
00073         return to_return;
00074       }
00075   };
00076 }
00077 
00078 #endif
00079 

Generated on Fri Nov 21 04:29:45 2008 for HOOPLE Libraries by  doxygen 1.5.1