• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TypeOf.hpp"
19 
20 #include "System/Debug.hpp"
21 
22 #include <memory>
23 #include <string>
24 
25 namespace vk {
26 namespace dbg {
27 
28 class Variables;
29 class VariableContainer;
30 
31 // FormatFlags holds settings used to serialize a Value to a string.
32 struct FormatFlags
33 {
34 	// The default FormatFlags used to serialize a Value to a string.
35 	static const FormatFlags Default;
36 
37 	std::string listPrefix;         // Prefix to lists.
38 	std::string listSuffix;         // Suffix to lists.
39 	std::string listDelimiter;      // List item delimiter.
40 	std::string listIndent;         // List item indententation prefix.
41 	const FormatFlags *subListFmt;  // Format used for list sub items.
42 };
43 
44 // Value holds a value that can be read.
45 class Value
46 {
47 public:
48 	virtual ~Value() = default;
49 
50 	// type() returns the typename for the value.
51 	virtual std::string type() = 0;
52 
53 	// get() returns a string representation of the value using the specified
54 	// FormatFlags.
55 	virtual std::string get(const FormatFlags & = FormatFlags::Default) = 0;
56 
57 	// children() returns the optional child members of this value.
children()58 	virtual std::shared_ptr<Variables> children() { return nullptr; }
59 };
60 
61 // Constant is constant value of type T.
62 template<typename T>
63 class Constant : public Value
64 {
65 public:
Constant(const T & val)66 	Constant(const T &val)
67 	    : val(val)
68 	{}
type()69 	std::string type() override { return TypeOf<T>::name; }
get(const FormatFlags & fmt=FormatFlags::Default)70 	std::string get(const FormatFlags &fmt = FormatFlags::Default) override { return std::to_string(val); }
71 
72 private:
73 	T const val;
74 };
75 
76 // Reference is reference to a value in memory.
77 template<typename T>
78 class Reference : public Value
79 {
80 public:
Reference(const T & ref)81 	Reference(const T &ref)
82 	    : ref(ref)
83 	{}
type()84 	std::string type() override { return TypeOf<T>::name; }
get(const FormatFlags & fmt=FormatFlags::Default)85 	std::string get(const FormatFlags &fmt = FormatFlags::Default) override { return std::to_string(ref); }
86 
87 private:
88 	T const &ref;
89 };
90 
91 // Struct is an implementation of Value that delegates calls to children() on to
92 // the constructor provided Variables.
93 class Struct : public Value
94 {
95 public:
Struct(const std::string & type,const std::shared_ptr<Variables> & members)96 	Struct(const std::string &type, const std::shared_ptr<Variables> &members)
97 	    : ty(type)
98 	    , members(members)
99 	{
100 		ASSERT(members);
101 	}
102 
type()103 	std::string type() override { return ty; }
104 	std::string get(const FormatFlags &fmt = FormatFlags::Default) override;
children()105 	std::shared_ptr<Variables> children() override { return members; }
106 
107 	// create() constructs and returns a new Struct with the given type name and
108 	// calls fields to populate the child members.
109 	// fields must be a function that has the signature:
110 	//   void(std::shared_pointer<VariableContainer>&)
111 	template<typename F>
create(const std::string & name,F && fields)112 	static std::shared_ptr<Struct> create(const std::string &name, F &&fields)
113 	{
114 		auto vc = std::make_shared<VariableContainer>();
115 		fields(vc);
116 		return std::make_shared<Struct>(name, vc);
117 	}
118 
119 private:
120 	std::string const ty;
121 	std::shared_ptr<Variables> const members;
122 };
123 
124 // make_constant() returns a shared_ptr to a Constant with the given value.
125 template<typename T>
make_constant(const T & value)126 inline std::shared_ptr<Constant<T>> make_constant(const T &value)
127 {
128 	return std::make_shared<Constant<T>>(value);
129 }
130 
131 // make_reference() returns a shared_ptr to a Reference with the given value.
132 template<typename T>
make_reference(const T & value)133 inline std::shared_ptr<Reference<T>> make_reference(const T &value)
134 {
135 	return std::make_shared<Reference<T>>(value);
136 }
137 
138 }  // namespace dbg
139 }  // namespace vk
140 
141 #endif  // VK_DEBUG_VALUE_HPP_
142