embedded-framework/utils/SharedData/src/SharedDataImpl.h
2023-11-17 07:46:03 -08:00

70 lines
2.2 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SHARED_DATA_IMPL_H
#define SHARED_DATA_IMPL_H
#include "SharedData.h"
#include "SharedMemory.h"
#include <memory>
#include <mutex>
constexpr int USER_NAME_BUF_LENGTH = 4;
#define USER_NAME_INIT_NAME "XING"
enum USER_DATA_INIT
{
USER_DATA_INIT_SUCCEED = 0,
USER_DATA_INIT_FAILED,
USER_DATA_INIT_END
};
typedef struct shared_data_header
{
const char *mCheckName;
} SharedDataHeader;
// TODO: Use mutex to luck memory data?
// typedef struct shared_memory_header
// {
// std::mutex mMutex;
// };
typedef struct user_data_header
{
char mUserName[USER_NAME_BUF_LENGTH];
unsigned int mDataLength;
int mIsInit;
} UserDataHeader;
class SharedDataCpp : public SharedMemory
{
public:
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId);
virtual ~SharedDataCpp() = default;
void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize);
const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength);
void SetWritableMemory(void *buf, const unsigned int &bufLength);
private:
void WritableDataInit(void);
private:
const SHARER_NAME mSharerName;
unsigned int mPrimaryReadSize;
unsigned int mMinorReadSize;
void *mSharedMemeory;
};
typedef struct shared_data_impl SharedDataImpl;
typedef struct shared_data_impl
{
SharedDataHeader mHeader;
SharedData mBase;
std::shared_ptr<SharedDataCpp> mSharedData;
} SharedDataImpl;
SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId);
#endif