Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary string copies #4201

Merged
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/ActionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Json::Value ActionSet::SerializeToJsonValue() const
{
Json::Value root = BaseCardElement::SerializeToJsonValue();

std::string actionsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Actions);
const std::string& actionsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Actions);
root[actionsPropertyName] = Json::Value(Json::arrayValue);

for (const auto& actionElement : m_actions)
Expand Down
24 changes: 19 additions & 5 deletions source/shared/cpp/ObjectModel/BaseActionElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,52 @@ using namespace AdaptiveSharedNamespace;

constexpr const char* const BaseActionElement::defaultStyle;

BaseActionElement::BaseActionElement(ActionType type) :
m_style(BaseActionElement::defaultStyle), m_type(type)
BaseActionElement::BaseActionElement(ActionType type) : m_style(BaseActionElement::defaultStyle), m_type(type)
{
SetTypeString(ActionTypeToString(type));
PopulateKnownPropertiesSet();
}

std::string BaseActionElement::GetTitle() const
const std::string& BaseActionElement::GetTitle() const
{
return m_title;
}

void BaseActionElement::SetTitle(std::string&& value)
{
m_title = std::move(value);
}

void BaseActionElement::SetTitle(const std::string& value)
{
m_title = value;
}

std::string BaseActionElement::GetIconUrl() const
const std::string& BaseActionElement::GetIconUrl() const
{
return m_iconUrl;
}

void BaseActionElement::SetIconUrl(std::string&& value)
{
m_iconUrl = std::move(value);
}

void BaseActionElement::SetIconUrl(const std::string& value)
{
m_iconUrl = value;
}

std::string BaseActionElement::GetStyle() const
const std::string& BaseActionElement::GetStyle() const
{
return m_style;
}

void BaseActionElement::SetStyle(std::string&& value)
{
m_style = std::move(value);
}

void BaseActionElement::SetStyle(const std::string& value)
{
m_style = value;
Expand Down
22 changes: 15 additions & 7 deletions source/shared/cpp/ObjectModel/BaseActionElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ namespace AdaptiveSharedNamespace
BaseActionElement& operator=(BaseActionElement&&) = default;
~BaseActionElement() = default;

virtual std::string GetTitle() const;
const std::string& GetTitle() const;

virtual void SetTitle(std::string&& value);
virtual void SetTitle(const std::string& value);

virtual std::string GetIconUrl() const;
virtual void SetIconUrl(const std::string& value);
const std::string& GetIconUrl() const;

void SetIconUrl(std::string&& value);
void SetIconUrl(const std::string& value);

virtual std::string GetStyle() const;
virtual void SetStyle(const std::string& value);
const std::string& GetStyle() const;

virtual ActionType GetElementType() const;
void SetStyle(std::string&& value);
void SetStyle(const std::string& value);

ActionType GetElementType() const;

void GetResourceInformation(std::vector<RemoteResourceInformation>& resourceUris) override;
Json::Value SerializeToJsonValue() const override;
Expand All @@ -45,10 +51,12 @@ namespace AdaptiveSharedNamespace
private:
void PopulateKnownPropertiesSet();

static constexpr const char* const defaultStyle = "default";

std::string m_title;
std::string m_iconUrl;
std::string m_style;
static constexpr const char* const defaultStyle = "default";

ActionType m_type;
};

Expand Down
8 changes: 4 additions & 4 deletions source/shared/cpp/ObjectModel/BaseCardElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool BaseCardElement::GetSeparator() const
return m_separator;
}

void BaseCardElement::SetSeparator(const bool value)
void BaseCardElement::SetSeparator(bool value)
{
m_separator = value;
}
Expand All @@ -49,7 +49,7 @@ Spacing BaseCardElement::GetSpacing() const
return m_spacing;
}

void BaseCardElement::SetSpacing(const Spacing value)
void BaseCardElement::SetSpacing(Spacing value)
{
m_spacing = value;
}
Expand All @@ -59,7 +59,7 @@ HeightType BaseCardElement::GetHeight() const
return m_height;
}

void BaseCardElement::SetHeight(const HeightType value)
void BaseCardElement::SetHeight(HeightType value)
{
m_height = value;
}
Expand All @@ -69,7 +69,7 @@ bool BaseCardElement::GetIsVisible() const
return m_isVisible;
}

void BaseCardElement::SetIsVisible(const bool value)
void BaseCardElement::SetIsVisible(bool value)
{
m_isVisible = value;
}
Expand Down
12 changes: 6 additions & 6 deletions source/shared/cpp/ObjectModel/BaseCardElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ namespace AdaptiveSharedNamespace
Json::Value SerializeToJsonValue() const override;

virtual bool GetSeparator() const;
virtual void SetSeparator(const bool value);
virtual void SetSeparator(bool value);

HeightType GetHeight() const;
void SetHeight(const HeightType value);
void SetHeight(HeightType value);

virtual Spacing GetSpacing() const;
virtual void SetSpacing(const Spacing value);
virtual void SetSpacing(Spacing value);

virtual bool GetIsVisible() const;
virtual void SetIsVisible(const bool value);
bool GetIsVisible() const;
void SetIsVisible(bool value);

