Skip to content

Commit

Permalink
Start convert from C# to C++. Issue: linksplatform#48
Browse files Browse the repository at this point in the history
  • Loading branch information
VasiliyevAD committed Nov 10, 2021
1 parent 297341e commit c98e0b9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
26 changes: 20 additions & 6 deletions cpp/Platform.Converters/CachingConverterDecorator.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
namespace Platform::Converters
#pragma once
#include <map>
#include <memory>

namespace Platform::Converters
{
using namespace Platform::Collections::Dictionaries;
using namespace Platform::Interfaces;

template <typename ...> class CachingConverterDecorator;
template <typename TSource, typename TTarget> class CachingConverterDecorator<TSource, TTarget> : public IConverter<TSource, TTarget>
{
private: readonly IConverter<TSource, TTarget> *_baseConverter;
private: readonly IDictionary<TSource, TTarget> *_cache;
private: std::unique_ptr<IConverter<TSource, TTarget>> _baseConverter;
private: std::unique_ptr<IDictionary<TSource, TTarget>> _cache;

public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter, IDictionary<TSource, TTarget> &cache) { (_baseConverter, _cache) = (baseConverter, cache); }
public: CachingConverterDecorator(IConverter<TSource, TTarget>& baseConverter, IDictionary<TSource, TTarget>& cache)
: _baseConverter(baseConverter)
, _cache(cache)
{}

public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter) : this(baseConverter, Dictionary<TSource, TTarget>()) { }
public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter)
: _baseConverter(baseConverter)
{
_cache = std::make_unique<std::map<TSource, TTarget>>();
}

public: TTarget Convert(TSource source) { return _cache.GetOrAdd(source, _baseConverter.Convert); }
public: TTarget Convert(TSource source) { return GetOrAdd(_cache->get(), source, _baseConverter->Convert); }
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -138,12 +139,23 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CachingConverterDecorator.h" />
<ClInclude Include="CheckedConverter.h" />
<ClInclude Include="Convert.h" />
<ClInclude Include="Converter.h" />
<ClInclude Include="ConverterBase.h" />
<ClInclude Include="IConverter[TSource, TTarget].h" />
<ClInclude Include="IConverter[T].h" />
<ClInclude Include="To.h" />
<ClInclude Include="is_detected.h" />
<ClInclude Include="Platform.Converters.h" />
<ClInclude Include="StringConverter.h" />
<ClInclude Include="UncheckedConverter.h" />
<ClInclude Include="UncheckedSignExtendingConverter.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Platform.Converters.Benchmarks\Int32ToUInt64ConverterBenchmarks.cpp" />
<ClCompile Include="..\Platform.Converters.Benchmarks\Program.cpp" />
<ClCompile Include="..\Platform.Converters.Benchmarks\UInt64ToUInt64ConverterBenchmarks.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,40 @@
<ClInclude Include="StringConverter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="is_detected.h">
<ClInclude Include="To.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="To.h">
<ClInclude Include="CachingConverterDecorator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CheckedConverter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ConverterBase.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IConverter[T].h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IConverter[TSource, TTarget].h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UncheckedConverter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UncheckedSignExtendingConverter.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Platform.Converters.Benchmarks\Int32ToUInt64ConverterBenchmarks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Platform.Converters.Benchmarks\Program.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Platform.Converters.Benchmarks\UInt64ToUInt64ConverterBenchmarks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit c98e0b9

Please sign in to comment.