Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F4003799
transformations.h
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 9, 8:30 AM
Size
3 KB
Mime Type
text/x-c++
Expires
Sat, Jul 11, 8:30 AM (8 h, 52 m)
Engine
blob
Format
Raw Data
Handle
273704
Attached To
rXR Xreate
transformations.h
View Options
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* File: transformations.h
* Author: pgess <v.melnychenko@xreate.org>
*
* Created on March 25, 2017, 9:04 PM
*/
#ifndef TRANSFORMATIONS_H
#define TRANSFORMATIONS_H
#include "pass/compilepass.h"
namespace xreate { namespace compilation {
template <class TransformerType>
struct TransformerInfo {
//static const unsigned int id = 1; (current vacant id)
};
class Transformer{
public:
virtual llvm::Value* transform(const Expression& expression, llvm::Value* raw, const Context& ctx)=0;
virtual ~Transformer(){};
};
class TransformationsManager {
public:
std::list<Transformer*> getRelevantTransformers(const Expression& expression);
template<class TransformerType>
void registerTransformer(const std::string& annotation, TransformerType* t){
const int id = TransformerInfo<TransformerType>::id;
assert(!__transformers.count(id));
__transformers[id] = t;
__subscriptions.emplace(annotation, id);
}
template<class TransformerType>
void unregisterTransformer(const std::string& annotation, TransformerType* t){
const unsigned int id = TransformerInfo<TransformerType>::id;
auto range = __subscriptions.equal_range(annotation);
const auto entry = make_pair(annotation, id);
__subscriptions.erase(std::find_if(range.first, range.second, [id](const auto& el){return el.second == id;}));
__transformers.erase(id);
}
template<class TransformerType>
TransformerType* update(TransformerType* newInstance){
const int id = TransformerInfo<TransformerType>::id;
Transformer* oldInstance = __transformers[id];
__transformers[id] = newInstance;
return static_cast<TransformerType*>(oldInstance);
}
template<class TransformerType>
bool exists(){
const int id = TransformerInfo<TransformerType>::id;
return __transformers.count(id);
}
template <class TransformerType>
TransformerType* get(){
const int id = TransformerInfo<TransformerType>::id;
return static_cast<TransformerType*>(__transformers.at(id));
}
private:
std::map<unsigned int, Transformer*> __transformers;
std::multimap<std::string, unsigned int> __subscriptions;
};
/**\brief Provides custom transformations during Code Scope compilation
* \extends xreate::compilation::IBruteScope
*/
template <class Parent>
class TransformationsScopeDecorator: public Transformer, public Parent {
// SCOPE DECORATOR PART
public:
TransformationsScopeDecorator(const CodeScope* const codeScope, IBruteFunction* f, CompilePass* compilePass)
: Parent(codeScope, f, compilePass){}
virtual llvm::Value*
process(const Expression& expr, const std::string& hintVarDecl=""){
llvm::Value* result = Parent::process(expr, hintVarDecl);
return transform(expr, result, Context{this, Parent::function, Parent::pass});
}
// TRANSFORMER PART
public:
virtual llvm::Value*
transform(const Expression& expression, llvm::Value* raw, const Context& ctx) {
llvm::Value* result = raw;
TransformationsManager* man = Parent::pass->managerTransformations;
if (expression.tags.size())
for (Transformer* handler: man->getRelevantTransformers(expression)){
result = handler->transform(expression, result, ctx);
}
return result;
}
};
} }
#endif /* TRANSFORMATIONS_H */
Event Timeline
Log In to Comment