Skip to content

Commit

Permalink
Refactor Metadata Get functions, add templated Metadata::Get<TYPE>
Browse files Browse the repository at this point in the history
  • Loading branch information
Auburn committed Aug 21, 2021
1 parent 916d643 commit dd3e781
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
4 changes: 2 additions & 2 deletions NoiseTool/FastNoiseNodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ FastNoiseNodeEditor::FastNoiseNodeEditor() :

auto menuSort = []( const MetadataMenu* a, const MetadataMenu* b ) { return std::strcmp( a->GetName(), b->GetName() ) < 0; };

for( const FastNoise::Metadata* metadata : FastNoise::Metadata::GetMetadataClasses() )
for( const FastNoise::Metadata* metadata : FastNoise::Metadata::GetAll() )
{
auto* metaDataGroup = root;

Expand Down Expand Up @@ -318,7 +318,7 @@ FastNoiseNodeEditor::FastNoiseNodeEditor() :
std::sort( metaDataGroup->items.begin(), metaDataGroup->items.end(), menuSort );
}

mOverheadNode.data = std::make_unique<FastNoise::NodeData>( &FastNoise::New<FastNoise::Constant>()->GetMetadata() );
mOverheadNode.data = std::make_unique<FastNoise::NodeData>( &FastNoise::Metadata::Get<FastNoise::Constant>() );
}

void FastNoiseNodeEditor::DoNodeBenchmarks()
Expand Down
33 changes: 23 additions & 10 deletions include/FastNoise/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,42 @@ namespace FastNoise
struct PerDimensionVariable;
struct NodeData;

namespace Impl
{
template<typename T>
const struct FastNoise::Metadata& GetMetadata();
}

// Stores definition of a FastNoise node class
// Node name, member name+types, functions to set members
struct FASTNOISE_API Metadata
{
virtual ~Metadata() = default;

/// <returns>Array containing metadata for every FastNoise node type</returns>
static const std::vector<const Metadata*>& GetMetadataClasses()
static const std::vector<const Metadata*>& GetAll()
{
return sMetadataClasses;
return sAllMetadata;
}

/// <returns>Metadata for given Metadata::id</returns>
static const Metadata* GetMetadataClass( std::uint16_t nodeId )
static const Metadata* GetFromId( std::uint16_t nodeId )
{
if( nodeId < sMetadataClasses.size() )
if( nodeId < sAllMetadata.size() )
{
return sMetadataClasses[nodeId];
return sAllMetadata[nodeId];
}

return nullptr;
}

/// <returns>Metadata for given node class</returns>
template<typename T>
static const Metadata& Get()
{
return Impl::GetMetadata<T>();
}

/// <summary>
/// Serialise node data and any source node datas (recursive)
/// </summary>
Expand Down Expand Up @@ -171,18 +184,18 @@ namespace FastNoise
protected:
Metadata()
{
id = AddMetadataClass( this );
id = AddMetadata( this );
}

private:
static std::uint16_t AddMetadataClass( const Metadata* newMetadata )
static std::uint16_t AddMetadata( const Metadata* newMetadata )
{
sMetadataClasses.emplace_back( newMetadata );
sAllMetadata.emplace_back( newMetadata );

return (std::uint16_t)sMetadataClasses.size() - 1;
return (std::uint16_t)sAllMetadata.size() - 1;
}

static std::vector<const Metadata*> sMetadataClasses;
static std::vector<const Metadata*> sAllMetadata;
};

// Stores data to create an instance of a FastNoise node
Expand Down
30 changes: 15 additions & 15 deletions src/FastNoise/FastNoise_C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ void fnGenTileable2D( const void* node, float* noiseOut, int xSize, int ySize, f

int fnGetMetadataCount()
{
return (int)FastNoise::Metadata::GetMetadataClasses().size();
return (int)FastNoise::Metadata::GetAll().size();
}

const char* fnGetMetadataName( int id )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
return metadata->name;
}
Expand All @@ -111,7 +111,7 @@ const char* fnGetMetadataName( int id )

void* fnNewFromMetadata( int id, unsigned simdLevel )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
return new FastNoise::SmartNode<>( metadata->CreateNode( (FastSIMD::eLevel)simdLevel ) );
}
Expand All @@ -120,7 +120,7 @@ void* fnNewFromMetadata( int id, unsigned simdLevel )

int fnGetMetadataVariableCount( int id )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
return (int)metadata->memberVariables.size();
}
Expand All @@ -129,7 +129,7 @@ int fnGetMetadataVariableCount( int id )

const char* fnGetMetadataVariableName( int id, int variableIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)variableIndex < metadata->memberVariables.size() )
{
Expand All @@ -142,7 +142,7 @@ const char* fnGetMetadataVariableName( int id, int variableIndex )

int fnGetMetadataVariableType( int id, int variableIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)variableIndex < metadata->memberVariables.size() )
{
Expand All @@ -155,7 +155,7 @@ int fnGetMetadataVariableType( int id, int variableIndex )

int fnGetMetadataVariableDimensionIdx( int id, int variableIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)variableIndex < metadata->memberVariables.size() )
{
Expand All @@ -168,7 +168,7 @@ int fnGetMetadataVariableDimensionIdx( int id, int variableIndex )

int fnGetMetadataEnumCount( int id, int variableIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)variableIndex < metadata->memberVariables.size() )
{
Expand All @@ -181,7 +181,7 @@ int fnGetMetadataEnumCount( int id, int variableIndex )

const char* fnGetMetadataEnumName( int id, int variableIndex, int enumIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)variableIndex < metadata->memberVariables.size() )
{
Expand Down Expand Up @@ -218,7 +218,7 @@ bool fnSetVariableIntEnum( void* node, int variableIndex, int value )

int fnGetMetadataNodeLookupCount( int id )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
return (int)metadata->memberNodeLookups.size();
}
Expand All @@ -227,7 +227,7 @@ int fnGetMetadataNodeLookupCount( int id )

