You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

187 lines
5.5 KiB

// Copyright 2018 Mobvoi Inc. All Rights Reserved.
// Author: gfbai@mobvoi.com (Gaofeng Bai)
#ifndef SDS_INTERFACE_PARAMETER_H_
#define SDS_INTERFACE_PARAMETER_H_
#include <iostream>
#include <map>
#include <string>
#include <vector>
//#ifdef _SDK_DLL_IE
//#define SDK_DLL_API _declspec(dllexport)
//#else
//#define SDK_DLL_API _declspec(dllimport)
//#endif
namespace mobvoi {
namespace sds {
class SDK_DLL_API Buf {
public:
Buf(): addr_(nullptr), size_(0) {}
Buf(char* addr, size_t size) : addr_(addr), size_(size) {}
void SetAddr(char* addr) { addr_ = addr; }
char* GetAddr() const { return addr_; }
void SetSize(size_t size) { size_ = size; }
size_t GetSize() const { return size_; }
private:
char* addr_;
size_t size_;
private:
friend std::ostream& operator<<(std::ostream& os, const Buf& buf);
friend bool operator==(const Buf& lhs, const Buf& rhs);
};
class Parameter;
class SDK_DLL_API CallBackBase {
public:
virtual ~CallBackBase() {}
virtual void CallBack(const Parameter&) = 0;
};
typedef CallBackBase* CallBackBasePtr;
typedef std::vector<std::string> StrVec;
typedef void* Handle;
struct SDK_DLL_API Value {
enum Type {
kUndefined,
kString,
kBuf,
kInt,
kDouble,
kCallBack,
kBool,
kStrVec,
kHandle,
};
// Constructors
Value();
Value(const Value& v);
~Value();
explicit Value(const char* k);
explicit Value(const std::string& k);
Value(const std::string& k, const char* s);
Value(const std::string& k, const std::string& s);
Value(const std::string& k, const Buf& b);
Value(const std::string& k, short s); // NOLINT
Value(const std::string& k, int i);
Value(const std::string& k, long l); // NOLINT
Value(const std::string& k, float f);
Value(const std::string& k, double d);
Value(const std::string& k, std::nullptr_t c);
Value(const std::string& k, CallBackBasePtr c);
Value(const std::string& k, bool b);
Value(const std::string& k, const StrVec& sv);
Value(const std::string& k, Handle h);
// Setters
Value& operator=(const char* s);
Value& operator=(const std::string& s);
Value& operator=(const Buf& b);
Value& operator=(short s); // NOLINT
Value& operator=(int i);
Value& operator=(long l); // NOLINT
Value& operator=(float f);
Value& operator=(double d);
Value& operator=(std::nullptr_t c);
Value& operator=(CallBackBasePtr c);
Value& operator=(bool b);
Value& operator=(const StrVec& sv);
Value& operator=(Handle h);
Value& operator=(const Value& v);
// Getters
operator std::string() const;
operator Buf() const;
operator int() const;
operator double() const;
operator CallBackBasePtr() const;
operator bool() const;
operator StrVec() const;
operator Handle() const;
const std::string& AsString() const;
Buf AsBuf() const;
int AsInt() const;
double AsDouble() const;
CallBackBasePtr AsCallBack() const;
bool AsBool() const;
const StrVec& AsStrVec() const;
Handle AsHandle() const;
const std::string& GetKey() const;
Type GetType() const;
private:
class Data;
Data* data_ = nullptr;
private:
friend std::ostream& operator<<(std::ostream& os, const Value& value);
friend bool operator==(const Value& lhs, const Value& rhs);
friend bool operator==(const Data& lhs, const Data& rhs);
};
typedef std::vector<std::string> ParamKeys;
class SDK_DLL_API Parameter {
public:
Parameter() {}
explicit Parameter(const std::string& intent): intent_(intent) {}
~Parameter() {}
void SetIntent(const std::string& intent) { intent_ = intent; }
std::string GetIntent() const { return intent_; }
void SetParam(const std::string& key, const char* value);
void SetParam(const std::string& key, const std::string& value);
void SetParam(const std::string& key, const Buf& value);
void SetParam(const std::string& key, short value); // NOLINT
void SetParam(const std::string& key, int value); // NOLINT
void SetParam(const std::string& key, long value); // NOLINT
void SetParam(const std::string& key, float value);
void SetParam(const std::string& key, double value);
void SetParam(const std::string& key, std::nullptr_t value);
void SetParam(const std::string& key, CallBackBasePtr value);
void SetParam(const std::string& key, bool value);
void SetParam(const std::string& key, const Value& value);
void SetParam(const std::string& key, const StrVec& value);
void SetParam(const std::string& key, Handle value);
const Value& GetParam(const std::string& key) const;
bool HasParam(const std::string& key) const;
ParamKeys GetParamKeys() const;
bool DropParam(const std::string& key);
void ClearParam();
Value& operator[](const std::string& key);
const Value& operator[](const std::string& key) const;
private:
std::string intent_;
mutable std::map<std::string, Value> params_;
private:
SDK_DLL_API friend std::ostream& operator<<(std::ostream& os, const Parameter& value);
};
} // namespace sds
} // namespace mobvoi
#endif // SDS_INTERFACE_PARAMETER_H_