virtual CardElementType GetElementType() const;
CardElementType GetElementType() const;

template<typename T> static std::shared_ptr<T> Deserialize(ParseContext& context, const Json::Value& json);

Expand Down
10 changes: 6 additions & 4 deletions source/shared/cpp/ObjectModel/BaseElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ namespace AdaptiveSharedNamespace

std::string BaseElement::Serialize() const { return ParseUtil::JsonToString(SerializeToJsonValue()); }

std::string BaseElement::GetId() const { return m_id; }
const std::string& BaseElement::GetId() const { return m_id; }

void BaseElement::SetId(std::string&& value) { m_id = std::move(value); }
void BaseElement::SetId(const std::string& value) { m_id = value; }

std::string BaseElement::GetElementTypeString() const { return m_typeString; }
const std::string& BaseElement::GetElementTypeString() const { return m_typeString; }

void BaseElement::SetElementTypeString(std::string&& value) { m_typeString = std::move(value); }
void BaseElement::SetElementTypeString(const std::string& value) { m_typeString = value; }

void BaseElement::PopulateKnownPropertiesSet()
Expand All @@ -53,7 +55,7 @@ namespace AdaptiveSharedNamespace

const Json::Value& BaseElement::GetAdditionalProperties() const { return m_additionalProperties; }

void BaseElement::SetAdditionalProperties(Json::Value const& value) { m_additionalProperties = value; }
void BaseElement::SetAdditionalProperties(const Json::Value& value) { m_additionalProperties = value; }

// Given a map of what our host provides, determine if this element's requirements are satisfied.
bool BaseElement::MeetsRequirements(const AdaptiveSharedNamespace::FeatureRegistration& featureRegistration) const
Expand Down Expand Up @@ -159,7 +161,7 @@ namespace AdaptiveSharedNamespace
catch (const AdaptiveCardParseException&)
{
throw AdaptiveCardParseException(ErrorStatusCode::InvalidPropertyValue,
"Invalid version in requires value: '" + memberValue + "'");
"Invalid version in requires value: '" + memberValue + "'");
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions source/shared/cpp/ObjectModel/BaseElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace AdaptiveSharedNamespace
BaseElement() :
m_typeString{}, m_additionalProperties{},
m_requires{std::make_shared<std::unordered_map<std::string, AdaptiveSharedNamespace::SemanticVersion>>()},
m_fallbackContent(nullptr), m_internalId{InternalId::Current()}, m_fallbackType(FallbackType::None),
m_canFallbackToAncestor(false), m_id{}
m_fallbackContent(nullptr), m_id{}, m_internalId{InternalId::Current()}, m_fallbackType(FallbackType::None),
m_canFallbackToAncestor(false)
{
PopulateKnownPropertiesSet();
}
Expand All @@ -60,12 +60,15 @@ namespace AdaptiveSharedNamespace
virtual ~BaseElement() = default;

// Element type and identity
std::string GetElementTypeString() const;
const std::string& GetElementTypeString() const;
void SetElementTypeString(std::string&& value);
void SetElementTypeString(const std::string& value);
virtual std::string GetId() const;

const std::string& GetId() const;
virtual void SetId(std::string&& value);
virtual void SetId(const std::string& value);

const InternalId GetInternalId() const { return m_internalId; }
InternalId GetInternalId() const { return m_internalId; }

template<typename T> void DeserializeBase(ParseContext& context, const Json::Value& json);

Expand All @@ -88,6 +91,7 @@ namespace AdaptiveSharedNamespace
virtual void GetResourceInformation(std::vector<RemoteResourceInformation>& resourceUris);

protected:
void SetTypeString(std::string&& type) { m_typeString = std::move(type); }
void SetTypeString(const std::string& type) { m_typeString = type; }
void SetCanFallbackToAncestor(bool value) { m_canFallbackToAncestor = value; }

Expand All @@ -102,10 +106,10 @@ namespace AdaptiveSharedNamespace

std::shared_ptr<std::unordered_map<std::string, AdaptiveSharedNamespace::SemanticVersion>> m_requires;
std::shared_ptr<BaseElement> m_fallbackContent;
std::string m_id;
InternalId m_internalId;
FallbackType m_fallbackType;
bool m_canFallbackToAncestor;
std::string m_id;
};

template<typename T> void BaseElement::DeserializeBase(ParseContext& context, const Json::Value& json)
Expand Down Expand Up @@ -135,7 +139,7 @@ namespace AdaptiveSharedNamespace
return;
}
throw AdaptiveCardParseException(ErrorStatusCode::InvalidPropertyValue,
"The only valid string value for the fallback property is 'drop'.");
"The only valid string value for the fallback property is 'drop'.");
}
else if (fallbackValue.isObject())
{
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/ChoiceSetInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Json::Value ChoiceSetInput::SerializeToJsonValue() const
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Wrap)] = m_wrap;
}

std::string propertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Choices);
const std::string& propertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Choices);
root[propertyName] = Json::Value(Json::arrayValue);
for (const auto& choice : m_choices)
{
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/Column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Json::Value Column::SerializeToJsonValue() const
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Width)] = m_width;
}

std::string propertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Items);
const std::string& propertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Items);
root[propertyName] = Json::Value(Json::arrayValue);
for (const auto& cardElement : m_items)
{
Expand Down
19 changes: 10 additions & 9 deletions source/shared/cpp/ObjectModel/EnumMagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ namespace AdaptiveSharedNamespace
public:
// Initialize with a single list -> automatically generate reverse mapping
EnumMapping(const std::initializer_list<std::pair<T, std::string>>& t) :
_enumToString{t.begin(), t.end()}, _stringToEnum(t.size())
_enumToString{t.begin(), t.end()},
_stringToEnum(t.size())
{
_GenerateStringToEnumMap();
}
Expand All @@ -53,7 +54,7 @@ namespace AdaptiveSharedNamespace
_GenerateStringToEnumMap();
}

const std::string toString(T t) const { return _enumToString.at(t); }
const std::string& toString(T t) const { return _enumToString.at(t); }
T fromString(const std::string& str) const { return _stringToEnum.at(str); }

private:
Expand All @@ -71,12 +72,12 @@ namespace AdaptiveSharedNamespace
}

// Provides forward declaration for EnumHelpers mapping accessor as well as global mapping functions.
#define DECLARE_ADAPTIVECARD_ENUM(ENUMTYPE) \
namespace EnumHelpers \
{ \
const EnumMapping<ENUMTYPE>& get##ENUMTYPE##Enum(); \
} \
const std::string ENUMTYPE##ToString(const ENUMTYPE t); \
#define DECLARE_ADAPTIVECARD_ENUM(ENUMTYPE) \
namespace EnumHelpers \
{ \
const EnumMapping<ENUMTYPE>& get##ENUMTYPE##Enum(); \
} \
const std::string& ENUMTYPE##ToString(const ENUMTYPE t); \
ENUMTYPE ENUMTYPE##FromString(const std::string& t);

#define _DEFINE_ADAPTIVECARD_ENUM_INVARIANT(ENUMTYPE, ...) \
Expand All @@ -88,7 +89,7 @@ namespace AdaptiveSharedNamespace
return generatedEnum; \
} \
} \
const std::string ENUMTYPE##ToString(const ENUMTYPE t) { return EnumHelpers::get##ENUMTYPE##Enum().toString(t); }
const std::string& ENUMTYPE##ToString(const ENUMTYPE t) { return EnumHelpers::get##ENUMTYPE##Enum().toString(t); }

// Define mapping functions for ENUMTYPE. Throw an exception if caller passes in a string that doesn't map
#define DEFINE_ADAPTIVECARD_ENUM_THROW(ENUMTYPE, ...) \
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/Fact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ DateTimePreparser Fact::GetValueForDateParsing() const
return DateTimePreparser(m_value);
}

std::string Fact::GetLanguage() const
const std::string& Fact::GetLanguage() const
{
return m_language;
}
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/Fact.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace AdaptiveSharedNamespace
DateTimePreparser GetValueForDateParsing() const;

void SetLanguage(const std::string& value);
std::string GetLanguage() const;
const std::string& GetLanguage() const;

static std::shared_ptr<Fact> Deserialize(const ParseContext& context, const Json::Value& root);
static std::shared_ptr<Fact> DeserializeFromString(const ParseContext& context, const std::string& jsonString);
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/FactSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Json::Value FactSet::SerializeToJsonValue() const
{
Json::Value root = BaseCardElement::SerializeToJsonValue();

std::string factsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Facts);
const std::string& factsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Facts);
root[factsPropertyName] = Json::Value(Json::arrayValue);
for (const auto& fact : GetFacts())
{
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/ImageSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Json::Value ImageSet::SerializeToJsonValue() const
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::ImageSize)] = ImageSizeToString(GetImageSize());
}

std::string const& itemsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Images);
const std::string& itemsPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Images);
root[itemsPropertyName] = Json::Value(Json::arrayValue);
for (const auto& image : m_images)
{
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/Media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Json::Value Media::SerializeToJsonValue() const
root[AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::AltText)] = GetAltText();
}

std::string sourcesPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Sources);
const std::string& sourcesPropertyName = AdaptiveCardSchemaKeyToString(AdaptiveCardSchemaKey::Sources);
root[sourcesPropertyName] = Json::Value(Json::arrayValue);
for (const auto& source : m_sources)
{
Expand Down
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/ParseContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,5 @@ namespace AdaptiveSharedNamespace

void ParseContext::SetLanguage(const std::string& value) { m_language = value; }

std::string ParseContext::GetLanguage() const { return m_language; }
const std::string& ParseContext::GetLanguage() const { return m_language; }
}
2 changes: 1 addition & 1 deletion source/shared/cpp/ObjectModel/ParseContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace AdaptiveSharedNamespace
void SetCanFallbackToAncestor(bool value) { m_canFallbackToAncestor = value; }

void SetLanguage(const std::string& value);
std::string GetLanguage() const;
const std::string& GetLanguage() const;

ContainerStyle GetParentalContainerStyle() const;
void SetParentalContainerStyle(const ContainerStyle style);
Expand Down
Loading