blob: c45b33c82e86dfb5099cec0368b681fb9bd1ba8b [file] [log] [blame]
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// 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 VK_DEBUG_VALUE_HPP_
#define VK_DEBUG_VALUE_HPP_
#include <memory>
#include <string>
namespace vk
{
namespace dbg
{
class Type;
// FormatFlags holds settings used to serialize a Value to a string.
struct FormatFlags
{
// The default FormatFlags used to serialize a Value to a string.
static const FormatFlags Default;
std::string listPrefix; // Prefix to lists.
std::string listSuffix; // Suffix to lists.
std::string listDelimiter; // List item delimiter.
std::string listIndent; // List item indententation prefix.
const FormatFlags* subListFmt; // Format used for list sub items.
};
// Value holds a value that can be read and possible written to.
class Value
{
public:
virtual ~Value() = default;
// type() returns the value's type.
virtual std::shared_ptr<Type> type() const = 0;
// string() returns a string representation of the value using the specified
// FormatFlags.
virtual std::string string(const FormatFlags& = FormatFlags::Default) const;
// get() returns a pointer to the value.
virtual const void* get() const = 0;
// set() changes the value to a copy of the value at ptr.
// set() returns true if the value was changed, or false if the value cannot
// be set.
virtual bool set(void* ptr) { return false; }
};
// Constant is an immutable value.
template <typename T>
class Constant : public Value
{
public:
inline Constant(const T& value);
inline std::shared_ptr<Type> type() const override;
inline const void* get() const override;
private:
const T value;
};
template <typename T>
Constant<T>::Constant(const T& value) :
value(value)
{
}
template <typename T>
std::shared_ptr<Type> Constant<T>::type() const
{
return TypeOf<T>::get();
}
template <typename T>
const void* Constant<T>::get() const
{
return &value;
}
// make_constant() returns a shared_ptr to a Constant with the given value.
template <typename T>
inline std::shared_ptr<Constant<T>> make_constant(const T& value)
{
return std::shared_ptr<Constant<T>>(new vk::dbg::Constant<T>(value));
}
} // namespace dbg
} // namespace vk
#endif // VK_DEBUG_VALUE_HPP_