Skip to content

Commit

Permalink
finished base funtion.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingstarcraft committed Mar 16, 2021
0 parents commit daf101e
Show file tree
Hide file tree
Showing 23 changed files with 1,846 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
windows/.vs
windows/python/x64
Binary file added data/lena.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions example/python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifdef USE_OPENCV
#include <opencv2/opencv.hpp>
#endif
#include "zero/python/python.h"



int main(int argc, char* argv[]) {
// open python engine
// zero::Python python(python_root) if can't find python with "where/which python" in cmd.
zero::Python python;


//import cv2
auto cv2 = python["cv2"];

//image = cv2.imread("data/lena.jpg")
auto python_image = cv2["imread"]("E:/zero/data/lena.jpg");
cv2["imwrite"]("E:/zero/data/lena.png", python_image);

#ifdef USE_OPENCV
try {
auto cpp_image = python_image.get<zero::Mat<uchar>>().get<cv::Mat>();
cv::imshow("cpp", cpp_image);
cv::waitKey(0);
}
catch (std::exception& error) {
std::cout << error.what();
};

#endif

return 0;
}
15 changes: 15 additions & 0 deletions include/zero/base/algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <string>
#include "zero/common.h"
namespace zero {
std::string ZERO_API Lower(const std::string &input);
std::string ZERO_API Execute(const std::string& command, const bool error = false);
std::string ZERO_API Convert(const std::wstring& input);
std::wstring ZERO_API Convert(const std::string& input);
// template <typename To, typename From>
// typename std::enable_if<!std::is_same<To, From>::value, To>::type lexical_cast(const From& from);
// template <typename To, typename From>
// typename std::enable_if<std::is_same<To, From>::value, To>::type lexical_cast(const From& from);
//
// template ZERO_API double lexical_cast<double>(const double& from);
}
195 changes: 195 additions & 0 deletions include/zero/base/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#pragma once
#include <stdexcept>
#include <vector>
namespace zero {

class Object {
public:
Object() {};
virtual ~Object() {};
};
template <typename Type>
class Template : public Object {
public:
Template(const Type &data) :data_(data) {};

const Type& get() const { return data_; }
Type& get() { return data_; }
private:
Type data_;
};

template<typename Type>
Type RemoveReference(const Type& s) {
throw std::runtime_error("this function can't callable.");
return Type();
}

template <typename Type>
struct Range {
Type minimum;
Type maximum;
};

template <typename Type>
class Storage: public Object {
public:
virtual inline bool empty() const {
return this->datas_.empty();
}
virtual inline void clear() {
datas_.clear();
}

protected:
Type datas_;
};

template <typename Type>
class Iterator: virtual public Storage<Type> {
public:
using iterator = typename Type::iterator;
using const_iterator = typename Type::const_iterator;

virtual inline iterator begin() {
return this->datas_.begin();
}
virtual inline const_iterator begin() const {
return this->datas_.begin();
}
virtual inline iterator end() {
return this->datas_.end();
}

virtual inline const_iterator end() const {
return this->datas_.end();
}

virtual inline size_t size() const {
return this->datas_.size();
}
};


template <typename Type, typename Stype>
class Array: virtual public Storage<Stype> {
public:
Array() {};
~Array() {};
virtual inline Type &operator[](const int id) {
if (id >= 0) {
return this->datas_[id];
}
else {
int size = static_cast<int>(this->datas_.size());
return this->datas_[id + size];
}
}
virtual inline const Type &operator[](const int id) const {
if (id >= 0) {
return this->datas_[id];
}
else {
int size = static_cast<int>(this->datas_.size());
return this->datas_[id + size];
}
}
virtual void resize(const size_t size, const Type &value) {
this->datas_.resize(size, value);
}
virtual void resize(const size_t size) {
this->datas_.resize(size);
}
virtual inline void push_back(const Type &data) {
this->datas_.push_back(data);
}
};

template <typename Type, typename Stype>
class Array<Type&, Stype>: virtual public Storage<Stype> {
public:
Array() {};
~Array() {
clear();
};
virtual inline Type &operator[](const int id) {
if (id >= 0) {
return *(this->datas_[id]);
}
else {
int size = static_cast<int>(this->datas_.size());
return *(this->datas_[id + size]);
}
}
virtual inline const Type &operator[](const int id) const {
if (id >= 0) {
return *(this->datas_[id]);
}
else {
int size = static_cast<int>(this->datas_.size());
return *(this->datas_[id + size]);
}
}
template<typename... Atype>
Type * New(Atype&&... args) {
auto pt = new Type(args...);
storage_.push_back(pt);
push_back(pt);
return pt;
}

Type * New() {
auto pt = new Type;
storage_.push_back(pt);
push_back(pt);
return pt;
}
// template <typename ...Ptype>
// virtual inline Type *New(Ptype&&... args) {
// auto ptr = std::make_shared<Type>(args...);
// storage_.pop_back(ptr);
// return ptr;
// return nullptr;
// }

virtual void resize(const size_t size, Type *value) {
this->datas_.resize(size, value);
}
virtual void resize(const size_t size) {
this->datas_.resize(size);
}
virtual inline void push_back(Type *data) {
this->datas_.push_back(data);
}

virtual inline void clear() {
Storage<Stype>::clear();
for (const auto &storage : storage_) {
if(storage) delete storage;
}
storage_.clear();
}
private:
std::vector<Type *> storage_;
};


template<size_t id = 0, typename FType, typename ...AType>
inline typename std::enable_if_t<id == sizeof ...(AType)> for_each(std::tuple<AType ...>&, FType) { }

template<size_t id = 0, typename FType, typename ...AType>
inline typename std::enable_if_t < id < sizeof ...(AType)> for_each(std::tuple<AType ...>& argv, FType &function) {
function(std::get<id>(argv));
for_each<id + 1, FType, AType...>(argv, function);
}

template<size_t id = 0, typename FType, typename ...AType>
inline typename std::enable_if_t<id == sizeof ...(AType)> for_each(std::tuple<AType& ...>, FType) { }

template<size_t id = 0, typename FType, typename ...AType>
inline typename std::enable_if_t < id < sizeof ...(AType)> for_each(std::tuple<AType& ...> argv, FType function) {
function(std::get<id>(argv));
for_each<id + 1, FType, AType...>(argv, function);
}

}
63 changes: 63 additions & 0 deletions include/zero/base/vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once
#include <vector>
#include <map>
//#include <algorithm>
#include "base.h"

