dawn-cmake/src/type_manager.h
Ben Clayton 165ff1c978 ast::Builder: Make the interface more fluent
Add C++ aliases for the wgsl types `i32`, `u32` and `f32`.

Separate types out from the builder and into a `Builder::Types` class. An instance of this is now held by the `Builder::ty` field. Makes it clear when you are referencing a `ast::type` instead of a constructor method.

Rework a number of builder methods so they take the type as a template argument instead of a parameter. This more closely resembles wgsl (example: `vec2<i32>(1,2)`)

Use PascalCase for the constructor methods, but keep the wgsl-like constructors lowercase to imitate the language style.

Add `BuilderWithContext` so that `Builder` can be truely immutable, and so we can remove `set_context()`.

Change-Id: Idf2d7d5abe7d11e27671b8e80d3d56d6bc4b3ca2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32980
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
2020-11-17 15:23:48 +00:00

64 lines
1.8 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_TYPE_MANAGER_H_
#define SRC_TYPE_MANAGER_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "src/ast/type/type.h"
namespace tint {
/// The type manager holds all the pointers to the known types.
class TypeManager {
public:
TypeManager();
~TypeManager();
/// Clears all registered types.
void Reset();
/// Get the given type from the type manager
/// @param type The type to register
/// @return the pointer to the registered type
ast::type::Type* Get(std::unique_ptr<ast::type::Type> type);
/// Get the given type `T` from the type manager
/// @param args the arguments to pass to the type constructor
/// @return the pointer to the registered type
template <typename T, typename... ARGS>
T* Get(ARGS&&... args) {
auto ty = Get(std::make_unique<T>(std::forward<ARGS>(args)...));
return static_cast<T*>(ty);
}
/// Returns the type map
/// @returns the mapping from name string to type.
const std::unordered_map<std::string, std::unique_ptr<ast::type::Type>>&
types() {
return types_;
}
private:
std::unordered_map<std::string, std::unique_ptr<ast::type::Type>> types_;
};
} // namespace tint
#endif // SRC_TYPE_MANAGER_H_