const char* fnGetMetadataNodeLookupName( int id, int nodeLookupIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)nodeLookupIndex < metadata->memberNodeLookups.size() )
{
Expand All @@ -240,7 +240,7 @@ const char* fnGetMetadataNodeLookupName( int id, int nodeLookupIndex )

int fnGetMetadataNodeLookupDimensionIdx( int id, int nodeLookupIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)nodeLookupIndex < metadata->memberNodeLookups.size() )
{
Expand All @@ -263,7 +263,7 @@ bool fnSetNodeLookup( void* node, int nodeLookupIndex, const void* nodeLookup )

int fnGetMetadataHybridCount( int id )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
return (int)metadata->memberHybrids.size();
}
Expand All @@ -272,7 +272,7 @@ int fnGetMetadataHybridCount( int id )

const char* fnGetMetadataHybridName( int id, int hybridIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)hybridIndex < metadata->memberHybrids.size() )
{
Expand All @@ -285,7 +285,7 @@ const char* fnGetMetadataHybridName( int id, int hybridIndex )

int fnGetMetadataHybridDimensionIdx( int id, int hybridIndex )
{
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetMetadataClass( (uint16_t)id ) )
if( const FastNoise::Metadata* metadata = FastNoise::Metadata::GetFromId( (uint16_t)id ) )
{
if( (size_t)hybridIndex < metadata->memberHybrids.size() )
{
Expand Down
16 changes: 10 additions & 6 deletions src/FastNoise/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using namespace FastNoise;

std::vector<const Metadata*> Metadata::sMetadataClasses;
std::vector<const Metadata*> Metadata::sAllMetadata;

NodeData::NodeData( const Metadata* data )
{
Expand Down Expand Up @@ -222,7 +222,7 @@ SmartNode<> DeserialiseSmartNodeInternal( const std::vector<uint8_t>& serialised
}

// Create node from nodeId
const Metadata* metadata = Metadata::GetMetadataClass( nodeId );
const Metadata* metadata = Metadata::GetFromId( nodeId );

if( !metadata )
{
Expand Down Expand Up @@ -331,7 +331,7 @@ NodeData* DeserialiseNodeDataInternal( const std::vector<uint8_t>& serialisedNod
}

// Create node from nodeId
const Metadata* metadata = Metadata::GetMetadataClass( nodeId );
const Metadata* metadata = Metadata::GetFromId( nodeId );

if( !metadata )
{
Expand Down Expand Up @@ -447,9 +447,9 @@ namespace FastNoise
template<typename T>
std::unique_ptr<const MetadataT<T>> CreateMetadataInstance( const char* className )
{
MetadataT<T>* newMetadata( new MetadataT<T> );
auto* newMetadata = new MetadataT<T>;
newMetadata->name = className;
return std::unique_ptr<const MetadataT<T>>{ newMetadata };
return std::unique_ptr<const MetadataT<T>>( newMetadata );
}

#if FASTNOISE_USE_SHARED_PTR
Expand All @@ -460,10 +460,14 @@ std::unique_ptr<const MetadataT<T>> CreateMetadataInstance( const char* classNam

#define FASTSIMD_BUILD_CLASS2( CLASS ) \
const std::unique_ptr<const FastNoise::MetadataT<CLASS>> g ## CLASS ## Metadata = CreateMetadataInstance<CLASS>( #CLASS );\
const FastNoise::Metadata& CLASS::GetMetadata() const\
template<> const FastNoise::Metadata& FastNoise::Impl::GetMetadata<CLASS>()\
{\
return *g ## CLASS ## Metadata;\
}\
const FastNoise::Metadata& CLASS::GetMetadata() const\
{\
return FastNoise::Impl::GetMetadata<CLASS>();\
}\
SmartNode<> FastNoise::MetadataT<CLASS>::CreateNode( FastSIMD::eLevel l ) const\
{\
return SmartNode<>( FastSIMD::New<CLASS>( l FASTNOISE_GET_MEMORY_ALLOCATOR() ) );\
Expand Down
4 changes: 2 additions & 2 deletions tests/FastNoiseBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FastNoise::SmartNode<> BuildGenerator( benchmark::State& state, const FastNoise:
if( !memberNode.setFunc( generator.get(), source ) )
{
// If constant source is not valid try all other node types in order
for( const FastNoise::Metadata* tryMetadata : FastNoise::Metadata::GetMetadataClasses() )
for( const FastNoise::Metadata* tryMetadata : FastNoise::Metadata::GetAll() )
{
FastNoise::SmartNode<> trySource = tryMetadata->CreateNode( level );

Expand Down Expand Up @@ -152,7 +152,7 @@ int main( int argc, char** argv )
continue;
}

for( const FastNoise::Metadata* metadata : FastNoise::Metadata::GetMetadataClasses() )
for( const FastNoise::Metadata* metadata : FastNoise::Metadata::GetAll() )
{
const char* groupName = "Misc";

Expand Down

0 comments on commit dd3e781

Please sign in to comment.