diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2017-07-11 12:41:05 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-11 12:41:05 -0700 |
| commit | a60dd57e5ac0d3cc43fddf62dbf72677d377121f (patch) | |
| tree | 2e4911b6da6573d334117a08130be5c945c31b15 /source/slang/syntax.h | |
| parent | 98b3e5bc95b6de081885798840c2deb79905a68f (diff) | |
| parent | bd7105ff8683a680d1270eca8cd74f9002144dbd (diff) | |
Merge pull request #72 from tfoleyNV/resources-in-structs
Resources in structs
Diffstat (limited to 'source/slang/syntax.h')
| -rw-r--r-- | source/slang/syntax.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/source/slang/syntax.h b/source/slang/syntax.h index de5467c71..83b2f5801 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -231,6 +231,65 @@ namespace Slang RefPtr<ExpressionType> operator->() { return type; } }; + // A reference to a class of syntax node, that can be + // used to create instances on the fly + struct SyntaxClassBase + { + typedef void* (*CreateFunc)(); + + SyntaxClassBase() + {} + + SyntaxClassBase(CreateFunc createFunc) + : createFunc(createFunc) + {} + + void* createInstanceImpl() + { + return createFunc ? createFunc() : nullptr; + } + + CreateFunc createFunc = nullptr; + + template<typename T> + struct Impl + { + static void* createFunc(); + }; + }; + + template<typename T> + struct SyntaxClass : SyntaxClassBase + { + SyntaxClass() + {} + + template <typename U> + SyntaxClass(SyntaxClass<U> const& other, + typename EnableIf<IsConvertible<T*, U*>::Value, void>::type* = 0) + : SyntaxClassBase(other.createFunc) + { + } + + T* createInstance() + { + return (T*)createInstanceImpl(); + } + + static SyntaxClass<T> getClass() + { + SyntaxClass<T> result; + result.createFunc = &SyntaxClass::Impl<T>::createFunc; + return result; + } + }; + + template<typename T> + SyntaxClass<T> getClass() + { + return SyntaxClass<T>::getClass(); + } + // A reference to a declaration, which may include // substitutions for generic parameters. |
