Commit 64af27bd by Jamie Madill

Make Optional a proper helper class.

Move this to a common area, out of perf tests. Change-Id: I53d36accfe0c53789c007edfb20ba7e964947def Reviewed-on: https://chromium-review.googlesource.com/260643Tested-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent f55d4d17
......@@ -50,6 +50,14 @@
'.',
'../include',
],
'direct_dependent_settings':
{
'include_dirs':
[
'<(angle_path)/src',
'<(angle_path)/include',
],
},
'conditions':
[
['angle_build_winrt==1',
......
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Optional.h:
// Represents a type that may be invalid, similar to std::optional.
//
#ifndef COMMON_OPTIONAL_H_
#define COMMON_OPTIONAL_H_
template <class T>
struct Optional
{
Optional()
: mValid(false),
mValue(T())
{}
explicit Optional(const T &valueIn)
: mValid(true),
mValue(valueIn)
{}
Optional(const Optional &other)
: mValid(other.mValid),
mValue(other.mValue)
{}
Optional &operator=(const Optional &other)
{
this->mValid = other.mValid;
this->mValue = other.mValue;
return *this;
}
static Optional None()
{
return Optional();
}
bool valid() const { return mValid; }
const T &value() const { return mValue; }
bool operator==(const Optional &other) const
{
return ((mValid == other.mValid) && (!mValid || (mValue == other.mValue)));
}
bool operator!=(const Optional &other) const
{
return !(*this == other);
}
private:
bool mValid;
T mValue;
};
#endif // COMMON_OPTIONAL_H_
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Unit tests for ANGLE's Optional helper class.
//
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "common/Optional.h"
namespace
{
TEST(OptionalTest, BasicInvalid)
{
Optional<int> testInvalid;
ASSERT_FALSE(testInvalid.valid());
ASSERT_EQ(Optional<int>::None(), testInvalid);
}
TEST(OptionalTest, BasicValid)
{
Optional<int> testValid(3);
ASSERT_TRUE(testValid.valid());
ASSERT_EQ(3, testValid.value());
ASSERT_NE(Optional<int>::None(), testValid);
}
TEST(OptionalTest, Copies)
{
Optional<int> testValid(3);
Optional<int> testInvalid;
Optional<int> testCopy = testInvalid;
ASSERT_FALSE(testCopy.valid());
ASSERT_EQ(testInvalid, testCopy);
testCopy = testValid;
ASSERT_TRUE(testCopy.valid());
ASSERT_EQ(3, testCopy.value());
ASSERT_EQ(testValid, testCopy);
}
} // namespace
......@@ -10,6 +10,7 @@
[
'common/MemoryBuffer.cpp',
'common/MemoryBuffer.h',
'common/Optional.h',
'common/angleutils.cpp',
'common/angleutils.h',
'common/debug.cpp',
......@@ -491,8 +492,8 @@
{
'include_dirs':
[
'.',
'../include',
'<(angle_path)/src',
'<(angle_path)/include',
],
'defines':
[
......
......@@ -15,6 +15,7 @@
# This file list will be shared with the GN build.
'angle_unittests_sources':
[
'<(angle_path)/src/common/Optional_unittest.cpp',
'<(angle_path)/src/libANGLE/Config_unittest.cpp',
'<(angle_path)/src/libANGLE/Fence_unittest.cpp',
'<(angle_path)/src/libANGLE/ImageIndexIterator_unittest.cpp',
......
......@@ -4,51 +4,18 @@
// found in the LICENSE file.
//
#include "SimpleBenchmark.h"
#include "BufferSubData.h"
#include "TexSubImage.h"
#include "PointSprites.h"
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/filestream.h>
namespace
{
#include "BufferSubData.h"
#include "PointSprites.h"
#include "SimpleBenchmark.h"
#include "TexSubImage.h"
#include "common/Optional.h"
template <class T>
struct Optional
namespace
{
Optional()
: valid(false),
value(T())
{}
explicit Optional(T valueIn)
: valid(true),
value(valueIn)
{}
Optional(const Optional &other)
: valid(other.valid),
value(other.value)
{}
Optional &operator=(const Optional &other)
{
this.valid = other.valid;
this.value = otehr.value;
return *this;
}
static Optional None()
{
return Optional();
}
bool valid;
T value;
};
Optional<std::string> GetStringMember(const rapidjson::Document &document, const char *name)
{
......@@ -170,50 +137,50 @@ bool ParseBenchmarkParams(const rapidjson::Document &document, BufferSubDataPara
auto iterations = GetUintMember(document, "iterations");
auto updateRate = GetUintMember(document, "update_rate");
if (!type.valid || !components.valid || !normalized.valid || !updateSize.valid ||
!bufferSize.valid || !iterations.valid || !updateRate.valid)
if (!type.valid() || !components.valid() || !normalized.valid() || !updateSize.valid() ||
!bufferSize.valid() || !iterations.valid() || !updateRate.valid())
{
return false;
}
GLenum vertexType = ParseAttribType(type.value);
GLenum vertexType = ParseAttribType(type.value());
if (vertexType == GL_NONE)
{
std::cerr << "Invalid attribute type: " << type.value << std::endl;
std::cerr << "Invalid attribute type: " << type.value() << std::endl;
return false;
}
if (components.value < 1 || components.value > 4)
if (components.value() < 1 || components.value() > 4)
{
std::cerr << "Invalid component count: " << components.value << std::endl;
std::cerr << "Invalid component count: " << components.value() << std::endl;
return false;
}
if (normalized.value && vertexType == GL_FLOAT)
if (normalized.value() && vertexType == GL_FLOAT)
{
std::cerr << "Normalized float is not a valid vertex type." << std::endl;
return false;
}
if (bufferSize.value == 0)
if (bufferSize.value() == 0)
{
std::cerr << "Zero buffer size is not valid." << std::endl;
return false;
}
if (iterations.value == 0)
if (iterations.value() == 0)
{
std::cerr << "Zero iterations not valid." << std::endl;
return false;
}
params->vertexType = vertexType;
params->vertexComponentCount = components.value;
params->vertexNormalized = normalized.value;
params->updateSize = updateSize.value;
params->bufferSize = bufferSize.value;
params->iterations = iterations.value;
params->updateRate = updateRate.value;
params->vertexComponentCount = components.value();
params->vertexNormalized = normalized.value();
params->updateSize = updateSize.value();
params->bufferSize = bufferSize.value();
params->iterations = iterations.value();
params->updateRate = updateRate.value();
return true;
}
......@@ -298,26 +265,26 @@ int main(int argc, char **argv)
auto testName = GetStringMember(document, "test");
if (!testName.valid)
if (!testName.valid())
{
return 1;
}
if (testName.value == "BufferSubData")
if (testName.value() == "BufferSubData")
{
return ParseAndRunBenchmark<BufferSubDataBenchmark>(rendererType, document);
}
else if (testName.value == "TexSubImage")
else if (testName.value() == "TexSubImage")
{
return ParseAndRunBenchmark<TexSubImageBenchmark>(rendererType, document);
}
else if (testName.value == "PointSprites")
else if (testName.value() == "PointSprites")
{
return ParseAndRunBenchmark<PointSpritesBenchmark>(rendererType, document);
}
else
{
std::cerr << "Unknown test: " << testName.value << std::endl;
std::cerr << "Unknown test: " << testName.value() << std::endl;
return 1;
}
}
......@@ -209,6 +209,7 @@
'includes': [ '../../build/common_defines.gypi', ],
'dependencies':
[
'<(angle_path)/src/angle.gyp:angle_common',
'<(angle_path)/src/angle.gyp:libGLESv2',
'<(angle_path)/src/angle.gyp:libEGL',
'<(angle_path)/util/util.gyp:angle_util',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment