Skip to content

Commit 4f728b5

Browse files
committed
Add data property to Asset
1 parent 260dfe4 commit 4f728b5

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

include/scratchcpp/asset.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class LIBSCRATCHCPP_EXPORT Asset : public Entity
2828

2929
const std::string &dataFormat() const;
3030

31+
const char *data() const;
32+
void setData(const char *data);
33+
34+
protected:
35+
virtual void processData(const char *data) { }
36+
3137
private:
3238
spimpl::unique_impl_ptr<AssetPrivate> impl;
3339
};

src/scratch/asset.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ const std::string &Asset::dataFormat() const
3838
{
3939
return impl->dataFormat;
4040
}
41+
42+
/*! Returns the asset data. */
43+
const char *Asset::data() const
44+
{
45+
return impl->data;
46+
}
47+
48+
/*! Sets the asset data. */
49+
void Asset::setData(const char *data)
50+
{
51+
impl->data = data;
52+
processData(data);
53+
}

src/scratch/asset_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct AssetPrivate
1717
std::string name;
1818
std::string dataFormat;
1919
std::string fileName;
20+
const char *data = nullptr;
2021
};
2122

2223
} // namespace libscratchcpp

test/assets/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
add_executable(
33
asset_test
44
asset_test.cpp
5+
testasset.cpp
6+
testasset.h
57
)
68

79
target_link_libraries(

test/assets/asset_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <scratchcpp/asset.h>
22

33
#include "../common.h"
4+
#include "testasset.h"
45

56
using namespace libscratchcpp;
67

@@ -21,3 +22,15 @@ TEST(AssetTest, Id)
2122
ASSERT_EQ(asset.id(), "b");
2223
ASSERT_EQ(asset.fileName(), "b.svg");
2324
}
25+
26+
TEST(AssetTest, Data)
27+
{
28+
TestAsset asset;
29+
ASSERT_EQ(asset.data(), nullptr);
30+
31+
static const char *data = "abcd";
32+
asset.setData(data);
33+
ASSERT_EQ(asset.data(), data);
34+
ASSERT_EQ(asset.processedData, data);
35+
ASSERT_EQ(asset.callCount, 1);
36+
}

test/assets/testasset.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "testasset.h"
2+
3+
using namespace libscratchcpp;
4+
5+
TestAsset::TestAsset() :
6+
Asset("", "", "")
7+
{
8+
}
9+
10+
void TestAsset::processData(const char *data)
11+
{
12+
processedData = data;
13+
callCount++;
14+
}

test/assets/testasset.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <scratchcpp/asset.h>
4+
5+
namespace libscratchcpp
6+
{
7+
8+
class TestAsset : public Asset
9+
{
10+
public:
11+
TestAsset();
12+
13+
const char *processedData = nullptr;
14+
unsigned int callCount = 0;
15+
16+
protected:
17+
void processData(const char *data) override;
18+
};
19+
20+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)