我有一个简单的项目,需要三个只有头的库才能编译:websocketpp,spdlog和nlohmann/json.
项目结构如下所示:
??? src
??? app
? ??? CMakeLists.txt
? ??? src
? ??? test
??? CMakeLists.txt
??? core
? ??? CMakeLists.txt
? ??? include
? ??? src
? ??? test
??? vendor
??? install.cmake
??? nlohmann_json
??? spdlog
??? websocketpp
Run Code Online (Sandbox Code Playgroud)
根CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.6.1 FATAL_ERROR)
..
# External 3rd party libs that we include
include(vendor/install.cmake)
add_subdirectory(core)
add_subdirectory(app)
Run Code Online (Sandbox Code Playgroud)
这个想法基本上是每个子目录都是一个库(例如core
),并app
"聚合"所有子目录.每个库(例如core
)都是这样构建的(core/CMakeLists.txt
):
project(foo-core VERSION 0.1 LANGUAGES CXX)
add_library(foo-core
src/foobar/foobar.cc
src/foobaz/baz.cc) …
Run Code Online (Sandbox Code Playgroud) 我偶然发现了一个有趣的场景,我无法找到解决方案.假设我必须在一个序列中找到majorant(至少出现的数字,序列的大小n / 2 + 1
在哪里n
).这是我的实施:
public static int FindMajorant(IList<int> numbers)
{
return numbers
.GroupBy(x => x)
.Where(g => g.Count() >= numbers.Count / 2 + 1)
.Select(g => g.Key)
.SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用SingleOrDefault()
,如果在序列中找到该元素或返回该类型的默认值,则返回该元素:在这种情况下,它将返回,0
因为它是一个的默认值int
.例如,我的方法返回3
以下序列:
List<int> sampleNumbers = new List<int>() { 2, 2, 3, 3, 2, 3, 4, 3, 3 };
Run Code Online (Sandbox Code Playgroud)
这是预期的行为.
但是,如果序列中的主要部分为零(0
),会发生什么?它会返回0,但是这样,我怎么能确定它SingleOrDefault()
是默认值的零,还是主要的?也许,我可以使用Single()
,但这会引发一个非常不同的例外.我也可以抓住这个例外,但这对我来说似乎是一个不好的做法.所以我的问题是,处理这种情况的首选方法是什么?
我有一个内部使用静态方法HttpContext.Current.Application
来存储键值对并随后在整个应用程序中检索它的方法。
现在,我要做的是对该方法进行单元测试。我目前所拥有的是:
private const string Foo = "foobar";
[TestInitialize]
public void InitializeContext()
{
HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));
HttpContext.Current.Application["fooKey"] = Foo;
}
[TestCleanup]
public void DisposeContext()
{
HttpContext.Current = null;
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,但是当我尝试在中投射该值时TestMethod
,该值为null。
var fooValue = (string)HttpContext.Current.Application["fooKey"];
Run Code Online (Sandbox Code Playgroud)
显然,在我正在测试的方法中,该值也为null。
到目前为止,我尝试的是模拟上下文-效果也不佳。然后我看到了在SO上伪造的HTTP上下文解决方案,仍然无法正常工作。我什至尝试接受HttpApplicationStateBase
/ HttpApplicationStateWrapper
并通过它们进行工作,但这变得一团糟,我觉得有比这简单得多的解决方案。
我在运行Ubuntu 12.04和Apache的服务器上有一堆脚本.目前,我可以通过两种方式访问网站:例如:通过IP和域名,http://example.com
以及http://1.1.1.1
example.com有一条指向的A记录1.1.1.1
.我的问题是,有没有办法在服务器IP而不是域名访问网站时抛出403或类似的错误?
我能找到的关于我的问题的所有内容都是Apache HOW-TOs的链接,但是,没有关于如何实现这一点的信息.我的假设是我必须编辑默认vhost的配置文件,但我不知道究竟要改变什么.或者也许有一个模块呢?
我需要Digest::SHA1
一个我想在 cygwin 上运行的 Perl 脚本。但是,我必须使用 CPAN 对其进行配置。一切似乎都很好,直到我跑了cpan[1]> install Digest::SHA1
。事情继续,然后我得到以下错误:
/bin/sh: gcc-4: command not found
Makefile:327: 目标‘SHA1.o’的配方失败
make: * [SHA1.o] Error 127
GAAS/Digest-SHA1-2.13.tar.gz
/usr/bin /make -- NOT OK
'YAML' 未安装,不会存储持久状态
运行 make test
不能在没有成功的情况下进行测试make 运行 make install
Make 返回了错误状态,安装似乎不可能
在此命令期间失败:
GAAS/Digest-SHA1- 2.13.tar.gz : 不
看来我必须安装gcc4。make 错误不是因为它的路径,因为它设置正确(o conf make
在 CPAN 返回/usr/bin/make
并且我已经make
安装了二进制文件。)。不幸的是,cygwin 不提供 gcc4 包(它被标记为过时的包)。有什么解决方法可以解决这个问题吗?或者有没有一种方法可以在cygwin下编译gcc4,因为简单地gcc-4
更改gcc
为Makefile
不起作用。
我有一个构建在Play / Lagom堆栈之上的应用程序。我需要调用一项服务,该服务需要Source[T, NotUsed]
将文件流式传输到该服务。这是服务的接口:
def foo(fooId: UUID): ServiceCall[Source[ByteString, NotUsed], Source[String, NotUsed]]
Run Code Online (Sandbox Code Playgroud)
因此,我通过Accumulator.source
以下方式从Play文档中使用:
private def doSomething(fooId: UUID): BodyParser[Future[Seq[String]]] = BodyParser { _ =>
Accumulator.source[ByteString]
.mapFuture { source: Source[ByteString, NotUsed] =>
externalService
.foo(fooId)
.invoke(source)
.map { x =>
Right(x.runWith(Sink.seq[String]))
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在mapFuture
调用中,source
的类型为Source[ByteString, _]
,但是如果Source[ByteString, NotUsed]
如上例所示,将其更改为以便调用服务,则在IDE中出现Source[ByteString, _]
预期的错误
。难道不_
意味着我可以将类型更改为想要的任何类型,包括NotUsed
吗?另一方面,这两者在语义上是否应该等效吗?我发现,在某些以前的版本中,当实例化值无关紧要时,引入Akka团队akka.NotUsed
来进行替换Unit
,但这仍然无法为我提供解决问题的线索。
上面的代码段类似于Play文档中有关将身体指向其他位置的示例。