- 1
https://github.com/ASDAlexander77/TypeScript2Cxx/blob/master/cpplib/core.h
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+5
https://github.com/ASDAlexander77/TypeScript2Cxx/blob/master/cpplib/core.h
Нужна помощь смелых и умных людей, надо сделать review кода и посоветовать что там по стандартам улучшить... короче любая помощь welcome
https://github.com/ASDAlexander77/TypeScript2Cxx/blob/master/cpplib/core.h
+1
struct any
{
std::any _val;
inline constexpr any() : _val{}
{
}
inline constexpr any(undefined_t) noexcept : _val{}
{
}
inline constexpr any(std::nullopt_t) noexcept : _val{}
{
}
template <typename T>
inline constexpr any(const T &val) : _val{val}
{
}
inline any(const any &val) noexcept : _val{val._val}
{
}
inline any(any &&val) noexcept : _val{std::move(val._val)}
{
}
inline any &operator=(const any &val)
{
_val = val._val;
return *this;
}
inline operator bool()
{
if (!_val.has_value())
{
return false;
}
auto type_index = std::type_index(_val.type());
if (type_index == std::type_index(typeid(int)))
{
return std::any_cast<int>(_val) > 0;
}
else if (type_index == std::type_index(typeid(double)))
{
return std::any_cast<double>(_val) > 0;
}
else if (type_index == std::type_index(typeid(tstring)))
{
return std::any_cast<tstring>(_val).size() > 0;
}
else if (type_index == std::type_index(typeid(tstring_view)))
{
return std::any_cast<tstring_view>(_val).size() > 0;
}
else if (type_index == std::type_index(typeid(const char_t *)))
{
auto v = std::any_cast<const char_t *>(_val);
return !v;
}
else if (type_index == std::type_index(typeid(std::nullptr_t)))
{
return false;
}
else if (type_index == std::type_index(typeid(bool)))
{
return std::any_cast<bool>(_val);
}
return true;
}
template <typename N = void>
requires Arithmetic<N> any operator+(N n)
{
return any{std::any_cast<N>(this->_val) + n};
}
friend std::ostream &operator<<(std::ostream &os, const any &val_)
{
auto val = val_._val;
if (val.has_value())
{
auto type_index = std::type_index(val.type());
if (type_index == std::type_index(typeid(int)))
{
os << std::any_cast<int>(val);
}
else if (type_index == std::type_index(typeid(double)))
{
os << std::any_cast<double>(val);
}
else if (type_index == std::type_index(typeid(tstring)))
{
os << std::any_cast<tstring>(val);
}
нужна помощь упростить этот ад.
0
reinterpret_cast<ppu_function_t>(static_cast<std::uintptr_t>(ppu_ref<u32>(cia)))(*this);
Лопни глазоньки. Шаблоны увечат С++ как бог черепаху.
https://github.com/RPCS3/rpcs3/blob/91d06a97296e5e418fb601284577475d8c7dfbaf/rpcs3/Emu/Cell/PPUThread.cpp
+1
if(object)
if(object->isSolid)
event.isCancelled = true;
;else //<
throw NullEventArgumentException("object == nullptr");
Новый токен в C++ про который знает только 0.6% говнокодеров
+1
template <class... Args>
void log(Args&&... args)
{
auto dummy = { (std::clog << args, 0)... };
std::clog << std::endl;
}
новый printf на с++
0
#define DECLARE_TYPE(Name, Type) \
using TYPE = Type; \
ObjectInterface s_##Type##ObjectInterface (Name, []()->ObjectType* { return new Type;
#define DECLARE_METHODS \
}, {
#define DECLARE_PROPERTIES \
}, {
#define DECLARE_METHOD(Method) \
{ #Method, reinterpret_cast <_ObjectMethodProc> (&TYPE::Method) },
#define DECLARE_CUSTOM_NAME_METHOD(Name, Method) \
{ Name, reinterpret_cast <_ObjectMethodProc> (&TYPE::Method) },
#define DECLARE_METHOD_MAPPER(_1,_2,DECLARE_METHOD,...) DECLARE_METHOD
#define METHOD(...) DECLARE_METHOD_MAPPER(__VA_ARGS__, DECLARE_CUSTOM_NAME_METHOD, DECLARE_METHOD)(__VA_ARGS__)
#define PROPERTY(Name, Get, Set) \
{ Name, { reinterpret_cast <_PropertyGettingProc> (&TYPE::Get), reinterpret_cast <_PropertySettingProc> (&TYPE::Set) } },
#define END_DECLARE_TYPE \
});
???
0
#include <iostream>
#include <chrono>
#include <thread>
#include <fstream>
#include <string>
using namespace std;
float Cheking = 0;
void read_file()
{
string line;
ifstream file("text.bin");
if (file)
{
while (getline(file, line))
{
file >> Cheking;
}
file.close();
}
}
void write_file()
{
ofstream myfile("text.bin");
if (myfile.is_open())
{
myfile << "\n";
myfile << Cheking << "\n";
myfile.close();
}
else
{
cout << "Unable to open file";
myfile.close();
}
}
int main()
{
using namespace chrono;
using namespace this_thread;
setlocale(LC_ALL, "ukr");
read_file();
cout << "Число: " << Cheking << "\t\t";
cout << endl;
if (Cheking == 0)
{
cin >> Cheking;
}
write_file();
sleep_for(milliseconds(1000));
return 0;
}
Простой гавнокод для храненния переменных в файле.
+3
template<typename T>
concept Addable = requires (T x) { x + x; }; // requires-expression
template<typename T> requires Addable<T> // requires-clause, not requires-expression
T add(T a, T b) { return a + b; }
все решено.. перехожу писать код на "конецепты"
+3
class Person {
protected name: string;
constructor(name: string) { this.name = name; }
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public get ElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
const howard = new Employee("Howard", "Sales");
console.log(howard.ElevatorPitch);
//===============================================>>>>>>>>>>>>>>>>>>
#ifndef TEST_H
#define TEST_H
#include "core.h"
using namespace js;
class Person;
class Employee;
class Person : public object, public std::enable_shared_from_this<Person> {
public:
string name;
Person(string name);
};
class Employee : public Person, public std::enable_shared_from_this<Employee> {
public:
string department;
Employee(string name, string department);
virtual any get_ElevatorPitch();
Employee(string name);
};
extern std::shared_ptr<Employee> howard;
#endif
#include "test.h"
using namespace js;
Person::Person(string name) {
this->name = name;
}
Employee::Employee(string name, string department) : Person(name) {
this->department = department;
}
any Employee::get_ElevatorPitch()
{
return "Hello, my name is "_S + this->name + " and I work in "_S + this->department + "."_S;
}
Employee::Employee(string name) : Person(name) {
}
std::shared_ptr<Employee> howard = std::make_shared<Employee>("Howard"_S, "Sales"_S);
void Main(void)
{
console->log(howard->get_ElevatorPitch());
}
int main(int argc, char** argv)
{
Main();
return 0;
}
Было делать нехрен в жизни и решил наговнокодить транспайлер с TypeScript в С++
https://github.com/ASDAlexander77/TypeScript2Cxx
+1
template<typename CharType>
class Formatter
{
private:
static int _ToString(Int32 Value){
return CString<CharType>::Snprintf(
(CharType*)GSupportToStringBuffer,
TO_STRING_BUFFER_SIZE,
"%" PRId32,
Value);
}
static int _ToString(float Value){
return CString<CharType>::Snprintf(
(CharType*)GSupportToStringBuffer,
TO_STRING_BUFFER_SIZE,
"%f",
Value);
}
template<typename First, typename ... Args>
static void _ConvertArgs(Array<GenericString<CharType>>& _ArgBuffer, const First& _First, const Args& ... _Args) {
_ArgBuffer.Add(ToString(_First));
_ConvertArgs(_ArgBuffer, _Args ...);
}
template<typename First>
static void _ConvertArgs(Array<GenericString<CharType>>& _ArgBuffer, const First& _First) {
_ArgBuffer.Add(ToString(_First));
}
static bool _ScanPlaceholder(const CharType* Format, size_t Index, size_t Length, size_t& Placeholder, size_t& LastIndex) {
size_t i = Index;
size_t j = 0;
while (i < Length && Format[i] != FormatterPlaceholder<CharType>::End)
{
if (!Char<CharType>::IsDigit(Format[i]))
{
return false;
}
else
{
GSupportToStringBuffer[j] = Format[i];
j++;
}
i++;
}
if (i == Length)
return false;
GSupportToStringBuffer[j] = 0;
#if defined(64BIT)
Placeholder = CString<CharType>::Atoi64((const CharType*)GSupportToStringBuffer);
#else
Placeholder = CString<CharType>::Atoi((const CharType*)GSupportToStringBuffer);
#endif
LastIndex = i;
return true;
}
public:
template<typename T>
static GenericString<CharType> ToString(const T& Value) {
int Length = Formatter<CharType>::_ToString(Value);
return GenericString<CharType>((char*)GSupportToStringBuffer, Length);
}
template<typename ... Args>
static GenericString<CharType> Format(const CharType* Format, const Args& ... _Args) {
Array<GenericString<CharType>> _FormatedArgs;
_FormatedArgs.Reserve(sizeof...(Args));
Formatter<CharType>::_ConvertArgs(_FormatedArgs, _Args ...);
const size_t _Length = CString<CharType>::Strlen(Format);
size_t Index = 0;
for (size_t i = 0; i < _Length; i++)
{
if (Format[i] == FormatterPlaceholder<CharType>::Begin)
{
size_t Placeholder = 0;
size_t LastIndex = 0;
if (_ScanPlaceholder(Format, i + 1, _Length, Placeholder, LastIndex) && Placeholder < sizeof...(Args))
{
Memory::Copy(GSupportFormatBuffer + Index, _FormatedArgs[Placeholder].Data(), _FormatedArgs[Placeholder].Length() * sizeof(CharType));
Index += _FormatedArgs[Placeholder].Length();
i = LastIndex;
}
}
else
{
GSupportFormatBuffer[Index] = Format[i];
Index++;
}
}
GSupportFormatBuffer[Index] = 0;
return GenericString<CharType>((const CharType*)GSupportFormatBuffer);
}
};
template<typename T>
forceinline String ToString(const T& Value){
return Formatter<char>::ToString<T>(Value);
}
template<typename ... Args>
forceinline String Format(const char* Format, const Args& ... _Args){
return Formatter<char>::Format(Format, _Args ...);
}
Три года назад писал printf аля Console.WriteLine в C#. Тут порезал до ста строк. https://pastebin.com/8BCLuBEm