Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SmartBSTR.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Characters_Platform_Windows_SmartBSTR_h_
5#define _Stroika_Foundation_Characters_Platform_Windows_SmartBSTR_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#if qStroika_Foundation_Common_Platform_Windows
10#include <Windows.h>
11
12#include <OAIdl.h>
13#include <wtypes.h>
14#else
15#error "WINDOWS REQUIRED FOR THIS MODULE"
16#endif
17
18#include "Stroika/Foundation/Common/Common.h"
20
21/**
22 * TODO:
23 *
24 * @todo Cleanup - moving IMPL to INL file, and perhaps adding move CTOR etc...
25 * AND throw in ThrowIfNull calls if allocs fail, and fix Require on const wchar_t* so we require before dereference!
26 *
27 */
28
30
31 /**
32 * Avoid dependency on CComBSTR since its part of ATL, and MSFT doesn't distribute that in Visual Studio Express.
33 * Avoid _bstr_t since I'm not sure thats portable to other SDK implementations (e.g. Mingw?? not sure)
34 */
35 class SmartBSTR {
36 public:
37 SmartBSTR () = default;
38 SmartBSTR (nullptr_t)
39 {
40 }
41 SmartBSTR (const wchar_t* from)
42 : fStr_ (::SysAllocString (from))
43 {
44 RequireNotNull (from);
45 }
46 ~SmartBSTR ()
47 {
48 if (fStr_ != nullptr) {
49 ::SysFreeString (fStr_);
50 }
51 }
52 SmartBSTR& operator= (const SmartBSTR& rhs)
53 {
54 if (fStr_ != nullptr) {
55 ::SysFreeString (fStr_);
56 fStr_ = NULL;
57 }
58 if (rhs.fStr_ != nullptr) {
59 fStr_ = ::SysAllocString (rhs.fStr_);
60 }
61 return *this;
62 }
63 operator BSTR () const noexcept
64 {
65 return fStr_;
66 }
67 unsigned int Length () const noexcept
68 {
69 return ::SysStringLen (fStr_);
70 }
71
72 private:
73 BSTR fStr_ = nullptr;
74 };
75
76}
77
78/*
79 ********************************************************************************
80 ***************************** Implementation Details ***************************
81 ********************************************************************************
82 */
83
84#endif /*_Stroika_Foundation_Characters_Platform_Windows_SmartBSTR_h_*/
#define RequireNotNull(p)
Definition Assertions.h:347