1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef VK_DEBUG_VALUE_HPP_ 16 #define VK_DEBUG_VALUE_HPP_ 17 18 #include "Type.hpp" 19 20 #include <memory> 21 #include <string> 22 23 namespace vk { 24 namespace dbg { 25 26 // FormatFlags holds settings used to serialize a Value to a string. 27 struct FormatFlags 28 { 29 // The default FormatFlags used to serialize a Value to a string. 30 static const FormatFlags Default; 31 32 std::string listPrefix; // Prefix to lists. 33 std::string listSuffix; // Suffix to lists. 34 std::string listDelimiter; // List item delimiter. 35 std::string listIndent; // List item indententation prefix. 36 const FormatFlags *subListFmt; // Format used for list sub items. 37 }; 38 39 // Value holds a value that can be read and possible written to. 40 class Value 41 { 42 public: 43 virtual ~Value() = default; 44 45 // type() returns the value's type. 46 virtual std::shared_ptr<Type> type() const = 0; 47 48 // string() returns a string representation of the value using the specified 49 // FormatFlags. 50 virtual std::string string(const FormatFlags & = FormatFlags::Default) const; 51 52 // get() returns a pointer to the value. 53 virtual const void *get() const = 0; 54 55 // set() changes the value to a copy of the value at ptr. 56 // set() returns true if the value was changed, or false if the value cannot 57 // be set. set(void * ptr)58 virtual bool set(void *ptr) { return false; } 59 }; 60 61 // Constant is an immutable value. 62 template<typename T> 63 class Constant : public Value 64 { 65 public: 66 inline Constant(const T &value); 67 inline std::shared_ptr<Type> type() const override; 68 inline const void *get() const override; 69 70 private: 71 const T value; 72 }; 73 74 template<typename T> Constant(const T & value)75Constant<T>::Constant(const T &value) 76 : value(value) 77 { 78 } 79 80 template<typename T> type() const81std::shared_ptr<Type> Constant<T>::type() const 82 { 83 return TypeOf<T>::get(); 84 } 85 86 template<typename T> get() const87const void *Constant<T>::get() const 88 { 89 return &value; 90 } 91 92 // Reference is reference to a value in memory. 93 template<typename T> 94 class Reference : public Value 95 { 96 public: 97 inline Reference(T &ptr); 98 inline std::shared_ptr<Type> type() const override; 99 inline const void *get() const override; 100 inline bool set(void *ptr) override; 101 102 private: 103 T &ref; 104 }; 105 106 template<typename T> Reference(T & ref)107Reference<T>::Reference(T &ref) 108 : ref(ref) 109 { 110 } 111 112 template<typename T> type() const113std::shared_ptr<Type> Reference<T>::type() const 114 { 115 return TypeOf<T>::get(); 116 } 117 118 template<typename T> get() const119const void *Reference<T>::get() const 120 { 121 return &ref; 122 } 123 124 template<typename T> set(void * ptr)125bool Reference<T>::set(void *ptr) 126 { 127 ref = *reinterpret_cast<const T *>(ptr); 128 return true; 129 } 130 131 // make_constant() returns a shared_ptr to a Constant with the given value. 132 template<typename T> make_constant(const T & value)133inline std::shared_ptr<Constant<T>> make_constant(const T &value) 134 { 135 return std::make_shared<Constant<T>>(value); 136 } 137 138 // make_reference() returns a shared_ptr to a Reference with the given value. 139 template<typename T> make_reference(T & value)140inline std::shared_ptr<Reference<T>> make_reference(T &value) 141 { 142 return std::make_shared<Reference<T>>(value); 143 } 144 145 } // namespace dbg 146 } // namespace vk 147 148 #endif // VK_DEBUG_VALUE_HPP_