我正在尝试使用C++ 11线程为我的小游戏实现更新线程.我已经"尽可能快地"更新周期,但我想限制它说,每秒60次.如何剩下剩余的时间?
Core::Core()
{
std::thread updateThread(update); // Start update thread
}
void Core::update()
{
// TODO Get start time
// Here happens the actual update stuff
// TODO Get end time
// double duration = ...; // Get the duration
// Sleep if necessary
if(duration < 1.0 / 60.0)
{
_sleep(1.0 / 60.0 - duration);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个回收器视图,用于LinearSnapHelper在用户滚动项目时捕捉项目.现在,我想听一下快照,最好是获取被捕捉的项目的索引.但是,我无法弄清楚是否有办法做到这一点.
起初我以为LinearSnapHelper的findTargetSnapPosition()将返回索引卡(如文档说),但是这是不正确的.它为第一个项随机返回-1或0,当滚动列表时,它会被随机调用.有时,该方法根本没有被调用; 有时,索引不正确,有时它是正确的.似乎尝试使用它找到索引是没用的.
那么:我如何找出回收者视图捕捉到的项目?
我有一个Component特征,该特征具有返回索引的方法,如下所示:
trait Component {
fn index(&self) -> usize;
}
Run Code Online (Sandbox Code Playgroud)
这些索引用于在位集中设置标志。例如,Component返回索引为5 的特征对象将导致在容器中设置第5位。
目前,我为每种实现类型手动返回运行索引:
struct Foo;
struct Bar;
impl Component for Foo {
fn index(&self) -> usize { 0 }
}
impl Component for Bar {
fn index(&self) -> usize { 1 }
}
Run Code Online (Sandbox Code Playgroud)
特质对象插入到容器中,该容器使用位集跟踪添加的组件:
struct Container<'a> {
components: Vec<Component + 'a>,
bits: BitSet
}
impl<'a> Container<'a> {
fn add<T: Component + 'a>(&mut self, component: T) {
self.components.push(component);
self.bits.set(component.index());
}
}
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,但是手动返回每种实现类型的索引很麻烦。我怎样才能使每种实现类型都自动获得其索引?
我的项目中有多个构建配置,我想根据当前选择的配置交换一些.CPP文件.我如何在Visual Studio 2013中执行此操作?
我正在尝试使用NDK编译C++代码以在Android上使用.但是,我在尝试编译时遇到此错误:
error: 'JNIEXPORT' does not name a type (File: JNIApi.h, line: 9)
error: 'JNIEXPORT' does not name a type (File: JNIApi.h, line: 10)
Run Code Online (Sandbox Code Playgroud)
JNIApi.h看起来像这样:
#pragma once
extern "C"
{
JNIEXPORT void JNICALL Java_com_manabreak_tremortest_TremorLauncher_initTremor(JNIEnv* jenv, jobject obj);
JNIEXPORT void JNICALL Java_com_manabreak_tremortest_TremorLauncher_setSurface(JNIEnv* jenv, jobject obj, jobject surface);
}
Run Code Online (Sandbox Code Playgroud)
构建过程输出:
Build started 1.8.2014 11:14:17.
1>Project "E:\Tremor\build\Tremor.vcxproj" on node 2 (Build target(s)).
1>ClCompile:
E:\ADT\ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe JNIApi.cpp
E:\ADT\ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe -o Android/Debug/JNIApi.o -marm -fno-strict-aliasing -funswitch-loops -finline-limit=100 -fomit-frame-pointer -fno-exceptions -fpic -fstack-protector -fno-rtti -fno-short-enums -x c++ -Wno-psabi -IE:/ADT/ndk/platforms/android-19/arch-arm/usr/include -IE:/ADT/ndk/sources/cxx-stl/gnu-libstdc++/4.8/include -IE:/ADT/ndk/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include …Run Code Online (Sandbox Code Playgroud) 我想知道为什么我推回到向量中的元素会在这种情况下调用它的析构函数:
#include <iostream>
#include <vector>
class Foo
{
public:
Foo(int a)
: m_a(a)
{
std::cout << "Foo ctor() " << m_a << std::endl;
}
~Foo()
{
std::cout << "Foo dtor() " << m_a << std::endl;
}
private:
int m_a;
};
class FooStorage
{
public:
static void createFoo(int a)
{
m_foos.push_back(Foo(a));
}
static std::vector<Foo> m_foos;
};
std::vector<Foo> FooStorage::m_foos;
int main()
{
std::cout << "Before: " << FooStorage::m_foos.size() << std::endl;
FooStorage::createFoo(53);
std::cout << "After: " << FooStorage::m_foos.size() << std::endl;
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我正在摆弄Java正则表达式,我试图提出一种模式,允许在任何其他地方使用某些字符集,但它不能以允许集中的某些字符开头.
例如,假设允许的字符是从A到Z,但字符串不能以X或Z开头.我该怎么做?我想出来了^[XZ][^A-Z]+,虽然它起作用,但它允许字符串以不在集合中的其他字母开头(例如用标点符号).
一个简单的问题:如果我有这样的一行:
int foo::bar::baz() {...
Run Code Online (Sandbox Code Playgroud)
这怎么解释?baz()是函数名,但是'foo'是类还是名称空间?'bar'是一个类还是一个子类还是什么?
我正在浏览Psycle源代码,这条线引起了我的注意:
bool user_choose_dialog(HWnd const window_handle,
format const * const source_format = 0,
format const * const proposed_format = 0;
std::string const & caption = "");
Run Code Online (Sandbox Code Playgroud)
如您所见,参数列表中有分号.我注意到有一个老问题,答案是分号用于前向声明.但是,在这种情况下,我看不到前瞻性声明.那个分号的含义是什么?它是有意义的和功能性的,还是来源中的拼写错误?
我试图搜索这个,但我没有找到任何答案.我们来看看这个:
class Foo
{
Foo();
Bar a; // 'a', the object, gets created (even if I don't want to!)
Bar* b; // 'b', the pointer, gets created, but the object doesn't
}
Foo::Foo()
{
a = a(); // I'd like to create 'a' here instead. This just "replaces" it
b = new Bar(); // 'b', the object, gets created
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:我可以声明一个对象而不创建它吗?或者我总是要使用指针?
我正在使用Django,Django REST框架,Django-rest-auth和Django-allauth编写服务器应用程序。我有一种用于在用户之间传递消息的方法,只有在接收者登录后才应该发生这种情况。
但是,is_authenticated()即使用户已注销(似乎叫rest-auth/logout/,该对象又应调用Django的注销),该用户对象的方法似乎仍返回True 。是什么原因造成的?我在这里想念什么吗?
这是我的代码:
class SendMessage(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = MessageSerializer
def perform_create(self, serializer):
m = self.request.data['msg']
targetUser = User.objects.get(pk = self.request.data['user'])
if targetUser.is_authenticated():
# Send message
else:
# Don't send message
Run Code Online (Sandbox Code Playgroud) authentication django django-rest-framework django-allauth django-rest-auth
我使用rapidjson来读取JSON文件,其中一些值是字符串.现在,rapidjson的GetString()方法返回一个const char *.不过,我想把它存储起来std::string.我试过这个:
const char* foo = d["foo"].GetString();
printf("Foo: %s\n", foo); // Prints correctly
std::string fooStr(foo);
printf("FooString: %s\n", fooStr); // Gibberish
Run Code Online (Sandbox Code Playgroud)
我如何得到正确的std::string?
c++ ×8
java ×2
string ×2
android ×1
android-ndk ×1
c++11 ×1
django ×1
function ×1
parameters ×1
pointers ×1
regex ×1
rust ×1
rust-macros ×1