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
Next step

Final
  • Loading branch information
VasiliyevAD committed Nov 14, 2021
1 parent 297341e commit 3301901
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
35 changes: 29 additions & 6 deletions cpp/Platform.Converters/CachingConverterDecorator.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
namespace Platform::Converters
#pragma once
#include <map>
#include <memory>

#include "IConverter[TSource, TTarget].h"

namespace Platform::Converters
{
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;
std::unique_ptr<std::map<TSource, TTarget>> _cache;

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

public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter) : this(baseConverter, Dictionary<TSource, TTarget>()) { }
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); }
TTarget Convert(TSource source)
{
auto iter = _cache->find(source);
if (iter != _cache.end())
{
*iter = _baseConverter->Convert(source);
}
return *iter;
}
};
}
4 changes: 1 addition & 3 deletions cpp/Platform.Converters/IConverter[T].h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
{
template <typename ...> class IConverter;
template <typename T> class IConverter<T> : public IConverter<T, T>
{
public:
};
{ };
}
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 3301901

Please sign in to comment.