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 19, 2021
1 parent 297341e commit 48a377f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
33 changes: 25 additions & 8 deletions cpp/Platform.Converters/CachingConverterDecorator.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
namespace Platform::Converters
#pragma once

#include <functional>
#include <memory>
#include <unordered_map>

namespace Platform::Converters
{
template <typename ...> class CachingConverterDecorator;
template <typename TSource, typename TTarget> class CachingConverterDecorator<TSource, TTarget> : public IConverter<TSource, TTarget>
template <typename ...> class Cached;
template <typename TSource, typename TTarget> class Cached<TSource, TTarget>
{
private: readonly IConverter<TSource, TTarget> *_baseConverter;
private: readonly IDictionary<TSource, TTarget> *_cache;
private: std::unique_ptr<std::function<TTarget(TSource)>> _baseConverter;
private: std::unordered_map<TSource, TTarget> _cache;

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

public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter) : this(baseConverter, Dictionary<TSource, TTarget>()) { }
public: Cached(std::function<TTarget(TSource)>& baseConverter): _baseConverter(baseConverter), _cache() {}

public: TTarget Convert(TSource source) { return _cache.GetOrAdd(source, _baseConverter.Convert); }
public: TTarget operator()(const TSource& source)
{
auto cursor = _cache.find(source);
if (cursor != _cache.end())
{
return *cursor;
}
else
{
return *_cache.insert({source, _baseConverter(source)})
}
}
};
}
9 changes: 1 addition & 8 deletions cpp/Platform.Converters/IConverter[T].h
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
namespace Platform::Converters
{
template <typename ...> class IConverter;
template <typename T> class IConverter<T> : public IConverter<T, T>
{
public:
};
}
// Translation is not required
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,22 @@
</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="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,37 @@
<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[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 48a377f

Please sign in to comment.