namespace zero {
template<typename Type>
class vector : public Iterator<std::vector<Type>>, public Array<Type, std::vector<Type>> {
public:
vector() {};
~vector() {};

vector(size_t size) {
this->datas_.resize(size);
}
vector(size_t size, Type value) {
this->datas_.resize(size, value);
}
vector(const std::initializer_list<Type> &datas) {
this->datas_ = datas;
}

virtual vector &operator=(std::initializer_list<Type> datas) {
this->datas_ = datas;
return *this;
}

// virtual vector operator-(const vector &datas) {
// vector reval;
// auto size = std::min(datas.size(), this->size());
// reval.resize(size);
// for (auto i = 0; i < size; i++) {
// reval[i] = this->datas_[i] - datas[i];
// }
// return reval;
// }
};

template<typename Type>
class vector<Type &>: public Iterator<std::vector<Type *>>, public Array<Type &, std::vector<Type *>> {
public:
vector() {};
~vector() {};

vector(size_t size) {
this->datas_.resize(size);
}
vector(size_t size, Type value) {
this->datas_.resize(size, value);
}
vector(const std::initializer_list<Type *> &datas) {
this->datas_ = datas;
}

virtual vector &operator=(const std::initializer_list<Type *> &datas) {
this->datas_ = datas;
return *this;
}
private:

};
}
Loading

0 comments on commit daf101e

Please sign in to comment.