forked from linksplatform/Converters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start convert from C# to C++. Issue: linksplatform#48
- Loading branch information
1 parent
297341e
commit c98e0b9
Showing
3 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters