Start refactoring source property implementations.

This commit is contained in:
Peter Johnson
2016-12-22 21:19:50 -08:00
parent ce69783871
commit d6ef2c04a5
7 changed files with 132 additions and 111 deletions

View File

@@ -19,9 +19,11 @@
#include "llvm/ArrayRef.h"
#include "llvm/StringMap.h"
#include "llvm/StringRef.h"
#include "cscore_cpp.h"
#include "Frame.h"
#include "Image.h"
#include "PropertyImpl.h"
namespace cs {
@@ -135,67 +137,13 @@ class SourceImpl {
std::atomic_int m_numSinksEnabled{0};
protected:
// Property data
class PropertyBase {
public:
PropertyBase() = default;
PropertyBase(llvm::StringRef name_) : name{name_} {}
PropertyBase(llvm::StringRef name_, CS_PropertyKind kind_, int step_,
int defaultValue_, int value_)
: name{name_},
propKind{kind_},
step{step_},
defaultValue{defaultValue_},
value{value_} {}
virtual ~PropertyBase() = default;
PropertyBase(const PropertyBase& oth) = delete;
PropertyBase& operator=(const PropertyBase& oth) = delete;
void SetValue(int v) {
if (hasMinimum && v < minimum)
value = minimum;
else if (hasMaximum && v > maximum)
value = maximum;
else
value = v;
valueSet = true;
}
void SetValue(llvm::StringRef v) {
valueStr = v;
valueSet = true;
}
void SetDefaultValue(int v) {
if (hasMinimum && v < minimum)
defaultValue = minimum;
else if (hasMaximum && v > maximum)
defaultValue = maximum;
else
defaultValue = v;
}
std::string name;
CS_PropertyKind propKind{CS_PROP_NONE};
bool hasMinimum{false};
bool hasMaximum{false};
int minimum{0};
int maximum{100};
int step{1};
int defaultValue{0};
int value{0};
std::string valueStr;
std::vector<std::string> enumChoices;
bool valueSet{false};
};
// Get a property; must be called with m_mutex held.
PropertyBase* GetProperty(int property) {
PropertyImpl* GetProperty(int property) {
if (property <= 0 || static_cast<size_t>(property) > m_propertyData.size())
return nullptr;
return m_propertyData[property - 1].get();
}
const PropertyBase* GetProperty(int property) const {
const PropertyImpl* GetProperty(int property) const {
if (property <= 0 || static_cast<size_t>(property) > m_propertyData.size())
return nullptr;
return m_propertyData[property - 1].get();
@@ -204,15 +152,21 @@ class SourceImpl {
// Create an "empty" property. This is called by GetPropertyIndex to create
// properties that don't exist (as GetPropertyIndex can't fail).
// Note: called with m_mutex held.
virtual std::unique_ptr<PropertyBase> CreateEmptyProperty(
virtual std::unique_ptr<PropertyImpl> CreateEmptyProperty(
llvm::StringRef name) const = 0;
// Cache properties. Implementations must return false and set status to
// CS_SOURCE_IS_DISCONNECTED if not possible to cache.
virtual bool CacheProperties(CS_Status* status) const = 0;
void NotifyPropertyCreated(int propIndex, PropertyImpl& prop);
// Update property value; must be called with m_mutex held.
void UpdatePropertyValue(int property, bool setString, int value,
llvm::StringRef valueStr);
// Cached properties and video modes (protected with m_mutex)
mutable std::vector<std::unique_ptr<PropertyBase>> m_propertyData;
mutable std::vector<std::unique_ptr<PropertyImpl>> m_propertyData;
mutable llvm::StringMap<int> m_properties;
mutable std::vector<VideoMode> m_videoModes;
// Current video mode